**************************************************** ** _____ _ ** ** / ___| | | ** ** \ `--. ___ _ __| |_ ___ _ __ ** ** `--. \/ _ \| '__| __/ _ \ '__| ** ** /\__/ / (_) | | | || __/ | ** ** \____/ \___/|_| \__\___|_| ** ** ** ** by Anestis Zioulis ** ** www.azioulis.com ** ****************************************************
About - Demo - Installation - Algorithms - Tools - Sources
Sorter is an array sorting and benchmarking program written in C++. It lets users measure how long
it will take to sort an array of user-defined sizes and scenarios. The array is populated by the
Mersenne Twister
, a pseudo-random number generator. Then each algorithm runs through a copy of
the same array while time is being recorded and saved in a Results.txt
file for further studies.
It should be able to run on every popular OS that can run C++.
This program was created as anΒ assignmentΒ for the lesson "Data Structures" at theΒ IHUΒ (International Hellenic University, Serres Campus) by Anestis Zioulis. Part of the code comes from their respectful creators. Check "Sources" for more information.
My assignment:
- Develop a program in
C++
that will create a dynamic integer array.- The user must determine the array length.
- A user-selection menu must follow that contains four scenarios:
100% random
,50% sorted and 50% random
,Sorted ASC
,Sorted DESC
.- Use the Mersenne Twister (a pseudo-random number generator) to fill the arrays (provided).
- After the scenario selection, the array must be initialized accordingly and get sorted with the following sorting algorithms (provided):
Bubble
,Selection
,Insertion
,Quick
.- Track each algorithm's time in seconds with the
clock()
function.- Save the result in the
Result.txt
file.
Additional changes:
I did implement some additional changes from the MVP I created.
- The program will loop until the user inputs
0
in the array length.- It will append and save all the benchmarks in the
Results.txt
file.- Renamed variables, classes, etc. to make it readable for international users.
- Added input validation to avoid crashes.
- Created this
readme
file.
You can access a runnable demo hosted on replit.com
from the following link:
In case it's down, you can watch the gif file below to get an idea.
To use this program, you can download the latest release and use the executable inside. Also, you can compile the source code manually or through an IDE with a bundled compiler.
- [1] Download the latest release and run the executable.
- [2] Download JetBrains CLion or another IDE that includes the compiler.
- [3] Download a tool chain, like MinGW, through the web and install it.
- [3] If you opted to compile the source code yourself.
- You downloaded the tool chain that has the compiler and installed it.
- Then open
cmd
, change directory to the folder where the source file is, and check ifg++
is available with the following command:g++ -v
- If you see something like this, you are good to compile:
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe Target: mingw32 Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-20200227-1' Thread model: win32 gcc version 9.2.0 (MinGW.org GCC Build-20200227-1)
- Type:
and your executable should have been compiled.
g++ -o Sorter.exe main.cpp class/Array.cpp class/Message.cpp class/RandMT.cpp class/User.cpp
- You can run it in
cmd
by just typing it:Sorter.exe
Quick
void Array::quickSort(int left, int right) {
unsigned long int i, j, x, mid;
if (left < right) {
i = left;
j = right;
mid = (left + right) / 2;
x = array[mid];
while (i < j) {
while (array[i] < x)
i++;
while (array[j] > x)
j--;
if (i < j) {
if (array[i] == array[j]) {
if (i < mid)
i++;
if (j > mid)
j--;
} else {
swap(array[i], array[j]);
}
}
}
quickSort(left, j - 1);
quickSort(i + 1, right);
}
}
Quick
void Array::quickSort(int left, int right) {
unsigned long int i, j, x, mid;
if (left < right) {
i = left;
j = right;
mid = (left + right) / 2;
x = array[mid];
while (i < j) {
while (array[i] < x)
i++;
while (array[j] > x)
j--;
if (i < j) {
if (array[i] == array[j]) {
if (i < mid)
i++;
if (j > mid)
j--;
} else {
swap(array[i], array[j]);
}
}
}
quickSort(left, j - 1);
quickSort(i + 1, right);
}
}
The tools used to create this assignment.
- MingGW_GCC 9.2.0 compiler from MinGW-w64
- Jetbrains CLion (student/full version)
- C++14 Standard
Sources I used materials to create the project and readme.
- Bubble-Sort Algorithm
- Selection-Sort Algorithm
- Insertion-Sort Algorithm
- Quick-Sort Algorithm
- Mersenne-Twister (a pseudo-random number generator)
- Wikipedia (overall)
- Text to ASCII generator
- Best-README-Template