Skip to content

Instantly share code, notes, and snippets.

@gene-ressler
Last active October 1, 2022 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gene-ressler/2b462947eb99fdc70f53a51b96e6cc2d to your computer and use it in GitHub Desktop.
Save gene-ressler/2b462947eb99fdc70f53a51b96e6cc2d to your computer and use it in GitHub Desktop.
Straight insertion sort
#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