-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion sort .cpp
48 lines (46 loc) · 1015 Bytes
/
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
#include <iostream>
using namespace std;
void INSERTIONSORT (int arr[],int size){
int i,temp, j;
for (i = 1; i < size; i++) {
temp = arr[i];
j = i - 1;
for (; j >= 0; j--){
if(arr[j]>temp){
arr[j + 1] = arr[j];
}
else{
break;
}
}
arr[j + 1] = temp;
}
/* for (int i = 1; i < n; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
} */
}
int main()
{
int size_of_array;
cout << "enter size of array" << endl;
cin >> size_of_array; // 5 4 2 3 1
int arr[size_of_array];
cout << "enter your element" << endl;
for (size_t i = 0; i < size_of_array; i++)
{
cin >> arr[i];
}
INSERTIONSORT(arr, size_of_array);
cout << "your sorted array is" << endl;
for (size_t i = 0; i < size_of_array; i++)
{
cout <<arr[i] << endl;
}
return 0;
}