-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertSort.cpp
50 lines (44 loc) · 954 Bytes
/
insertSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//最差和平均时间复杂度O(n^2) -> O(n)最好 稳定
void insertSort(int arr[], int size)
{
for(int i = 1; i < size; ++i)
{
int val = arr[i];
int j = i - 1;
for(; j >=0 && arr[j] > val; --j)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = val;
}
}
int main()
{
int arr[10];
srand(time(0));
for(int i = 0; i < 10; ++i)
{
arr[i] = rand() % 100 + 1;
}
int size = sizeof(arr) / sizeof(arr[0]);
for(auto v : arr)
{
cout << v << " ";
}
cout << endl;
clock_t startTime, endTime;
startTime = clock();
insertSort(arr, size);
endTime = clock();
cout << "The time of sort is: " << (endTime - startTime) * 1.0 / CLOCKS_PER_SEC << "s" << endl;
for(auto v : arr)
{
cout << v << " ";
}
cout << endl;
return 0;
}