Created
June 23, 2022 16:55
-
-
Save DeclanGas/9cf64f9c382ab55dda75fbadbd2a75af 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 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