Created
February 22, 2023 15:31
-
-
Save YeyoM/6bfed4ddaee06f1246d8c3a096d36d3e to your computer and use it in GitHub Desktop.
Direct Insertion implemented on cpp
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
// 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