Skip to content

anestisZioulis/Sorter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

****************************************************
**           _____            _                   **
**          /  ___|          | |                  **
**          \ `--.  ___  _ __| |_ ___ _ __        **
**           `--. \/ _ \| '__| __/ _ \ '__|       **
**          /\__/ / (_) | |  | ||  __/ |          **
**          \____/ \___/|_|   \__\___|_|          **
**                                                **
**                by Anestis Zioulis              **
**                 www.azioulis.com               **
****************************************************

About

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.





Demo

You can access a runnable demo hosted on replit.com from the following link:

https://replit.com/@anestisZioulis/Sorter?v=1

In case it's down, you can watch the gif file below to get an idea.





Installation

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.
  • or

  • [2] Download JetBrains CLion or another IDE that includes the compiler.
  • or

  • [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 if g++ 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:
    g++ -o Sorter.exe main.cpp class/Array.cpp class/Message.cpp class/RandMT.cpp class/User.cpp
    and your executable should have been compiled.

  • You can run it in cmd by just typing it:
    Sorter.exe




Algorithms

Bubble Sort Quick Sort Insertion Sort Selection Sort
Bubble Quick Insertion Selection

Click below to see the algorithm's implementation.


Bubble

Bubble

void Array::bubbleSort() {
    unsigned long int i, j, temp;
    for (i = 1; i < arraySize; i++)
        for (j = arraySize - 1; j >= i; j--)
            if (array[j - 1] > array[j]) {
                temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }
}


Quick

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);
    }
}


Insertion

Quick

void Array::insertSort() {
    int i, j;
    unsigned long int x;
    for (i = 1; i < arraySize; i++) {
        x = array[i];
        j = i - 1;
        while (j >= 0 && array[j] > x) {
            array[j + 1] = array[j];
            j = j - 1;
        }
        array[j + 1] = x;
    }
}


Selection

Selection

void Array::selectSort() {
    unsigned long int i, j, k, min;
    for (i = 0; i < arraySize - 1; i++) {
        k = i;
        min = array[i];
        for (j = i + 1; j < arraySize; j++) {
            if (array[j] < min) {
                k = j;
                min = array[j];
            }
        }
        array[k] = array[i];
        array[i] = min;
    }
}





Tools

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

Sources I used materials to create the project and readme.