forked from vikasgond807/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vikasgond807#11 from rajpatil6355/patch-1
insertion sort in cpp
- Loading branch information
Showing
1 changed file
with
37 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,37 @@ | ||
//Insertion Sort Algorithm | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
void insertionSort(int a[], int n) | ||
{ | ||
int i, o, j; | ||
for (i = 1; i < n; i++) | ||
{ | ||
o = a[i]; | ||
j = i - 1; | ||
while (j >= 0 && a[j] > o) | ||
{ | ||
a[j + 1] = a[j]; | ||
j = j - 1; | ||
} | ||
a[j + 1] = o; | ||
} | ||
} | ||
void printArray(int a[], int n) | ||
{ | ||
int i; | ||
for (i = 0; i < n; i++) | ||
cout << a[i] << " "; | ||
cout << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 5,23,10,2,56,4 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
insertionSort(arr, n); | ||
printArray(arr, n); | ||
return 0; | ||
} | ||
// This is code is contributed by raj patil |