-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionsort.cpp
41 lines (37 loc) · 908 Bytes
/
Insertionsort.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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
using namespace std;
void insertionSort(int arr[], int length)
{
int i, j, tmp;
for (i = 1; i < length; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}
int displayArray( int arr[], int length )
{
cout<<"{";
for( int i=0; i<length; i++ )
cout<<" "<<arr[i];
cout<<"}\n";
}
int main( int argc, char* argv[] )
{
int array[10] = { 2,1,7,4,3,5,9,6,8,0 };
size_t length = sizeof(array)/sizeof(int);
cout<<" Array Before Sorting \n";
displayArray( array, length );
insertionSort( array, length );
cout<<" Array After Sorting \n";
displayArray( array, length );
}