Created
March 23, 2022 04:51
-
-
Save shivajichalise/1f56ea42a2577b5551c9fe8cf81fdb74 to your computer and use it in GitHub Desktop.
Insertion Sort implementation in C++
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
#include <iostream> | |
using namespace std; | |
#define MAX 10 | |
void insertionSort(int arr[]) { | |
int key, j; | |
for (int i = 1; i < MAX; i++) { | |
key = arr[i]; | |
j = i - 1; | |
while (j >= 0 && arr[j] > key) { | |
arr[j + 1] = arr[j]; | |
j = j - 1; | |
} | |
arr[j + 1] = key; | |
} | |
} | |
int main() { | |
int arr[MAX] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; | |
insertionSort(arr); | |
for (int i = 0; i < MAX; i++) { | |
cout << arr[i] << " "; | |
} | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment