-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
56 lines (42 loc) · 1.37 KB
/
InsertionSort.cpp
File metadata and controls
56 lines (42 loc) · 1.37 KB
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
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
// Insertion sort.
/*
Time Complexity: O(n^2);
Stability: True; // as we implemented it when we used arr[j] <= temp for not shifting the first number.
Adaptive = True; // as when array is sorted its time complexity is O(n) i.e less than actual time complexity, means it is making use of the fact that array is sorted if it is sorted.
>> comparison with Bubble sort:
Bubble sort is better as if in bubble sort in one iteration we found that we do not swapped any element,
then this implies that the array is "now"(after that iteration) is sorted and we stop there, but there is no such convinience in insersion sort.
*/
void show(vector<int> arr){
for (int i=0;i<arr.size();i++){
cout << arr[i] << endl;
}
}
void insertionSort(vector<int>& arr){
for (int i=1;i<arr.size();i++){
for (int j=i-1;j>=0;j--){
int temp = arr[j+1];
if (arr[j] > temp){
arr[j+1] = arr[j];
arr[j] = temp;
}else if (arr[j] <= temp){
arr[j+1] = temp;
}
}
}
}
int main(){
int size;
cin >> size;
vector <int> arr(size);
for (int i=0;i<size;i++){
cin >> arr[i];
}
insertionSort(arr);
cout << "SHOWING SORTED ARRAY (BY INSERSION SORT): " << endl;
show(arr);
return 0;
}