Skip to content

Instantly share code, notes, and snippets.

@YeyoM
Created February 22, 2023 15:31
Show Gist options
  • Save YeyoM/6bfed4ddaee06f1246d8c3a096d36d3e to your computer and use it in GitHub Desktop.
Save YeyoM/6bfed4ddaee06f1246d8c3a096d36d3e to your computer and use it in GitHub Desktop.
Direct Insertion implemented on cpp
// Diego Emilio Moreno Sanchez
#include <iostream>
using namespace std;
void directInsertion(int arr[], int n) {
int i, j, aux;
for (i = 1; i < n; i++) {
j = i;
aux = arr[i];
while (j > 0 && arr[j - 1] > aux) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = aux;
}
}
int main() {
int arr[] = {5, 4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
directInsertion(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment