-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP68SORTING.cpp
More file actions
38 lines (30 loc) · 935 Bytes
/
Copy pathP68SORTING.cpp
File metadata and controls
38 lines (30 loc) · 935 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
// INSERTION SORT
// Take an element from the unsorted array, place it in its corresponding position in the sorted part,and shift the elements accordingly.
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter size of data";
cin>>n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
for(int i=1; i<n ; i++){ // in this sort we are not starting with 0th element but its next element which is at 1st position
int current = arr[i];
int j=i-1;
while(arr[j] > current && j>=0)
{
arr[j+1]= arr[j]; // we r moving 1 position ahead
j--; // then we move to its before element
}
arr[j+1]=current;
}
for (int i = 0; i < n; i++) // this loop is for printing array
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}