-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.c
More file actions
42 lines (34 loc) · 1008 Bytes
/
bubble.c
File metadata and controls
42 lines (34 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "bubble.h"
#include "sorting_statistics.h"
#include <stdbool.h>
#include <stdint.h>
// Description:
// Uses bubble sort to sort an array.
//
// Parameters:
// uint32_t *arr - The array to sort.
// uint32_t len - The length of the array to sort.
//
// Returns:
// SortingStatistics - The statistics for the sort.
SortingStatistics bubble_sort( uint32_t *arr, uint32_t len ) {
SortingStatistics stats = sorting_statistics_create( len );
uint32_t pass_size = len;
bool swapped = true;
while ( swapped ) { // Continue until there is nothing else to check.
swapped = false;
for ( uint32_t i = 1; i < pass_size; i++ ) {
if ( arr[ i ] < arr[ i - 1 ] ) { // Compare to previous element.
// Swap arr[i] and arr[i - 1].
uint32_t old_arr_i = arr[ i ];
arr[ i ] = arr[ i - 1 ];
arr[ i - 1 ] = old_arr_i;
stats.moves += 3;
swapped = true;
}
stats.compares++;
}
pass_size -= 1; // Last element is sorted, so we can ignore it in the future.
}
return stats;
}