Skip to content

Instantly share code, notes, and snippets.

@DeclanGas
Created June 23, 2022 16:55
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/9cf64f9c382ab55dda75fbadbd2a75af to your computer and use it in GitHub Desktop.
Save DeclanGas/9cf64f9c382ab55dda75fbadbd2a75af to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
// function prototypes
void bubble_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
bubble_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");
}
// bubble sort
void bubble_sort(int arr[], int size)
{
int temp = 0;
for (int i = 0; i < size - 1; i++)
{
for (int k = 0; k < size - 1; k++)
{
if (arr[k] > arr[k + 1])
{
temp = arr[k];
arr[k] = arr[k + 1];
arr[k + 1] = temp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment