refactored code
This commit is contained in:
parent
2ba223598e
commit
2aad5564f1
|
@ -3,6 +3,8 @@ add_executable(sortalgs
|
|||
"application.c"
|
||||
"visualizer.c"
|
||||
"visualizers/histogram.c"
|
||||
"algorithms/bubblesort.c"
|
||||
"algorithms/mergesort.c"
|
||||
)
|
||||
|
||||
target_include_directories(sortalgs PRIVATE
|
||||
|
|
9
src/algorithms.h
Normal file
9
src/algorithms.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef _ALGORITHMS_H
|
||||
#define _ALGORITHMS_H
|
||||
|
||||
typedef struct Application Application;
|
||||
|
||||
void bubblesort(Application*);
|
||||
void mergesort(Application*);
|
||||
|
||||
#endif
|
21
src/algorithms/bubblesort.c
Normal file
21
src/algorithms/bubblesort.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "algorithms.h"
|
||||
#include "application.h"
|
||||
|
||||
void bubblesort(Application* app)
|
||||
{
|
||||
for(size_t n = visualizer_data.array_size; n > 1; n--)
|
||||
{
|
||||
for(int i = 0; i < n - 1; i++)
|
||||
{
|
||||
if(visualizer_data.array[i] > visualizer_data.array[i + 1])
|
||||
{
|
||||
int tmp = visualizer_data.array[i];
|
||||
visualizer_data.array[i] = visualizer_data.array[i + 1];
|
||||
visualizer_data.array[i + 1] = tmp;
|
||||
}
|
||||
|
||||
if(update_screen(app))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
67
src/algorithms/mergesort.c
Normal file
67
src/algorithms/mergesort.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include "algorithms.h"
|
||||
#include "application.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static void merge(Application* app, size_t left, size_t middle, size_t right);
|
||||
static void mergesort_rec(Application* app, size_t left, size_t right);
|
||||
|
||||
static int* tmp = NULL;
|
||||
|
||||
void mergesort(Application* app)
|
||||
{
|
||||
tmp = (int*)malloc(visualizer_data.array_size * sizeof(int));
|
||||
mergesort_rec(app, 0, visualizer_data.array_size - 1);
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
static void mergesort_rec(Application* app, size_t left, size_t right)
|
||||
{
|
||||
if(right > left)
|
||||
{
|
||||
size_t middle = (left + right) / 2;
|
||||
mergesort_rec(app, left, middle);
|
||||
mergesort_rec(app, middle + 1, right);
|
||||
|
||||
merge(app, left, middle, right);
|
||||
}
|
||||
}
|
||||
|
||||
static void merge(Application* app, size_t left, size_t middle, size_t right)
|
||||
{
|
||||
for(size_t k = left; k <= middle; k++)
|
||||
{
|
||||
tmp[k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
for(size_t k = middle + 1; k <= right; k++)
|
||||
{
|
||||
tmp[right + middle + 1 - k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
size_t i = left;
|
||||
size_t j = right;
|
||||
size_t k = left;
|
||||
|
||||
while(i < j)
|
||||
{
|
||||
if(tmp[i] <= tmp[j])
|
||||
{
|
||||
visualizer_data.array[k] = tmp[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
visualizer_data.array[k] = tmp[j];
|
||||
j--;
|
||||
}
|
||||
|
||||
k++;
|
||||
|
||||
if(update_screen(app))
|
||||
return;
|
||||
}
|
||||
|
||||
visualizer_data.array[right] = tmp[i];
|
||||
update_screen(app);
|
||||
}
|
|
@ -7,10 +7,7 @@
|
|||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "visualizers/histogram.h"
|
||||
|
||||
void bubblesort(Application* app);
|
||||
void mergesort(Application* app, size_t left, size_t right);
|
||||
void merge(Application* app, size_t left, size_t middle, size_t right);
|
||||
#include "algorithms.h"
|
||||
|
||||
void shuffle(int* array, size_t n);
|
||||
|
||||
|
@ -85,6 +82,12 @@ void run_application(Application* app)
|
|||
mergesort(app, 0, visualizer_data.array_size - 1);
|
||||
|
||||
while(!glfwWindowShouldClose(window))
|
||||
{
|
||||
update_screen(app);
|
||||
}
|
||||
}
|
||||
|
||||
int update_screen(Application* app)
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
|
@ -93,8 +96,8 @@ void run_application(Application* app)
|
|||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
glfwSwapBuffers(app->window);
|
||||
return glfwWindowShouldClose(app->window);
|
||||
}
|
||||
|
||||
void framebuffer_changed(GLFWwindow* window, int width, int height)
|
||||
|
@ -116,103 +119,3 @@ void shuffle(int *array, size_t n)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bubblesort(Application* app)
|
||||
{
|
||||
for(size_t n = visualizer_data.array_size; n > 1; n--)
|
||||
{
|
||||
for(int i = 0; i < n - 1; i++)
|
||||
{
|
||||
if(visualizer_data.array[i] > visualizer_data.array[i + 1])
|
||||
{
|
||||
int tmp = visualizer_data.array[i];
|
||||
visualizer_data.array[i] = visualizer_data.array[i + 1];
|
||||
visualizer_data.array[i + 1] = tmp;
|
||||
}
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mergesort(Application* app, size_t left, size_t right)
|
||||
{
|
||||
if(right > left)
|
||||
{
|
||||
size_t middle = (left + right) / 2;
|
||||
mergesort(app, left, middle);
|
||||
mergesort(app, middle + 1, right);
|
||||
|
||||
merge(app, left, middle, right);
|
||||
}
|
||||
}
|
||||
|
||||
void merge(Application* app, size_t left, size_t middle, size_t right)
|
||||
{
|
||||
int* tmp = (int*)malloc(visualizer_data.array_size * sizeof(int));
|
||||
for(size_t k = left; k <= middle; k++)
|
||||
{
|
||||
tmp[k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
for(size_t k = middle + 1; k <= right; k++)
|
||||
{
|
||||
tmp[right + middle + 1 - k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
size_t i = left;
|
||||
size_t j = right;
|
||||
size_t k = left;
|
||||
|
||||
while(i < j)
|
||||
{
|
||||
if(tmp[i] <= tmp[j])
|
||||
{
|
||||
visualizer_data.array[k] = tmp[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
visualizer_data.array[k] = tmp[j];
|
||||
j--;
|
||||
}
|
||||
|
||||
k++;
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
||||
|
||||
visualizer_data.array[right] = tmp[i];
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
typedef struct GLFWwindow GLFWwindow;
|
||||
|
||||
typedef struct
|
||||
typedef struct Application
|
||||
{
|
||||
GLFWwindow* window;
|
||||
Visualizer* active_visualizer;
|
||||
|
@ -15,5 +15,6 @@ int init_application(Application* app, int width, int height, const char* title)
|
|||
void destroy_application(Application* app);
|
||||
|
||||
void run_application(Application* app);
|
||||
int update_screen(Application* app);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue