插入排序 insertion sort

https://github.com/hotwater99/practice_datastructure_and_algorithm.git

《数据结构与算法分析——C语言描述》机械工业出版社,原书第2版,7.2节


插入排序的时间复杂度为O(N2),经过排序后数据从小到大排列。

生成一个随机排列的测试数组test_array用于测试。

1、从test_array[1]开始往后处理;

2、假设正在处理test_array[k],此时test_array[0]~test_array[k-1]已经从小到大排好序;

3、使用临时变量tmp来实现一个“前滤”的策略,可以避免交换操作;

4、将test_array[k]的指保存在tmp中;

5、从test_array[k]开始往前比较(k-1, …, 0),如果test_array[m]>tmp,则将test_array[m]的值放到test_array[m+1];否则将tmp插入到test_array[m+1]

GIF 2020-4-28 11-12-33[5]

插入排序:

  1 void InsertSort(int array[], int n)
  2 {
  3   int i, j;
  4   int tmp;
  5 
  6   for (i = 1; i < n; i++)
  7   {
  8     tmp = array[i];
  9 
 10     for (j = i; j > 0 && array[j - 1] > tmp; j--) {
 11       array[j] = array[j - 1];
 12     }
 13     array[j] = tmp;
 14   }
 15 }


完整代码:

  1 #include <iostream>
  2 #include <ctime>
  3 
  4 using namespace std;
  5 
  6 #define random(x)       (rand()%x)
  7 #define ARRAY_LENTH     10
  8 
  9 void InsertSort(int array[], int n)
 10 {
 11   int i, j;
 12   int tmp;
 13 
 14   for (i = 1; i < n; i++)
 15   {
 16     tmp = array[i];
 17 
 18     for (j = i; j > 0 && array[j - 1] > tmp; j--) {
 19       array[j] = array[j - 1];
 20     }
 21     array[j] = tmp;
 22   }
 23 }
 24 
 25 int main() {
 26   int test_array[ARRAY_LENTH];
 27   int i, N = ARRAY_LENTH;
 28   clock_t start_time, stop_time;
 29 
 30   for (i = 0; i < N; i++) {
 31     test_array[i] = random(N);
 32   }
 33 
 34   cout << "raw : ";
 35   for (i = 0; i < N; i++) {
 36     cout << test_array[i] << " ";
 37   }
 38   cout << endl;
 39 
 40   start_time = clock();
 41 
 42   InsertSort(test_array, N);
 43 
 44   stop_time = clock();
 45 
 46   cout << "sort: " ;
 47   for (i = 0; i < N; i++) {
 48     cout << test_array[i] << " ";
 49   }
 50   cout << endl;
 51 
 52   cout << "InsertSort(" << N << ")..." << endl;
 53   cout << "total time used: ";
 54   cout << (double)(stop_time - start_time) / CLOCKS_PER_SEC << "s" << endl;
 55 
 56   system("pause");
 57 
 58   return 0;
 59 }


测试结果


N = 10

raw : 1 7 4 0 9 4 8 8 2 4
sort: 0 1 2 4 4 4 7 8 8 9
InsertSort(10)...
total time used: 0s

N = 100

raw : 41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 91 4 2 53 92 82 21 16 18 95 47 26 71 38 69 12 67 99 35 94 3 11 22 33 73 64 41 11 53 68 47 44 62 57 37 59 23 41 29 78 16 35 90 42 88 6 40 42 64 48 46 5 90 29 70 50 6 1 93 48 29 23 84 54 56 40 66 76 31 8 44 39 26 23 37 38 18 82 29 41
sort: 0 1 2 3 4 5 5 6 6 8 11 11 12 16 16 18 18 21 22 23 23 23 24 26 26 27 27 29 29 29 29 31 33 34 35 35 36 37 37 38 38 39 40 40 41 41 41 41 42 42 42 44 44 45 46 47 47 48 48 50 53 53 54 56 57 58 59 61 62 62 64 64 64 66 67 67 68 69 69 70 71 73 76 78 78 81 82 82 84 88 90 90 91 91 92 93 94 95 95 99
InsertSort(100)...
total time used: 0s

N = 1000

InsertSort(1000)...
total time used: 0.001s

N = 10000

InsertSort(10000)...
total time used: 0.056s

N = 100000

  1 InsertSort(100000)...
  2 total time used: 5.542s
原文地址:https://www.cnblogs.com/hotwater99/p/12784985.html