Skip to content

NumberSorter is a C++ program that makes use of OOP to implement a NumberSorter class that reads a user-provided file containing integer numbers and generates random integer numbers concurrently and then performs sorting.

Notifications You must be signed in to change notification settings

hpoleselo/number_sorter_cpp

Repository files navigation

Number Sorter in C++

NumberSorter is a C++ program that makes use of OOP to implement a NumberSorter class that reads a user-provided .txt file containing integer numbers and generates random integer numbers concurrently by making use of Mutexes to avoid data racing between threads, sorting the read/generated numbers by using the sort std::sort algorithm from Standard Library Template, which has O(n) complexity.

NumberSorter was developed in:

  • Ubuntu 20.04
  • GCC 9.4.0
  • C++ 17

Running the Application

Clone this repository:

$ git clone git@github.com:hpoleselo/number_sorter_cpp.git

Run the provided executable:

$ ./NumberSorter

There's an out-of-the-box NumbersToBeSorted.txt file which has some pre-defined integers to validate the application. One can extend this file to contain more numbers, such file is located in the root directory of this repository. Recall the file must contain integers separated by spaces, otherwise other cases will be disregarded.

Building the Application Locally

$ g++ -std=c++17 main.cpp NumberSorter.cpp -lpthread -o NumberSorter

Or if wished to build it with GCC:

$ gcc -o NumberSorter NumberSorter.cpp -pthread -lstdc++

Building and Running the Containerized Application Locally

To avoid any compatibility issues, one is able to run this application containerized using Podman/Docker. Make sure to be in the root folder of this repository and build the image:

$ podman build -t numbersorter .

Run the container (in my local it ended up building with the localhost/ tag prefix):

$ podman run --rm -it localhost/programsorter:latest

Expected Output:

image_2023-07-17_07-43-20

Testing

$ g++ -std=c++17 NumberSorter.cpp tests/test_NumberSorter.cpp -lgtest -lgtest_main -pthread -o test_executable

Next Steps | Future Implementations

  • Feature: check file consistency, if provided file is using backspaces and add handling for such scenarios.
  • [ X ] Unit tests and fulfill at least 80% code coverage.
  • Set-up Codacy/Sonarqube to track code smells and so on.
  • Add logging.
  • Trade-off on performance comparison by having a specific thread to sort the data and accounting for the overhead by using Load Testing.
  • Refactor: Unique Lock instead of lock.

Useful Resources

Resources used to accomplish this task as future reference for myself:

About

NumberSorter is a C++ program that makes use of OOP to implement a NumberSorter class that reads a user-provided file containing integer numbers and generates random integer numbers concurrently and then performs sorting.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published