Skip to content

Commit

Permalink
Added cpp code for insertion sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Mighty-Geek committed Oct 17, 2020
1 parent 5da339c commit 3b9c16b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Insertion Sort is a sorting technique in which we spit array into sorted and
// unsorted part and sorted ones from unsorted part are picked and placed at
// correct position

#include<bits/stdc++.h>
using namespace std;

void insertionSort(vector<int>nums){
int n = nums.size();
for(int i=0; i<n; i++){
int key;
key = nums[i];
int j = i-1;
while(j >= 0 && nums[j] > key){
nums[j+1] = nums[j];
j = j-1;
}
nums[j+1] = key;
}
for(int i=0; i<n; i++)
cout << nums[i] << " ";
}
int main(){
vector<int>arr;
int n;
cin >> n;
for(int i=0; i<n; i++){
int data;
cin >> data;
arr.push_back(data);
}
insertionSort(arr);
return 0;
}

0 comments on commit 3b9c16b

Please sign in to comment.