forked from zatch3301/RTU-DigitalLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
37 lines (35 loc) · 730 Bytes
/
InsertionSort.cpp
File metadata and controls
37 lines (35 loc) · 730 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>
using namespace std;
//Making a function to Sort the given array using Insertion Sort
void InsertionSort(int arr[],int n){
for(int i=1;i<n;i++){
int current=arr[i];
int j;
for(j=i-1;j>=0;j--){
if(current<arr[j]){
arr[j+1]=arr[j];
}
else{
break;
}
}
arr[j+1]=current;
}
}
int main() {
int n;
//Taking size as input from user
cin>>n;
//Initialising the array
int input[100];
//Taking input from the user
for(int i=0;i<n;i++){
cin>>input[i];
}
InsertionSort(input,n);
//Printing out the sorted Array
for(int i=0;i<n;i++){
cout<<input[i]<<" ";
}
cout<<endl;
}