C++ Implementation
Deciding To Implement in C++
I knew from the start that I wanted to implement the sorting algorithms in C++ first. The reason for this was 2 fold:
I knew from the start that I wanted to implement the sorting algorithms in C++ first. The reason for this was 2 fold:
- I knew how to implement it without too much difficulty and it was much easier to debug
- Being able to compare the time differences for sorting algorithms in C++ and Assembly sounded interesting
The Code:
sortingalgorithms.cpp | |
File Size: | 4 kb |
File Type: | cpp |
Code Structure Explained:
In this code, I structured my main to execute my three implemented sorting algorithms. A part of each algorithm is a setup step that generates a list of 50,000 random number and times the execution of the algorithm. It then displays how long it took for the algorithm to finish. The algorithms themselves are implemented as described in the algorithms section.
In this code, I structured my main to execute my three implemented sorting algorithms. A part of each algorithm is a setup step that generates a list of 50,000 random number and times the execution of the algorithm. It then displays how long it took for the algorithm to finish. The algorithms themselves are implemented as described in the algorithms section.
Output:
The out put shows the various speeds of each sorting algorithms. The first thing we see is that bubble sort takes order of magnitude longer than either of the two other sorts. This is again because bubble sort has O(n^2) whereas quick and merge sort both have O(n*log(n)). The level of difference for these two is also partly due to the large size of 50,000. If the lists were more like 10-100 long, the difference would not be nearly as noticeable.