Skip to content

Instantly share code, notes, and snippets.

@DeclanGas
Created June 23, 2022 16:56
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/94d1b21dd9e70d1571a67a0b59390e13 to your computer and use it in GitHub Desktop.
Save DeclanGas/94d1b21dd9e70d1571a67a0b59390e13 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
// function prototypes
void selection_sort(int arr[], int size);
void print_array(int arr[], int size);
// 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
selection_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 size)
{
for (int i = 0; i < size; i++)
{
printf("%i ", arr[i]);
}
printf("\n");
}
// selection sort
void selection_sort(int arr[], int size)
{
int temp = 0;
int min = 0;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
temp = arr[j];
arr[j] = min;
min = temp;
}
arr[i] = min;
}
}
// TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment