Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit eef8d06

Browse files
committed
v0.1
1 parent 95aea77 commit eef8d06

File tree

13 files changed

+671
-0
lines changed

13 files changed

+671
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <vector>
2+
3+
using namespace std;
4+
5+
// O(N^2)
6+
void bubble_sort(vector<int> &vector) {
7+
int length = vector.size();
8+
// Main loop
9+
for (int i = 0; i < length - 1; i++) {
10+
for (int j = 0; j < length - i - 1; j++) {
11+
// Variable swap
12+
if (vector[j] > vector[j + 1]) {
13+
int aux = vector[j];
14+
vector[j] = vector[j + 1];
15+
vector[j + 1] = aux;
16+
}
17+
}
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <vector>
2+
3+
using namespace std;
4+
5+
// O(N^2)
6+
void selection_sort(vector<int> &vector) {
7+
int length = vector.size();
8+
// Main loop
9+
for (int i = 0; i < length; i++) {
10+
// With two variables (one for the minimum value and one for its index) we managed to halve the
11+
// number of memory accesses of the algorithm
12+
int minimum = vector[i];
13+
int minumum_index = i;
14+
// Search for the minimum
15+
for (int j = i + 1; j < length; j++) {
16+
if (vector[j] < minimum) {
17+
minimum = vector[j];
18+
minumum_index = j;
19+
}
20+
}
21+
// Variable swap
22+
if (minumum_index != i) {
23+
int aux = vector[i];
24+
vector[i] = vector[minumum_index];
25+
vector[minumum_index] = aux;
26+
}
27+
}
28+
}

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Sort Algorithm Viewer
2+
3+
Made by Adrián Fenollar Navarro, 3rd year computer engineering student
4+
at the University of Murcia; October 26, 2020.
5+
6+
## Introduction
7+
This project consists of a simple ordering algorithm viewer,
8+
with which it is intended that it can be a tool that facilitates
9+
the study and learning of these.
10+
11+
That's the first beta version of the application, with only two sort algorithms implemented.
12+
13+
## Requirements
14+
15+
The only thing you need to be able to execute it is:
16+
17+
- A Linux environment (it has only been tested on Ubuntu)
18+
- Have the SFML library installed
19+
20+
If you don't have SFML installed, you can use the attached script `sfml_installer.sh`
21+
to install it, or use the next commands (valid on Ubuntu):
22+
```
23+
sudo apt-get update
24+
sudo apt-get install libsfml-dev
25+
```
26+
If you use another version of linux and the above has not worked for you,
27+
here is the [official SFML installation page on Linux](https://www.sfml-dev.org/tutorials/2.5/start-linux.php).
28+
29+
If you want to recompile it, you must have the following packages installed:
30+
31+
```
32+
sudo apt-get build-essential make cmake
33+
```
34+
35+
## How to compile it
36+
37+
In this repository there is already an executable called `SortAlgorithmViewer` with the
38+
project code compiled and ready to run. In the event that this executable does not
39+
work for you, or you want to try to make modifications to the code and recompile it,
40+
you must execute the next commands from the project directory:
41+
42+
```
43+
cmake CMakeLists.txt
44+
make
45+
```
46+
47+
## How to use it
48+
49+
A more detailed user manual will be included in later versions. At the moment, the
50+
program runs as follows:
51+
52+
```
53+
./SortAlgorithmViewer [OPTIONS] <algorithms>
54+
```
55+
56+
where the `OPTIONS` are
57+
58+
- `-n, --size <array_size>`: specifies the size of the array
59+
- `-f, --framerate <framerate>`: specify the number of frames per second
60+
- `-h, --help`: show help menu
61+
62+
and `algorithms` can be (non case sensitive)
63+
64+
- `Selection`
65+
- `Bubble`
66+
67+
At the moment only one algorithm can be included per run.
68+
69+
## Updates
70+
71+
(still empty)
72+

SortAlgorithmViewer

765 KB
Binary file not shown.

include/SortAlgorithms.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SORT_ALGORITHM_VIEWER_SORTALGORITHMS_H
2+
#define SORT_ALGORITHM_VIEWER_SORTALGORITHMS_H
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
#include "VisualWindow.h"
8+
9+
// TODO Add new sort methods
10+
11+
void selection_sort(class VisualWindow &window);
12+
13+
void bubble_sort(class VisualWindow &window);
14+
15+
const static map<string, void (*)(class VisualWindow&)> sortAlgorithm = {
16+
{"selection", selection_sort},
17+
{"bubble", bubble_sort}
18+
};
19+
20+
#endif //SORT_ALGORITHM_VIEWER_SORTALGORITHMS_H

include/VisualVector.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef SORT_ALGORITHM_VIEWER_VISUALVECTOR_H
2+
#define SORT_ALGORITHM_VIEWER_VISUALVECTOR_H
3+
4+
#include <iostream>
5+
#include "SortAlgorithms.h"
6+
7+
using namespace std;
8+
9+
#define DEFAULT_ARRAY_SIZE 100
10+
11+
enum BoxStatus {
12+
NONE = 0, SELECTED = 1, CHANGED = 2
13+
};
14+
15+
class VisualVector {
16+
private:
17+
// Properties
18+
int *array;
19+
BoxStatus *status;
20+
int length;
21+
int max_value;
22+
// Counters
23+
long long contComparisons = 0, contArrayAccesses = 0, contSwaps = 0;
24+
25+
public:
26+
// Constructor
27+
explicit VisualVector(int length);
28+
29+
// Destroyer
30+
~VisualVector();
31+
32+
// Operators
33+
int & operator[](int i);
34+
35+
// Getters
36+
int getLength() const;
37+
int getMaxValue() const;
38+
BoxStatus getStatus(int index);
39+
long long getNumComparisons() const;
40+
long long getNumArrayAccesses() const;
41+
long long getNumSwaps() const;
42+
43+
// Setters
44+
void setStatus(int index, BoxStatus status);
45+
46+
// Main methods
47+
void initialize();
48+
void fillWithRandom();
49+
void addComparisons(int q);
50+
void addArrayAccesses(int q);
51+
void addSwaps(int q);
52+
void print();
53+
};
54+
55+
56+
#endif //SORT_ALGORITHM_VIEWER_VISUALVECTOR_H

include/VisualWindow.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef SORT_ALGORITHM_VIEWER_VISUALWINDOW_H
2+
#define SORT_ALGORITHM_VIEWER_VISUALWINDOW_H
3+
4+
#include <iostream>
5+
#include <SFML/Graphics.hpp>
6+
7+
#include "VisualVector.h"
8+
9+
using namespace std;
10+
using namespace sf;
11+
12+
// Window default dimensions
13+
#define DEFAULT_WINDOW_WIDTH 600
14+
#define DEFAULT_WINDOW_HEIGHT 600
15+
16+
// Color palette
17+
#define NONE_COLOR Color::White
18+
#define SELECTED_COLOR Color::Blue
19+
#define CHANGED_COLOR Color::Green
20+
21+
class VisualWindow : public RenderWindow {
22+
private:
23+
// Properties
24+
class VisualVector *vector;
25+
double width, height;
26+
string algorithm;
27+
Font font;
28+
29+
// Helper methods to draw frames
30+
void drawBarChart(double x, double y, double width, double height);
31+
32+
public:
33+
// Constructor
34+
VisualWindow(VisualVector *vector, string algorithm, int framerate, int width, int height, const string &name);
35+
36+
// Destroyer
37+
~VisualWindow() override;
38+
39+
// Getters
40+
VisualVector *getVisualVector();
41+
double getWidth() const;
42+
double getHeight() const;
43+
44+
// Draw methods
45+
void drawAndDisplay();
46+
};
47+
48+
49+
#endif //SORT_ALGORITHM_VIEWER_VISUALWINDOW_H

resources/fonts/jack.ttf

53 KB
Binary file not shown.

sfml_installer.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
echo Installing SFML library for your distribution...
4+
echo Entering privileged mode...
5+
6+
if [ $(command -v apt) ]
7+
then
8+
exec sudo apt install libsfml-dev
9+
fi
10+
if [ $(command -v dnf) ]
11+
then
12+
exec sudo dnf install sfml
13+
fi
14+
if [ $(command -v rpm) ]
15+
then
16+
exec sudo rpm install sfml
17+
fi
18+
if [ $(command -v pacman) ]
19+
then
20+
exec sudo pacman -S sfml
21+
else
22+
echo "Sorry!!!"
23+
echo "Your distribution is not supported!!!"
24+
fi

src/SortAlgorithms.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "../include/SortAlgorithms.h"
2+
3+
// Implies 4 memory accesses
4+
void swap(int &a, int &b) {
5+
int aux = a;
6+
a = b;
7+
b = aux;
8+
}
9+
10+
// O(N^2)
11+
void selection_sort(class VisualWindow &window) {
12+
13+
VisualVector *vector = window.getVisualVector();
14+
Event e{};
15+
int length = vector->getLength();
16+
// Main loop
17+
for (int i = 0; i < length && window.isOpen(); i++) {
18+
// Window handler
19+
while (window.pollEvent(e)) {
20+
// Window closed
21+
if (e.type == Event::Closed) {
22+
window.close();
23+
exit(0);
24+
}
25+
}
26+
27+
// With two variables (one for the minimum value and one for its index) we managed to halve the
28+
// number of memory accesses of the algorithm
29+
int minimum = (*vector)[i];
30+
int minumum_index = i;
31+
// Search for the minimum
32+
for (int j = i + 1; j < length; j++) {
33+
if ((*vector)[j] < minimum) {
34+
minimum = (*vector)[j];
35+
minumum_index = j;
36+
vector->addArrayAccesses(1);
37+
}
38+
}
39+
// Variable swap
40+
if (minumum_index != i) {
41+
swap((*vector)[i], (*vector)[minumum_index]);
42+
vector->addArrayAccesses(4);
43+
vector->addSwaps(1);
44+
}
45+
46+
// Adding remaining numComparisons and numArrayAccesses
47+
vector->addComparisons(length - i);
48+
vector->addArrayAccesses(length - i);
49+
50+
// Set a status for exchanged boxes
51+
vector->setStatus(i, CHANGED);
52+
vector->setStatus(minumum_index, SELECTED);
53+
54+
// Draw the current frame
55+
window.drawAndDisplay();
56+
}
57+
}
58+
59+
// O(N^2)
60+
void bubble_sort(class VisualWindow &window) {
61+
62+
VisualVector *vector = window.getVisualVector();
63+
Event e{};
64+
int length = vector->getLength();
65+
// Main loop
66+
for (int i = 0; i < length - 1 && window.isOpen(); i++) {
67+
// Window handler
68+
while (window.pollEvent(e)) {
69+
// Window closed
70+
if (e.type == Event::Closed) {
71+
window.close();
72+
exit(0);
73+
}
74+
}
75+
76+
for (int j = 0; j < length - i - 1; j++) {
77+
if ((*vector)[j] > (*vector)[j+1]) {
78+
swap((*vector)[j],(*vector)[j+1]);
79+
vector->addArrayAccesses(4);
80+
vector->addSwaps(1);
81+
82+
// Adding remaining numComparisons and numArrayAccesses
83+
vector->addComparisons(length - i);
84+
vector->addArrayAccesses(length - i);
85+
86+
// Set a status for exchanged boxes
87+
vector->setStatus(j, CHANGED);
88+
vector->setStatus(j + 1, SELECTED);
89+
90+
}
91+
92+
}
93+
94+
// Draw the current frame
95+
window.drawAndDisplay();
96+
}
97+
}

0 commit comments

Comments
 (0)