-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert_Sort.cpp
More file actions
37 lines (30 loc) · 751 Bytes
/
Insert_Sort.cpp
File metadata and controls
37 lines (30 loc) · 751 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
#include <iostream>
void Insertion_Sort(int array[]);
int main()
{
int array[10]{1,5,7,8,9,0,4,10,5,0};
Insertion_Sort(array);
//printing the array
for (auto x:array)
std::cout<<x<<std::endl;
}
void Insertion_Sort(int array[])
{
// this loop travere the array
for (int i = 1;i<10;i++)
{
int j = i-1;
int key = array[i];
//checking if the previous element is larger
//and if we haven't crossed the boundray
while(array[j]> key && j >=0)
{
//move each element's index by one
array[j+1] = array[j];
--j;
}
//when the condiiton is out or we no longer have
//an element that is bigger than our key; swap.
array[j+1] = key;
}
}