-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.cpp
76 lines (58 loc) · 1.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Insertion Sort
Input - numbers in random order
Output - numbers sorted in ascnding or dscending order
Time Complexity - O(n^2)
*/
#include<iostream>
using namespace std;
int main()
{
int numbers[100];
int n;
int i,j,temp,key;
cout<<"\n Insertion Sort \n";
cout<<"\n Enter total number of numbers : ";
cin>>n;
cout<<"\n Enter Numbers : ";
for(int i=0;i<n;i++)
{
cin>>numbers[i];
}
cout<<"\n Your Numbers : ";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i]<<" ";
}
for(i=1;i<n;i++)
{
key=numbers[i];
j=i-1;
while(j>=0 && key<numbers[j])
{
numbers[j+1]=numbers[j];
j--;
}
numbers[j+1]=key;
}
cout<<"\n Your numbers in ascending order : ";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i]<<" ";
}
cout<<"\n";
return 0;
}
/* OUTPUT
(base) mansi@mansi-Vostro-15-3568:~$ c++ insertion_sort.cpp
(base) mansi@mansi-Vostro-15-3568:~$ ./a.out
Insertion Sort
Enter total number of numbers : 5
Enter Numbers : 9
3
6
5
7
Your Numbers : 9 3 6 5 7
Your numbers in ascending order : 3 5 6 7 9
(base) mansi@mansi-Vostro-15-3568:~$ ^C
*/