【算法】希尔排序,快速排序,选择排序,插入排序,冒泡排序

头文件

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 #ifndef _MYINCLUDE_H
 7 #define _MYINCLUDE_H
 8 
 9 #include <stdio.h>
10 #include <stdbool.h>
11 
12 extern void bubble_sort(int n,int test[]);//冒泡排序
13 
14 extern void select_sort(int n,int number[]);//选择排序
15 
16 extern void swap(int number[], int i, int j);//用于交换数组中的索引为i,j的数
17 
18 extern int quick_pass(int *array, int low, int high);//得到基准值应该存放的位置
19 extern void quick_sort(int *array, int low, int high);//快速排序
20 
21 extern void insert_sort(int len, int *array);//插入排序
22 
23 extern void shell_sort(int len, int *array);//希尔排序
24 
25 #endif
View Code

主文件

 1 // 实现数组的排序
 2 
 3 // 冒泡排序,选择排序,快速排序,插入排序,希尔排序
 4 // @author:成鹏致远
 5 // #net:infodown.tap.cn
 6 
 7 #include "myinclude.h"
 8 
 9 #define LEN 6
10 
11 int main()
12 {
13     int test[LEN] = {4,3,5,9,11,8};
14     int i;
15 
16     //bubble_sort(LEN,test);//冒泡排序
17     //select_sort(LEN,test);//选择排序
18     //quick_sort(test,0,LEN-1);//快速排序
19     //insert_sort(LEN,test);//插入排序
20     shell_sort(LEN,test);//希尔排序
21 
22     for(i=0; i<LEN; i++)
23     {
24         printf("%d\t",test[i]);
25     }
26     printf("\n");
27 
28     return 0;
29 }
View Code

 希尔排序

 1 /*
 2 * @function: 希尔排序
 3 * @author: 成鹏致远
 4 * @net: infodown.tap.cn
 5 */
 6 //希尔排序
 7 //希尔排序为非稳定排序算法
 8 //希尔排序减少了元素的移动的次数
 9 
10 #include "myinclude.h"
11 
12 void shell_sort(int len, int *array)//希尔排序
13 {
14     int d;//d个元素分为一组进行排序
15     int i;//i为组内未排序部分
16     int j;//j为组内排好序的部分
17     int tmp;
18 
19     for(d = len/2; d >=1 ; d /= 2)
20     {
21         for(i = d;i < len; i++)//内两层循环实质是一个插入排序,分组减少了元素移动的次数
22         {
23             tmp = array[i];
24             /*寻找插入的位置*/
25             for(j = i-d;(tmp < array[j]) && (j>=0);j -= d)//i-d即为组内排好序的最后一个元素
26                 array[j+d] = array[j];
27             array[j+d] = tmp;
28         }
29 
30     }
31 }
View Code

 快速排序

 1 /*
 2 * @function: 快速排序
 3 * @author: 成鹏致远
 4 * @net: infodown.tap.cn
 5 */
 6 
 7 
 8 #include "myinclude.h"
 9 
10 void quick_sort(int *array, int low, int high)//快速排序
11 {
12     int mid;//存储基准值
13 
14     if(low < high)
15     {
16         mid = quick_pass(array,low,high);//得到基准值应该存放的位置
17         quick_sort(array,low,mid-1);//对左边的数组递归进行快速排序
18         quick_sort(array,mid+1,high);//对左边的数组递归进行快速排序
19     }
20 }
21 
22 
23 
24 int quick_pass(int *array, int low, int high)//得到基准值应该存放的位置
25 {
26     int tmp = array[low];//tmp保存基准值,此时array[low]位置可认为空
27 
28     while(low < high)
29     {
30         while(low < high && tmp < array[high])//仍需要low<high条件是因为low和high在变化
31             high--;//从后往前找,直到找到比基准值小的,下标为high
32         if(low < high)
33             array[low] = array[high];//将比基准值小的放到空位置,此时array[high]位置可认为空
34         while(low < high && tmp > array[low])//再从前往后找,直到找到比基准值大的,下标为low
35             low++;
36         if(low < high)
37         {
38             array[high] = array[low];//此时array[low]可认为空
39         }
40     }
41     //此时array[low]为空,此位置正好是基准值的位置
42     array[low] = tmp;
43 
44     return low;//返回基准值的位置
45 }
View Code

选择排序 

 1 /*
 2 * @function: 选择排序
 3 * @author: 成鹏致远
 4 * @net: infodown.tap.cn
 5 */
 6 
 7 #include "myinclude.h"
 8 
 9 void select_sort(int n,int number[])
10 {
11     int i,j;
12 
13     for(i=0; i<n; i++)
14     {
15         int m =i;//每次确定一个最小数
16         for (j=i+1; j<n; j++)
17         {
18             if(number[j] <number[m])
19             {
20                 m =j;
21             }
22         }
23         if (i != m)
24         {
25             swap(number,i,m);
26         }
27     }
28 }
29 
30 void swap(int number[], int i, int j)
31 {
32     int t;
33     t = number[i];
34     number[i] = number[j];
35     number[j] = t;
36 }
View Code

插入排序 

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 //插入排序
 7 
 8 void insert_sort(int len, int *array)//插入排序
 9 {
10     int i,j;
11     int tmp;//保存待排序的值
12 
13     for(i=1; i<len; i++)//第1个数为已经排好序的序列,从第2个数开始比较,依次插入已经排好序的序列
14     {
15         tmp = array[i];//待排序的值
16         for(j=i-1; (tmp<array[j]) && (j>=0); j--)//j指向已经排好序的最后一个元素,依次和tmp比较,如果tmp比已经排好序中的值小,则依次向后移动
17             array[j+1] = array[j];
18         //此时j+1的位置即为tmp的位置
19         array[j+1] = tmp;
20     }
21 }
View Code

 冒泡排序

 1 /*
 2 * @function: 冒泡排序
 3 * @author: 成鹏致远
 4 * @net: infodown.tap.cn
 5 */
 6 
 7 #include "myinclude.h"
 8 
 9 void bubble_sort(int n,int before[])
10 {
11     int i,j,temp;
12     for(i=0; i<n; i++)
13     {
14         for(j=0; j<n-i-1; j++)
15         {
16             if(before[j] > before[j+1])
17             {
18                 temp = before[j+1];
19                 before[j+1] = before[j];
20                 before[j] = temp;
21             }
22         }
23     }
24 
25 }
View Code

  

你可能感兴趣的:(冒泡排序)