-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
41 lines (33 loc) · 839 Bytes
/
InsertionSort.cpp
File metadata and controls
41 lines (33 loc) · 839 Bytes
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
/*
Insertion sort in C++: C++ program for insertion sort to sort numbers.
This code implements insertion sort algorithm to arrange numbers of an array in ascending order.
With a little modification it will arrange numbers in descending order.
*/
#include<iostream.h>
#include <conio.h>
int main()
{
int n, array[1000], c, d, t;
cout<<"Enter number of elements\n";
cin>>n;
cout<<"Enter Elements";
for (c = 0; c < n; c++) {
cin>>array[c];
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
cout<<"Sorted list in ascending order:\n";
for (c = 0; c <= n - 1; c++) {
cout<<array[c]<<endl;
}
getch();
return 0;
}