Implement Bubble Sort

This commit is contained in:
baz 2024-01-29 16:42:31 +00:00
parent 4252fb6918
commit c7232dc22b
2 changed files with 50 additions and 1 deletions

49
bubblesort/main.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#define ARRAYLENGTH 100
void generateRandomArray(int *array)
{
for (size_t i = 0; i < ARRAYLENGTH; i++)
{
array[i] = rand();
}
}
void printArrayContent(int *array)
{
for (size_t i = 0; i < ARRAYLENGTH; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
void sortArray(int *array)
{
for (size_t i = 0; i < ARRAYLENGTH - 1; i++ )
{
for (size_t j = 0; j < ARRAYLENGTH - 1 - i; j++)
{
if (array[j] > array[j+1])
{
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
}
int main(int argc, char *argv[])
{
int array[ARRAYLENGTH];
generateRandomArray(array);
sortArray(array);
printArrayContent(array);
return 1;
}

View File

@ -11,4 +11,4 @@ For my own personal revision, all implementations were initially attemped using
### Algorithms
- TBA
- [Bubble Sort](bubblesort/main.c)