diff options
author | Juan Manuel Tomas <jtomas1815@gmail.com> | 2021-08-14 13:28:00 -0300 |
---|---|---|
committer | Juan Manuel Tomas <jtomas1815@gmail.com> | 2021-08-14 13:28:00 -0300 |
commit | b79a4d1e41f273945ebac8c620702e73ad63cb51 (patch) | |
tree | 784641f42c9d4b2ca7a32dc95a9cc453ab6b8d6c | |
download | sort-b79a4d1e41f273945ebac8c620702e73ad63cb51.tar.gz sort-b79a4d1e41f273945ebac8c620702e73ad63cb51.zip |
Initial Commit
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | main.c | 127 |
3 files changed, 135 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf5f013 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +main +font.png +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c88f7a1 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +main: main.o + gcc main.o -o main `sdl2-config --libs` -lSDL2_image + +main.o: main.c + gcc -c main.c -o main.o `sdl2-config --cflags` @@ -0,0 +1,127 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <SDL.h> +#include <SDL_image.h> + +#define ASCII_SPACE 32 +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define SRC_FONT_WIDTH 7 +#define SRC_FONT_HEIGHT 9 +#define DST_FONT_WIDTH 28 +#define DST_FONT_HEIGHT 32 +#define ATLAS_WIDTH 18 +#define DATASET_SIZE 100 +#define STATS_SIZE 80 + +int main(int argc, char **argv) { + srand(time(0)); + SDL_Init(SDL_INIT_VIDEO); + IMG_Init(IMG_INIT_PNG); + SDL_Window *win = SDL_CreateWindow( + "Sorting algorithms", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + WINDOW_WIDTH, + WINDOW_HEIGHT, + 0); + SDL_Renderer *ren = SDL_CreateRenderer( + win, + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + SDL_Texture *font_atlas = IMG_LoadTexture(ren, "./font.png"); + + int to_be_sorted[DATASET_SIZE] = {0}; + for (int i = 0; i < DATASET_SIZE;i++) { + to_be_sorted[i] = i + 1; + } + for (int i = 0; i < DATASET_SIZE; i++) { + int tmp = to_be_sorted[i]; + int j = rand() % DATASET_SIZE; + to_be_sorted[i] = to_be_sorted[j]; + to_be_sorted[j] = tmp; + } + int sort_index = 0; + int sort_index_internal = 0; + int comparisons = 0; + int swaps = 0; + + while (1) { + SDL_Event e; + if (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + break; + } + } + + if (sort_index < DATASET_SIZE - 1) { + if (sort_index_internal >= 0 + && to_be_sorted[sort_index_internal] > to_be_sorted[sort_index_internal + 1]) { + int tmp = to_be_sorted[sort_index_internal + 1]; + to_be_sorted[sort_index_internal + 1] = to_be_sorted[sort_index_internal]; + to_be_sorted[sort_index_internal] = tmp; + sort_index_internal--; + comparisons++; + swaps++; + } else { + sort_index++; + sort_index_internal = sort_index; + comparisons++; + } + } + + SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); + SDL_RenderClear(ren); + + char stats[STATS_SIZE] = {0}; + sprintf(stats, "Comparisons: %d\nSwaps: %d", comparisons, swaps); + int line = 0; + int col = 0; + for (int i = 0; i < STATS_SIZE; i++) { + if (stats[i] == '\n') { + line++; + col = 0; + } else { + col++; + } + SDL_Rect src = { + (stats[i] - ASCII_SPACE) % ATLAS_WIDTH * SRC_FONT_WIDTH, + (stats[i] - ASCII_SPACE) / ATLAS_WIDTH * SRC_FONT_HEIGHT, + SRC_FONT_WIDTH, + SRC_FONT_HEIGHT + }; + SDL_Rect dst = { + col * DST_FONT_WIDTH, + line * DST_FONT_HEIGHT, + DST_FONT_WIDTH, + DST_FONT_HEIGHT + }; + SDL_RenderCopy(ren, font_atlas, &src, &dst); + } + + for (int i = 0; i < DATASET_SIZE; i++) { + SDL_Rect bar = { + i * WINDOW_WIDTH / DATASET_SIZE, + WINDOW_HEIGHT, + WINDOW_WIDTH / DATASET_SIZE, + -to_be_sorted[i] * (int) (WINDOW_HEIGHT * 0.8) / DATASET_SIZE + }; + if (sort_index_internal == i - 1) { + SDL_SetRenderDrawColor(ren, 0, 255, 0, 255); + } else if (sort_index == i - 1) { + SDL_SetRenderDrawColor(ren, 255, 0, 0, 255); + } else { + SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); + } + SDL_RenderFillRect(ren, &bar); + } + + SDL_RenderPresent(ren); + } + SDL_DestroyWindow(win); + SDL_DestroyRenderer(ren); + IMG_Quit(); + SDL_Quit(); + return 0; +} |