-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5da339c
commit 3b9c16b
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |