Skip to content

Instantly share code, notes, and snippets.

@ancientstraits
Last active February 27, 2022 14:52
Show Gist options
  • Save ancientstraits/f0687ccd1fc5f81bd074ad1640175678 to your computer and use it in GitHub Desktop.
Save ancientstraits/f0687ccd1fc5f81bd074ad1640175678 to your computer and use it in GitHub Desktop.
I ran the program 3 times.
1st run:
Bubble Sort No Temp: 1.147000 ms (4.779167 μs per 240 iterations)
Bubble Sort With Temp: 0.963000 ms (4.012500 μs per 240 iterations)
2nd run:
Bubble Sort No Temp: 1.134000 ms (4.685950 μs per 242 iterations)
Bubble Sort With Temp: 0.980000 ms (4.049587 μs per 242 iterations)
3rd run:
Bubble Sort No Temp: 1.165000 ms (4.814050 μs per 242 iterations)
Bubble Sort With Temp: 0.953000 ms (3.938016 μs per 242 iterations)
You can clearly see that while using a temp variable creates an extra
one, it is faster than doing some arithmetic to swap the variables' values.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 256
int is_sorted(int* arr, int size) {
for (int i = 1; i < size; i++) {
if (arr[i] < arr[i - 1])
return 0;
}
return 1;
}
int has_duplicates(int* arr, int size) {
for (int i = 0; i < size; i++)
for (int j = 0; j < i; j++)
if (arr[i] == arr[j])
return 1;
return 0;
}
int sort(void (*sorter_cb)(int* arr, int size), int print_iter) {
printf("Initializing list...\n");
srand(time(NULL));
int arr[SIZE];
for (int i = 0; i < SIZE; i++) {
arr[i] = rand();
}
if (has_duplicates(arr, SIZE)) {
printf("ERROR: has duplicates!\n");
exit(1);
}
printf("Starting sort...\n");
int iterations = 0;
while (!is_sorted(arr, SIZE)) {
sorter_cb(arr, SIZE);
iterations++;
if (print_iter)
printf("Iteration %d\n", iterations);
}
printf("Took %d iterations\n", iterations);
return iterations;
}
void swap_no_temp(int* a, int* b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
void bubble_sort_no_temp(int* arr, int size) {
for (int i = 1; i < size; i++)
if (arr[i] < arr[i - 1])
swap_no_temp(&arr[i], &arr[i - 1]);
}
void swap_temp(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort_temp(int* arr, int size) {
for (int i = 1; i < size; i++)
if (arr[i] < arr[i - 1])
swap_temp(&arr[i], &arr[i - 1]);
}
void willbe_sort(int* arr, int size) {
for (int i = 0; i < size; i++)
arr[i] = size;
}
int main() {
long start_no_temp = clock();
int iter_no_temp = sort(bubble_sort_no_temp, 0);
long end_no_temp = clock();
long start_temp = clock();
int iter_temp = sort(bubble_sort_temp, 0);
long end_temp = clock();
double no_temp_seconds = (float)(end_no_temp - start_no_temp) / CLOCKS_PER_SEC;
double temp_seconds = (float)(end_temp - start_temp) / CLOCKS_PER_SEC;
printf(
"Results:\n"
"Bubble Sort No Temp: %f ms (%f μs per iteration)\n"
"Bubble Sort With Temp: %f ms (%f μs per iteration)\n",
no_temp_seconds * 1000,
(no_temp_seconds / iter_no_temp) * 1e6,
temp_seconds * 1000,
(temp_seconds / iter_temp) * 1e6
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment