-
-
Save gene-ressler/2b462947eb99fdc70f53a51b96e6cc2d to your computer and use it in GitHub Desktop.
Straight insertion sort
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 <stdio.h> | |
#include <stdlib.h> | |
void sort(int *a, int n) { | |
for (int i = 1; i < n; ++i) { | |
int j, t = a[i]; | |
for (j = i; j > 0 && a[j - 1] > t; --j) a[j] = a[j - 1]; | |
a[j] = t; | |
} | |
} | |
int main(void) { | |
int n = 30, a[n]; | |
for (int i = 0; i < n; ++i) a[i] = rand(); | |
sort(a, n); | |
for (int i = 0; i < n; ++i) printf("%d\n", a[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment