-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionSort.cpp
63 lines (55 loc) · 978 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
void input(int arr[], int &size)
{
cin>> size;
for(int i=0;i<size;i++)
cin>> arr[i];
}
void swap (int &a, int &b)
{
int t=a;
a=b;
b=t;
}
void insertionSort(int arr[],int size)
{
int i, j, key;
for( i=0; i<size;i++)
{
key=arr[i];
j=i-1;
while(j>=0 && arr[j]>key)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
void insertionSort2(int arr[],int size)
{
int j;
for(int i=0; i<size; i++)
{
j=i;
while(j>0 && arr[j]<arr[j-1])
{
swap(arr[j],arr[j-1]);
j--;
}
}
}
void output(int arr[], int size)
{
insertionSort(arr, size);
for(int i=0; i<size;i++)
cout<<arr[i]<<" ";
}
int main()
{
int arr[100], size;
input(arr, size);
output(arr, size);
return 0;
}