InsertSort
Posted purzel
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了InsertSort相关的知识,希望对你有一定的参考价值。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 #define MAXSIZE 200000 5 typedef int KeyType; 6 typedef struct { 7 KeyType key; 8 }RedType; 9 typedef struct { 10 RedType r[MAXSIZE + 1]; 11 int length; 12 }SqList; 13 int Random(int start, int end){ 14 int dis = end - start; 15 return rand() % dis + start; 16 } 17 void InsertSort(SqList &L) { 18 double start_time, finish_time, cord_time; 19 start_time = clock(); 20 int i, j; 21 for (i = 2; i <= L.length; ++i) { 22 if (L.r[i].key < L.r[i-1].key) { 23 L.r[0] = L.r[i]; 24 L.r[i] = L.r[i-1]; 25 for (j = i-2; L.r[0].key < L.r[j].key; --j) { 26 L.r[j+1] = L.r[j]; 27 } 28 L.r[j+1] = L.r[0]; 29 } 30 } 31 finish_time = clock(); 32 cord_time = (double)(finish_time - start_time) ; 33 printf("InsertSort time=%f ms ", cord_time); 34 } 35 void InPut(SqList &L) { 36 int i; 37 srand((unsigned)time(NULL)); 38 cin >> L.length; 39 for (i = 1; i <= L.length; ++i) { 40 // cin >> L.r[i].key; 41 L.r[i].key = Random(1, 1000000); 42 } 43 } 44 void OutPut(SqList &L) { 45 int i; 46 for (i = 1; i <= L.length; ++i) { 47 cout << L.r[i].key << " "; 48 } 49 } 50 int main() { 51 SqList L; 52 // L.r = new RedType [MAXSIZE+1]; 53 InPut(L); 54 InsertSort(L); 55 OutPut(L); 56 return 0; 57 }
以上是关于InsertSort的主要内容,如果未能解决你的问题,请参考以下文章