Created
June 23, 2022 16:57
-
-
Save DeclanGas/198f2c872087273accdb1439a5b18e45 to your computer and use it in GitHub Desktop.
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 <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