forked from reerajput930/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.cpp
59 lines (48 loc) · 1.3 KB
/
insertion_sort.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
51
52
53
54
55
56
57
58
/*
Insertion sort is a simple and efficient comparison sort. In this algorithm, each iteration removes
an element from the input data and inserts it into the correct position in the list being sorted. The
choice of the element being removed from the input is random and this process is repeated until
all input elements have gone through.
* Algorithm :
Every repetition of insertion sort removes an element from the input data, and inserts it into the
correct position in the already-sorted list until no input elements remain. Sorting is typically done
in-place. The resulting array after k iterations has the property where the first k + 1 entries are
sorted.Each element greater than x is copied to the right as it is compared against x.
*/
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int i = 1;i<n ;i++ ){
for(int j = i; j>0 ; j-- ){
if(a[j]<a[j-1]){
swap(a[j],a[j-1]);
}
else{
break;
}
}
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
/*
Test case :
Input : 5
4 3 5 1 2
Output :
1 2 3 4 5
Time Complexity :
Worst case complexity: Θ(n^2 )
Best case complexity: Θ(n)
Average case complexity: Θ(n^2 )
Worst case space complexity: O(n^2 ) total, O(1) auxiliary
*/