1. 冒泡排序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* 5 *max times:n*(n-1)/2 6 *根据大小交换相邻数值的位置,每次都会将最大的一个数字放在最后 7 *因此,每次从0开始,且每次执行完毕,则次数减1.关键注意j的取值。 8 ****/ 9 void maopao_order(int *nums,int count){ 10 int i,j; 11 int tmp; 12 i = 0; 13 j = 0; 14 for(i = 0; i < count; i++){ 15 for(j = 0; j < count-1-i; j++){ 16 if(nums[j] > nums[j+1]){ 17 tmp =nums[j]; 18 nums[j] = nums[j+1]; 19 nums[j+1] = tmp; 20 } 21 } 22 } 23 24 } 25 26 /* 27 *the first argument is count of number 28 **/ 29 int main(int argc, char *argv[]){ 30 int count, numb; 31 int i; 32 int *nums = (int *)malloc(sizeof(int)*10); 33 34 if( argc < 2){ 35 printf("too few argument\n"); 36 free(nums); 37 exit(1); 38 } 39 40 printf("input number is %s\n",argv[1]); 41 count = atoi(argv[1]); 42 43 printf("please input:\n"); 44 for(i = 0; i < count; i++){ 45 scanf("%d",nums+i); 46 } 47 48 maopao_order(nums,count); 49 50 printf("output is:\n"); 51 for(i = 0; i < count; i++){ 52 printf("%d\t",*(nums+i)); 53 if(i == count -1) 54 printf("\n"); 55 } 56 57 }
2.选择排序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* 5 *max times:n*(n-1)/2 6 *每次选择出一个最小的放在数组的最前面。 7 ****/ 8 void select_order(int *nums,int count){ 9 int i,j; 10 int tmp; 11 i = 0; 12 j = 0; 13 for(i = 0; i < count; i++){ 14 for(j = i; j < count -1; j++){ 15 if(nums[i] > nums[j+1]){ 16 tmp =nums[i]; 17 nums[i] = nums[j+1]; 18 nums[j+1] = tmp; 19 } 20 } 21 } 22 23 } 24 25 /* 26 *the first argument is count of number 27 **/ 28 int main(int argc, char *argv[]){ 29 int count, numb; 30 int i; 31 int *nums = (int *)malloc(sizeof(int)*10); 32 33 if( argc < 2){ 34 printf("too few argument\n"); 35 free(nums); 36 exit(1); 37 } 38 39 printf("input number is %s\n",argv[1]); 40 count = atoi(argv[1]); 41 42 printf("please input:\n"); 43 for(i = 0; i < count; i++){ 44 scanf("%d",nums+i); 45 } 46 47 select_order(nums,count); 48 49 printf("output is:\n"); 50 for(i = 0; i < count; i++){ 51 printf("%d\t",*(nums+i)); 52 if(i == count-1) 53 printf("\n"); 54 } 55 56 return 0; 57 }
3.快速排序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* 5 *nums:数组,l:排序数组中最左边的index,r:排序数组中最右边的index;index均为nums中数值的下标 6 * 7 */ 8 void quicksort(int nums[], int l, int r){ 9 int left,right; 10 int i; 11 int key; 12 int tmp; 13 14 left = l; 15 right = r; 16 tmp = l; 17 key = nums[l]; 18 19 //或者if((right -left) < 1){ 20 if(r <= l){ 21 return ; 22 } 23 24 while(left < right){ 25 26 //从右往左直到发现一个数比基数小 27 while(right > left){ 28 if(nums[right] < key){ 29 nums[tmp] = nums[right];// 30 tmp = right;//记录需要替换值的位值,下次在该位置替换填充 31 right--; 32 break; 33 } 34 right--; 35 } 36 37 //从左往右直到发现一个数比基数大 38 while(left < right){ 39 //起始值为left,不必比较,从left + 1 开始 40 if(nums[left + 1] > key){ 41 //大的数填充在右边,需要序号比tmp小才行 42 if(left > tmp){ 43 left++; 44 continue; 45 } 46 nums[tmp] = nums[left + 1]; 47 tmp = left + 1; 48 left++; 49 break; 50 } 51 left++; 52 } 53 54 } 55 //此时可能:1. right比left大,left为偶数,right为奇数;2. left和right相等 56 nums[tmp] = key; 57 58 //for debug 59 /* 60 printf("tmp=%d,r=%d,count=%d\n",tmp,r,count); 61 for(i = 0; i < count; i++){ 62 if(i%10==0) 63 printf("\n"); 64 printf("%d\t",nums[i]); 65 if(i == count -1) 66 printf("\n"); 67 } 68 */ 69 quicksort(nums, tmp + 1, r);//右边的部分重复操作 70 quicksort(nums, l, tmp);//左边的部分重复操作 71 72 } 73 74 75 int main(int argc, char *argv[]){ 76 int count; 77 int *nums; 78 int i; 79 80 //带输入排序的数组的大小 81 count = atoi(argv[1]); 82 nums = (int *)malloc(sizeof(int)*count); 83 84 //输入要排序的数值 85 printf("please input num:\n"); 86 for(i = 0; i < count; i++){ 87 scanf("%d",nums+i); 88 } 89 //l = 0;r = count -1,为数组的起始、终止下标 90 quicksort(nums, 0, count - 1); 91 92 //排序后输出 93 printf("the output is :"); 94 for(i = 0; i < count; i++){ 95 if(i%10==0) 96 printf("\n"); 97 printf("%d\t",nums[i]); 98 if(i == count -1) 99 printf("\n"); 100 } 101 102 //释放malloc的内存 103 free(nums); 104 }
4.插入排序
1 #include <stdio.h> 2 #include <stdlib.h> 3 void insert(int *nums, int count){ 4 int tmp_value;//记录待插入的数 5 int i,j; 6 /* 7 * 每次插入一个,从nums中选一个插入到前面的有序排列中。选取第i个值待插入时,此时 8 * 第i个值前面的序列是已经按顺序排好的有序列;对第i个值进行插入时候,不需要考虑 9 * 第i个值后面的元素,只需要插入后的i个元素是有序的即可。 10 * 11 */ 12 for(i = 1; i < count; i++){ 13 if(nums[i] < nums[i-1]){ 14 //插入一个元素后可能需要将i-1个元素中部分整体往后移动仅仅一个位置 15 //此时,如果最终真正的插入,那么nums[i-1]移动到i位置处 16 tmp_value = nums[i]; 17 j = i; 18 //将tmp_value和第i个前面的数依次比较,如果tmp_value小就要将比较数后移一位 19 //后移一位后,就会出现连着的两个数一样,其中一个移动前位置的数,就继续等着 20 //该数前面的数来覆盖它,或者最后该位置填充tmp_value 21 while(j>0 &&nums[j-1]>tmp_value){ 22 nums[j] = nums[j-1]; 23 j--; 24 } 25 nums[j] = tmp_value; 26 } 27 } 28 } 29 30 /* 31 *the first argument is count of number 32 **/ 33 int main(int argc, char *argv[]){ 34 int count, numb; 35 int i; 36 int *nums; 37 38 if( argc < 2){ 39 printf("too few argument\n"); 40 // free(nums); 41 return 1; 42 exit(1); 43 } 44 45 printf("input number is %s\n",argv[1]); 46 count = atoi(argv[1]); 47 nums = (int *)malloc(sizeof(int)*count); 48 printf("please input:\n"); 49 for(i = 0; i < count; i++){ 50 scanf("%d",nums+i); 51 } 52 53 insert(nums,count); 54 55 printf("output is:\n"); 56 for(i = 0; i < count; i++){ 57 if(i%10 == 0) 58 printf("\n"); 59 printf("%d\t",*(nums+i)); 60 if(i == count -1) 61 printf("\n"); 62 } 63 64 }
5.希尔排序
希尔排序是在插入排序的基础上演变演变出来的排序方法。
1 #include <stdio.h> 2 #include <stdlib.h> 3 void shellsort(int *nums, int count) 4 { 5 int i,j,k,gap; 6 int tmp_value; 7 int m; 8 //选定步长,分组个数大约为count/2个 9 for(gap = count/2; gap > 0; gap = gap/2){ 10 //选定一组对该组进行插入排序,组数为gap个 11 for(i = 0; i < gap; i++){ 12 printf("步长为%d时,执行第%d次插入排序\n",gap,i); 13 //插入排序的数组中第一个数默认插入,可以直接从第二个开始:j = i+1 14 for(j = i+gap; j < count; j+= gap){ 15 16 if(nums[j] < nums[j-gap]){ 17 tmp_value = nums[j]; 18 19 k = j; 20 //注意k的界限值k>gap,否则出现段错误 21 while(k >= gap && nums[k-gap] > tmp_value){ 22 nums[k] = nums[k-gap]; 23 k-= gap; 24 } 25 nums[k] = tmp_value; 26 m = 0; 27 while(m < count){ 28 printf("%d\t",nums[m++]); 29 if(m == count) 30 printf("\n"); 31 } 32 } 33 } 34 } 35 } 36 37 } 38 39 40 /* 41 *the first argument is count of number 42 **/ 43 int main(int argc, char *argv[]){ 44 int count, numb; 45 int i; 46 int *nums; 47 48 if( argc < 2){ 49 printf("too few argument\n"); 50 // free(nums); 51 return 1; 52 exit(1); 53 } 54 55 printf("input number is %s\n",argv[1]); 56 count = atoi(argv[1]); 57 nums = (int *)malloc(sizeof(int)*count); 58 printf("please input:\n"); 59 for(i = 0; i < count; i++){ 60 scanf("%d",nums+i); 61 } 62 63 shellsort(nums,count); 64 65 printf("output is:\n"); 66 for(i = 0; i < count; i++){ 67 if(i%10 == 0) 68 printf("\n"); 69 printf("%d\t",*(nums+i)); 70 if(i == count -1) 71 printf("\n"); 72 } 73 74 }