Skip to content

Instantly share code, notes, and snippets.

@shivajichalise
Created March 23, 2022 04:51
Show Gist options
  • Save shivajichalise/1f56ea42a2577b5551c9fe8cf81fdb74 to your computer and use it in GitHub Desktop.
Save shivajichalise/1f56ea42a2577b5551c9fe8cf81fdb74 to your computer and use it in GitHub Desktop.
Insertion Sort implementation in C++
#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