An open API service indexing awesome lists of open source software.

https://github.com/salma-mamdoh/sorting-algorithms

Our project for Data Structures Course taken during fall 2023 semester
https://github.com/salma-mamdoh/sorting-algorithms

analysis cpp sorting-algorithms

Last synced: 8 months ago
JSON representation

Our project for Data Structures Course taken during fall 2023 semester

Awesome Lists containing this project

README

          




Sorting Algorithms


In this project, we implemented several sorting algorithms using templates to sort data of different types. The algorithms we implemented are:



  • Insertion sort

  • Selection sort

  • Bubble sort

  • Shell sort

  • Count sort

  • Merge sort

  • Quick sort


Implementation


Each algorithm is implemented as a separate function using templates. We tested each algorithm on randomly generated arrays of sizes 200, 500, 1000, 5000, 10000, 20000, and 50000. We calculated the running time on each dataset and drew a plot for each algorithm to see how the running time changes according to the different sizes of datasets.


Results


Insertion sort


Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. It has an average time complexity of O(n^2).


Insertion sort plot

As we can see from the plot, the running time of insertion sort increases drastically as the size of the dataset increases. Therefore, it is not recommended to use insertion sort for large datasets.


Selection sort


Selection sort is an in-place comparison sorting algorithm that has an average time complexity of O(n^2).


Selection sort plot

As we can see from the plot, the running time of selection sort increases significantly as the size of the dataset increases. Therefore, it is not recommended to use selection sort for large datasets.


Bubble sort


Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. It has an average time complexity of O(n^2).


Bubble sort plot

As we can see from the plot, the running time of bubble sort increases significantly as the size of the dataset increases. Therefore, it is not recommended to use bubble sort for large datasets.


Shell sort


Shell sort is an in-place comparison sorting algorithm that is a generalization of insertion sort. It has an average time complexity of O(n^2).


Shell sort plot

As we can see from the plot, the running time of shell sort increases significantly as the size of the dataset increases. Therefore, it is not recommended to use shell sort for large datasets.


Count sort


Count sort is an efficient algorithm for sorting arrays with small integer values. It has a time complexity of O(n+k), where k is the maximum value in the array.


Count sort plot

As we can see from the plot, the running time of count sort is very low and almost constant, regardless of the size of the dataset. Therefore, count sort is a good choice for sorting arrays with small integer values.


Merge sort


Merge sort is a divide-and-conquer algorithm that recursively divides the input array into two halves, sorts each half, and then merges the sorted halves. It has a time complexity of O(n log n).


Merge sort plot

As we can see from the plot, the running time of merge sort increases slowly as the size of the dataset increases. Therefore, merge sort is a good choice for large datasets.


Quick sort


Quick sort is a divide-and-conquer algorithm that selects a pivot element and partitions the input array around the pivot, recursively sorting the two subarrays. It has an average time complexity of O(n log n).


Quick sort plot

As we can see from the plot, the running time of quick sort increases slowly as the size of the datasetincreases. Therefore, quick sort is a good choice for large datasets.


Conclusion


In conclusion, we implemented several sorting algorithms using templates and tested them on randomly generated arrays of different sizes to analyze their running time. We found that count sort is a good choice for sorting arrays with small integer values, while merge sort and quick sort are good choices for large datasets. On the other hand, insertion sort, selection sort, bubble sort, and shell sort are not recommended for large datasets due to their significantly increasing running time.