常用排序算法(插入排序)

插入排序使用的是增量(incremental)方法;在排好子数组A[1..j-1]后,将A[j]插入,形成排好序的子数组A[1..j]。
更多信息请参考:http://baike.baidu.com/view/396887.htm

C语言代码:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <memory.h>
 4 #include <time.h>
 5 
 6 void init(int *array, int count)
 7 {
 8     int n = 0;
 9     srand((unsigned int)time(NULL));
10 
11     for (n=0; n<count; n++)
12     {
13         array[n] = rand()%100 + 1;
14     }
15 }
16 
17 void output(int *array, int count)
18 {
19     int n = 0;
20     for (n=0; n<count; n++)
21     {
22         printf("%5d", array[n]);
23     }
24 
25     printf("\n");
26 }
27 
28 void insertsort(int* array, int count, int type)   
29 {
30     int i = 0;
31     int j = 0;
32     int temp = 0;;  
33     if (type == 0)   
34     {   
35         //正排序,从小排到大  
36         //比较的轮数  
37         for (i = 1; i<count; i++)   
38         {  
39             //保证前i个数排好序  
40             temp=array[i];  
41             j = i-1;  
42             while(j>=0&&array[j]>temp)  
43             {  
44                 array[j+1]=array[j];  
45                 j--;  
46             }  
47 
48             array[j+1]=temp;  
49         }  
50     }  
51     else
52     {   
53         //倒排序,从大排到小  
54         //比较的轮数  
55         for (i = 1; i<count; i++)   
56         {  
57             //保证前i个数排好序  
58             temp=array[i];  
59             j = i-1;  
60             while(j>=0&&array[j]<temp)  
61             {  
62                 array[j+1]=array[j];  
63                 j--;  
64             }  
65 
66             array[j+1]=temp;  
67         }  
68     }
69 }
70 
71 int main()
72 {
73     const int count = 10;
74     int array[count];
75 
76     memset(array, 0, sizeof(int)*count);
77 
78     init(array, count);
79     
80     printf("data before sort:      ");
81     output(array, count);
82 
83     insertsort(array, count, 0);  // 正排序,从小排到大
84     printf("data after sort(asc):  ");
85     output(array, count);
86 
87     insertsort(array, count, 1);  // 倒排序,从大排到小
88     printf("data after sort(desc): ");
89     output(array, count);
90     
91     return 0;
92 }

运行结果如下:
data before sort:         71   68   58   93   32   80  100   73   10   18
data after sort(asc):     10   18   32   58   68   71   73   80   93  100
data after sort(desc):   100   93   80   73   71   68   58   32   18   10


Java代码:

  1 import java.util.Random;  
  2   
  3 /** 
  4 * 插入排序 
  5 * 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。      
  6 * 性能:比较次数O(n^2),n^2/4 
  7 * 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。 
  8 */  
  9   
 10 public class Sort   
 11 {  
 12     /* 
 13      * 输出数组中的数据 
 14      */  
 15     public static void OutputArray(int[] array)   
 16     {  
 17         for (int data : array)   
 18         {  
 19             System.out.print(data + "\t");  
 20         }  
 21           
 22         System.out.println();  
 23     }  
 24       
 25     /* 
 26      * 生成需要排序的数组 
 27      */  
 28     public static int[] createArray(int count)   
 29     {  
 30         int array[] = new int[count];  
 31         Random r = new Random();  
 32           
 33         for (int i = 0; i < count; i++)   
 34         {  
 35             array[i] = r.nextInt(100);  
 36         }  
 37           
 38         System.out.println("");  
 39         System.out.print("data before sort:\t");  
 40           
 41         OutputArray(array);  
 42           
 43         return array;  
 44     }  
 45       
 46     /** 
 47     * 插入排序 
 48     */  
 49     public static void insertSort(int[] array, String sortType)   
 50     {  
 51         int insertValue;  
 52         if (sortType.equals("asc"))   
 53         {   //正排序,从小排到大  
 54             //比较的轮数  
 55             for (int i = 1; i <array.length; i++)   
 56             {  
 57                 //保证前i个数排好序  
 58                 insertValue=array[i];  
 59                 int index=i-1;  
 60                 while(index>=0&&array[index]>insertValue)  
 61                 {  
 62                     array[index+1]=array[index];  
 63                     index--;  
 64                 }  
 65                   
 66                 array[index+1]=insertValue;  
 67             }  
 68         }  
 69         else if (sortType.equals("desc"))   
 70         {   //倒排序,从大排到小  
 71             //比较的轮数  
 72             for (int i = 1; i <array.length; i++)   
 73             {  
 74                 //保证前i个数排好序  
 75                 insertValue=array[i];  
 76                 int index=i-1;  
 77                 while(index>=0&&array[index]<insertValue)  
 78                 {  
 79                     array[index+1]=array[index];  
 80                     index--;  
 81                 }  
 82                   
 83                 array[index+1]=insertValue;  
 84             }  
 85         }   
 86         else  
 87         {  
 88             System.out.println("您输入的排序类型错误!");  
 89         }  
 90     }  
 91       
 92     public static void main(String[] args)   
 93     {  
 94         int[] arr1=createArray(10);  
 95           
 96         System.out.print("data after sort(asc):\t");  
 97         insertSort(arr1, "asc");  
 98         OutputArray(arr1);  
 99           
100         System.out.print("data after sort(desc):\t");  
101         insertSort(arr1, "desc");  
102         OutputArray(arr1);  
103     }  
104 }  


运行结果如下:
data before sort:    33    99    96    91    85    97    85    82    58    86    
data after sort(asc):    33    58    82    85    85    86    91    96    97    99    
data after sort(desc):    99    97    96    91    86    85    85    82    58    33   

转载于:https://www.cnblogs.com/yuanping/archive/2012/12/24/2831632.html

猜你喜欢

转载自blog.csdn.net/weixin_33728268/article/details/94067234