Created
May 31, 2021 13:08
-
-
Save smvd/f95bdf6f49f18744289a2825d1f987ac to your computer and use it in GitHub Desktop.
Sorting algorithms in c
This file contains hidden or 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
/* | |
The algorithims i have implemented are: | |
1. Bubble sort | |
2. Insertion sort | |
3. Selection sort | |
4. Comb sort | |
Youtube: www.rebrand.ly/eclip-coding | |
Github: gist.github.com/smvd/f95bdf6f49f18744289a2825d1f987ac | |
*/ | |
#include <stdio.h> // Header for standard IO | |
#include <stdlib.h> // Header for standard functions | |
#include <time.h> // Header for working with time | |
#include <string.h> // Header for working with strings | |
#include <unistd.h> // Header for POSIX | |
#define BLOCK_COUNT 25 // Count of blocks in the reference array | |
#define DISPLAY_BUFFER_SIZE 5000 // The maximum size of the display buffer | |
#define SLEEP 100 // The time waited between each cycle | |
#define SHRINK 1.3 // The ammount to shrink the comb (Comb Sort) | |
int blocks[BLOCK_COUNT]; // The array to be solved | |
void Swap(int *a, int s1, int s2) // Function to swap two items in an array | |
{ | |
int t = a[s1]; // Save the first item in a temp variable | |
a[s1] = a[s2]; // Set the first item to the second item | |
a[s2] = t; // Set the second item to the temp item | |
} | |
void GenerateBlocks(int *a, int n) // Function to generate an array of scrambled blocks | |
{ | |
srand(time(0)); // Set the seed so its less consistent | |
for (int i = 0; i < n; i++) // Loop over each item | |
{ | |
a[i] = i +1; // Set the starting value | |
} | |
if (n > 1) // If there is more than 1 item | |
{ | |
for (int i = 0; i < n; i++) // Loop over each item | |
{ | |
int j = i + rand() / (RAND_MAX / (n - i) + 1); // Make a random number between 0 and the array size | |
Swap(a, j, i); // Swap the current item with the random item | |
} | |
} | |
} | |
void VisualiseBlocks(int *a, int n, // Function to show blocks and cycles while highlighting 2 | |
int h1, int h2, | |
int c | |
) | |
{ | |
char buff[DISPLAY_BUFFER_SIZE]; // Display buffer | |
for (int i = 0; i < n +2; i++) // Loop for each item + 2 lines | |
{ | |
printf("\e[1A"); // Move the cursor up a line | |
} | |
sprintf(buff, "Cycles: %d\n", c); // Write the cycles to the buffer | |
for (int y = 0; y <= n; y++) // Loop over each item in the y direction | |
{ | |
for (int x = 0; x < n; x++) // Loop over each item int the x direction | |
{ | |
if (a[x] <= y) // If the current hight is equal or less than y | |
{ | |
if (x == h1 +1 || x == h2 +1) // If its a highlight | |
{ | |
strcat(buff, "\e[31m[]\e[0m"); // Append to the buffer in red | |
} | |
else | |
{ | |
strcat(buff, "[]"); | |
} | |
} | |
else | |
{ | |
strcat(buff, " "); | |
} | |
} | |
strcat(buff, "\n"); | |
} | |
printf(buff); // Write the buffer to the screen | |
} | |
void BubbleSort(int *a, int n) // Function to run the bubble sort algorithm | |
{ | |
int r = 0; // Variable that holds weather we are running | |
int c = 0; // Variable for the cycle count | |
while (r != n-1) // Loop while we still need to sort items | |
{ | |
r = 0; // Set the sort count to 0 | |
for (int i = 1; i < n; i++) // Loop over each item | |
{ | |
if (a[i] > a[i -1]) // If the item to its left is larger | |
{ | |
Swap(a, i, i -1); // Swap them | |
} | |
else | |
{ | |
r++; | |
} | |
c++; | |
VisualiseBlocks(a, n, i, i -1, c); // Update the visuals | |
usleep(SLEEP * 1000); // Wait a set ammount of milliseconds | |
} | |
} | |
} | |
void InsertionSort(int *a, int n) // Function to run the insertion sort algorithm | |
{ | |
int c = 0; // Variable for the cycle count | |
for (int i = 1; i < n; i++) // Loop over each item ignoring the first one | |
{ | |
if (a[i -1] < a[i]) // If the last item is smaller than the current item | |
{ | |
Swap(a, i, i -1); // Swap the last and current item | |
(i != 1) ? (i -= 2) : (0); // If i isnt 1 subtract 2 from i | |
} | |
c++; | |
VisualiseBlocks(a, n, i -1, i, c); // Update the visuals | |
usleep(SLEEP * 1000); // Wait for a set ammount of time | |
} | |
} | |
void SelectionSort(int *a, int n) // Function to run the selection sort algorithm | |
{ | |
int max = 0; // Variable for the location of the highest block | |
int c = 0; // Variable for the cycle count | |
for (int i = 0; i < n; i++) // Loop over each item | |
{ | |
max = i; // Set the current item to the maximum | |
for (int b = i; b < n; b++) // Loop over each item after the current item | |
{ | |
if (a[b] > a[max]) // If the current is larger than the maximum | |
{ | |
max = b; // Set the max equal to b | |
} | |
c++; | |
VisualiseBlocks(a, n, b, max, c); // Update the visuals | |
usleep(SLEEP * 1000); // Wait for a set ammount of time | |
} | |
if (max != i) // If the current item isnt the max | |
{ | |
Swap(a, i, max); // Swap the current item and the max | |
c++; | |
VisualiseBlocks(a, n, i, max, c); // Update the visuals | |
usleep(SLEEP * 1000); // Wait for a set ammount of time | |
} | |
} | |
} | |
void CombSort(int *a, int n, float s) // Function to run the comb sort algorithm | |
{ | |
int g = n -1; // Variable for the gap | |
int d = 0; // Variable for being done | |
int c = 0; // Variable for the cycles count | |
while (!d) // Loop while d = 0 | |
{ | |
g = g / s; // Reduce the gap by the scaling value | |
if (g <= 1) // If we have scaled below 2 | |
{ | |
d = 1; // Set done to true | |
} | |
for (int i = 0; i +g < n; i++) // Loop over each item accesable item | |
{ | |
if (a[i] < a[i +g]) // If the current item is smaller than the gap item | |
{ | |
Swap(a, i, i +g); // Swap the current item and gap item | |
d = 0; // Set done to false | |
} | |
c++; | |
VisualiseBlocks(a, n, i, i +g, c); // Update the visuals | |
usleep(SLEEP * 1000); // Wait for a set ammount of time | |
} | |
} | |
} | |
void Help() | |
{ | |
printf(" -- HELP --\n"); | |
printf("1. Bubble sort\n"); | |
printf("2. Insertion sort\n"); | |
printf("3. Selection Sort\n"); | |
printf("4. Comb Sort\n"); | |
} | |
int main(int argc, char ** argv) // Main entry point | |
{ | |
int choice = atoi(argv[1]); // Variable to hold the choice converted to an int | |
int n = BLOCK_COUNT; // Variable to hold the number of blocks | |
if (argc != 2) // If there are no arguments provided | |
{ | |
Help(); | |
return 0; | |
} | |
else if (choice < 1 || choice > 4) | |
{ | |
Help(); | |
return 0; | |
} | |
for (int i = 0; i < n +2; i++) // Loop over each item +2 | |
{ | |
printf("\n"); | |
} | |
GenerateBlocks(blocks, n); // Generate the new array | |
VisualiseBlocks(blocks, n, -1, -1, 0); // Show the new array | |
switch (choice) // Test against the users choice | |
{ | |
case 1: | |
BubbleSort(blocks, n); // Run the bubble sort algorithm | |
break; | |
case 2: | |
InsertionSort(blocks, n); // Run the insertion sort algorithm | |
break; | |
case 3: | |
SelectionSort(blocks, n); // Run the selection sort algorithm | |
break; | |
case 4: | |
CombSort(blocks, n, SHRINK); // Run the comb sort algorithm | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment