implements selection sort

This commit is contained in:
2023-08-03 22:38:27 -07:00
parent 5564d94684
commit 09e18f9ac9
5 changed files with 98 additions and 7 deletions

45
c/bubble-sort-take-2.c Normal file
View File

@@ -0,0 +1,45 @@
#include <stdio.h>
/*
in bubble sort we iterate through the array each time we encounter adjacent elements
out of order, we swap them. We continue this iteration until we have reached
*/
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort(int arr[], int len) {
int swap_count = 1;
int temp;
int end_index = len - 1;
while (swap_count > 0) {
swap_count = 0;
for (int i = 0; i < end_index; i++) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swap_count++;
}
}
end_index--;
}
}
void print_array(int arr[], int len) {
printf("[ ");
for (int i = 0; i < len; i++) {
printf(" %d", arr[i]);
}
printf(" ]\n");
}
int main() {
int input[5] = {5, 4, 3, 2, 1};
bubble_sort(input, 5);
print_array(input, 5);
}