Tag Archives: quick sort

C++ || Decrease By Half Sorting Using Bubble Sort, Quick Sort, & Optimized Bubble Sort

The following is another homework assignment which was presented in an Algorithm Engineering class. Using a custom timer class, the following is a program which tries to improve upon the sorting code demonstrated in the initial Empirical Analysis.

The following program will execute two approaches: (1) implementing an algorithm with better asymptotic performance, and (2) tuning an existing algorithm.

==== 1. THE OBJECTIVE ====

The purpose of implementing this program is to obtain empirical results that answer the following questions:

• Are O(n log n) expected-time sorting algorithms, such as merge sort and quick sort, significantly faster than O(n2)-time algorithms in practice?
• If so, by what margin? Is implementing a faster algorithm worth the effort?
• Is it possible to get a O(n2)-time algorithm to beat a O(nlogn)-time algorithm by paying attention to implementation details?
• If so, how much faster? Do you get better bang-for-the-buck by switching to an asymptotically-faster algorithm, or optimizing the same algorithm?

==== 2. THE ALGORITHMS ====

This program involves implementing and analyzing three algorithms:

1. Baseline: The O(n2) sorting algorithm implemented in Project 1.
2. Decrease-by-half: An O(n log n) algorithm (Quick Sort).
3. Optimized: A tuned, optimized version of the O(n2) baseline algorithm.

==== 3. FLOW OF CONTROL ====

A test harness program is created which executes the above functions and measures the elapsed time of the code corresponding to the algorithm in question. The test program will perform the following steps:

1. Input the value of n. Your code should treat n as a variable.
2. Create an array or vector of n random integers to serve as a problem instance.
3. Use a clock function to get the current time t1 .
4. Execute one algorithm (Bubble Sort, Quick sort, or Optimized Bubble Sort), using the array of random integers as input.
5. Use a clock function to get the current time t2 .
6. Output the elapsed time, t2 − t1 .

The test harness is configured in such a way to run all of the three algorithms, using a switch statement to change between the algorithms.

==== 4. TEST HARNESS ====

Note: This program uses two external header files (Timer.h and Project1.h).
• Code for the Timer class (Timer.h) can be found here.
• Code for “Project1.h” can be found here.
• “Project3.h” is listed below.

==== 5. THE ALGORITHMS – “include Project3.h” ====


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Note: This page presents sample code for the above problem, but scatter plots will not be provided.

The following is sample output:

Array Size = 150000

----- STARTING ALGORITHM #1 -----

It took 248290 clicks (248.29 seconds)

----- ALGORITHM #1 DONE! -----

----- STARTING ALGORITHM #2 -----

It took 50 clicks (0.05 seconds)

----- ALGORITHM #2 DONE! -----

----- STARTING ALGORITHM #3 -----

It took 164300 clicks (164.3 seconds)

----- ALGORITHM #3 DONE! -----