Skip to content

Instantly share code, notes, and snippets.

@DeclanGas
Created June 23, 2022 16:57
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 DeclanGas/198f2c872087273accdb1439a5b18e45 to your computer and use it in GitHub Desktop.
Save DeclanGas/198f2c872087273accdb1439a5b18e45 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
// function prototypes
void insertion_sort(int arr[], int n);
void print_array(int arr[], int n);
// size of array
#define SIZE 10
int main(void)
{
// initialize array
int arr[] = {1, 8, 4, 6, 0, 3, 5, 2, 7, 9};
// sort array
insertion_sort(arr, SIZE);
// print out the array
print_array(arr, SIZE);
// done
return 0;
}
// function to print an array
void print_array(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%i ", arr[i]);
}
printf("\n");
}
// insertion sort
void insertion_sort(int arr[], int n)
{
for (int i = 1; i < SIZE - 1; i++)
{
int element = arr[i];
int j = i;
while (j > 0 && arr[j - 1] > element)
{
arr[j] = arr[j - 1];
j = j - 1;
}
arr[j] = element;
}
// TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment