c programming language learn note 4

本文详细介绍了希尔排序算法的实现原理及过程。通过逐步分析代码,解释了希尔排序如何通过间隔比较和交换来对数组进行排序,最终达到完全有序的状态。


这章讲的是控制流,几乎每种语言豆有这个的
也没有什么新的发现,
说简单,就这些语法
说很难,好的算法就出这里的

挑一个我看懂的排序算法吧

/* shellsort: sort v[0]...v[n-1] into increasing order */
void shellsort(int v[], int n)
{
58
int gap, i, j, temp;
for (gap = n/2; gap > 0; gap /= 2)//间隔,直到间隔为1的时候停止循环
      for (i = gap; i < n; i++)
           for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {//以特定的间隔实现两个数的排序,循环结束后排序成功
                 temp = v[j];
                 v[j] = v[j+gap];
                 v[j+gap] = temp;
                 }

发现,这个要解释清楚好像很困难的说^_^

帮我完成xv6实验的代码,以下是实验指南: sleep (easy) Implement a user-level sleep program for xv6, along the lines of the UNIX sleep command. Your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c. Some hints: Before you start coding, read Chapter 1 of the xv6 book. Put your code in user/sleep.c. Look at some of the other programs in user/ (e.g., user/echo.c, user/grep.c, and user/rm.c) to see how command-line arguments are passed to a program. Add your sleep program to UPROGS in Makefile; once you've done that, make qemu will compile your program and you'll be able to run it from the xv6 shell. If the user forgets to pass an argument, sleep should print an error message. The command-line argument is passed as a string; you can convert it to an integer using atoi (see user/ulib.c). Use the system call sleep. See kernel/sysproc.c for the xv6 kernel code that implements the sleep system call (look for sys_sleep), user/user.h for the C definition of sleep callable from a user program, and user/usys.S for the assembler code that jumps from user code into the kernel for sleep. sleep's main should call exit(0) when it is done. Look at Kernighan and Ritchie's book The C programming language (second edition) (K&R) to learn about C. Run the program from the xv6 shell: $ make qemu ... init: starting sh $ sleep 10 (nothing happens for a little while) $ Your solution is correct if your program pauses when run as shown above. Run to see if you indeed pass the sleep tests. make grade Note that runs all tests, including the ones for the assignments below. If you want to run the grade tests for one assignment, type: make grade $ ./grade-lab-util sleep This will run the grade tests that match "sleep". Or, you can type: which does the same. $ make GRADEFLAGS=sleep grade
最新发布
11-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值