## Tag Archives: bubble 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.: The O(n2) sorting algorithm implemented in Project 1.Baseline

2.: An O(n log n) algorithm (Quick Sort).Decrease-by-half

3.: A tuned, optimized version of the O(n2) baseline algorithm.Optimized

**==== 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.

```
```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
// =============================================================================// Author: K Perkins// Date: Nov 2, 2013// Taken From: http://programmingnotes.org/// File: main.cpp// Description: This is the test harness program which runs the code and // measures the elapsed time of the code corresponding to the algorithm // in question. This program will try to improve the sorting methods// written in Project 1 by two approaches: // (1) Implementing an algorithm with better asymptotic performance// (2) Tuning an existing algorithm. // The sorting methods being reviewed are Bubble sort, Quick sort, and an // optimized version of Bubble sort.// =============================================================================#include <iostream>#include <cstdlib>#include <ctime>#include "Timer.h"#include "Project1.h"#include "Project3.h"using namespace std; // typedefs for the functions to useenum Algorithms {bubble, quick, bubbleOptim}; // number of algs being testedint NUM_ALGS = 3; // default arry sizeconst int ARRAY_SIZE = 150000; int main(){ // declare variables srand(time(NULL)); int* arry = new int[ARRAY_SIZE]; int seed = rand(); // generate a random seed to use for array on all algorithms Timer timer; Project1 proj1; Project3 proj3; // display the array size cerr<<"nArray Size = "<<ARRAY_SIZE<<endl; // loop to automatically execute the proj1 being tested for(int x=0; x < NUM_ALGS; ++x) { cout<<"n----- STARTING ALGORITHM #"<<x+1<<" ----- nn"; timer.Reset(); // place data in the array proj1.Generate(arry, ARRAY_SIZE, seed); // display data in array if its within range if(ARRAY_SIZE <= 25) { proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // start the timer timer.Start(); // determine which alg to execute switch(x) { case bubble: proj1.BubbleSort(arry, ARRAY_SIZE); break; case quick: proj3.QuickSort(arry, ARRAY_SIZE); break; case bubbleOptim: proj3.BubbleSort(arry, ARRAY_SIZE); break; default: cout<<"nThat option doesnt exist...n"; exit(1); break; } // stop the timer timer.Stop(); // display data in array if its within range if(ARRAY_SIZE <= 25) { cout<<endl; proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // display time cout<<endl<<"It took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; cout<<"n----- ALGORITHM #"<<x+1<<" DONE! ----- nn"; } delete[] arry; return 0;}// http://programmingnotes.org/

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

```
```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
// =============================================================================// Author: K Perkins// Date: Nov 2, 2013// Taken From: http://programmingnotes.org/// File: Project3.h// Description: This is a simple class which holds the functions for // project 3// =============================================================================#ifndef PROJECT3_H#define PROJECT3_H #include <cstdlib>#include <algorithm> class Project3{public: Project3(){} // exchange unsorted elements with the last element // not the adjacent element void BubbleSort(int arry[], int size) { int last = size-1; do{ for(int current=0; current < last; ++current) { if(arry[current] > arry[last]) { std::swap(arry[current], arry[last]); } } --last; }while(last >= 0); }// end of BubbleSort void QuickSort(int arry[], int size) { if(size > 1) { // choose pivot int pivotIndex = rand()%(size-1); // partition the arry and get the new pivot position int newPiviotIndex = Partition(arry, size, pivotIndex); // quick sort the first part QuickSort(arry, newPiviotIndex); // quick sort the second part QuickSort(arry+newPiviotIndex+1, size-newPiviotIndex-1); } }// end of QuickSort int Partition(int arry[], int size, int pivotIndex) { int pivotValue = arry[pivotIndex]; arry[pivotIndex] = arry[size-1]; // swap pivot with last element arry[size-1] = pivotValue; int left = 0; // left index int right = size-2; // right index while(left < right) { // ( < pivot ), pivot, ( >= pivot) while((arry[left] < pivotValue) && (left < right)) { ++left; } while((arry[right] >= pivotValue) && (left < right)) { --right; } if(left < right) { std::swap(arry[left], arry[right]); ++left; --right; } } if(left == right) { if(arry[left] < pivotValue) { ++left; } } arry[size-1] = arry[left]; // move pivot to its final place arry[left] = pivotValue; return left; // return the position of the pivot }// end of Partition ~Project3(){}};#endif // http://programmingnotes.org/

**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! -----`

## C++ || Empirical Analysis Using Min Element, Bubble Sort, & Selection 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 performs an empirical analysis of three non recursive algorithms. This program implements the algorithms and displays their performance running time to the screen.

The algorithms being examined are: MinElement, which finds the smallest element an array. Bubble Sort, and Selection Sort.

**==== 1. ASYMPTOTIC ANALYSIS ====**

Selection sort and Bubble sort both run in O(n2) time. MinElement runs in O(n) time. The empirical analysis implemented in this program should agree with the above asymptotic bounds, but sometimes experiments surprise us.

**==== 2. EMPIRICAL ANALYSIS ====**

To analyze the three algorithms empirically the elapsed running time (in seconds) should be measured for various values of array sizes “n.” These results should be graphed on a scatter plot, which will then help to infer which complexity class the plot corresponds to. The asymptotic analysis above says that we should expect these graphs to resemble linear or quadratic curves.

Timing code for empirical analysis takes some care. It is important to measure the elapsed time of only the code for the algorithm itself, and not other steps such as loading input files or printing output. Also, since computer code executes very rapidly, it is important to measure time in small fractions of seconds.

**==== 3. WHAT TO MEASURE ====**

The goal is to draw a scatter plot graph for each algorithm’s running times (a total of three plots). Each plot needs to have enough data points to interpolate a fitting curve; 5 is the smallest number that might be reasonable.

So each algorithm should be ran for at least 5 different values of size “n.” At least one very small value of n (less than 10) should be included, and one big value that’s large enough to make the code run for at least 5 minutes should be used. Once the data is graphed, the curve should resemble the appropriate asymptotic bounds for the function being examined.

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

**==== 4. 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 (MinElement, bubble sort, or insertion 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.

**==== 5. TEST HARNESS ====**

**Note**: This program uses a custom Timer class (Timer.h). To obtain code for that class, click here.

“Project1.h” is listed below.

```
```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
// =============================================================================// Author: K Perkins// Date: Nov 1, 2013// Taken From: http://programmingnotes.org/// File: main.cpp// Description: This is the test harness program which runs the code and // measures the elapsed time of the code corresponding to the algorithm // in question. This test program performs the following operations: // 1. Input a value of n.// 2. Create an array 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 (MinElement, bubble sort, insertion 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.// =============================================================================#include <iostream>#include <ctime>#include <cstdlib>#include "Timer.h"#include "Project1.h"using namespace std; // typedefs for the functions to useenum Algorithms {minElem, bubble, selection}; // the total number of algs to executeconst int NUM_ALGS = 3; // default array sizeconst int ARRAY_SIZE = 20000; int main(){ srand(time(NULL)); int* arry = new int[ARRAY_SIZE]; int min = 0; int seed = rand(); // generate a random seed to use for array on all algorithms Timer timer; Project1 proj1; // display the array size cerr<<"nArray Size = "<<ARRAY_SIZE<<endl; // loop to automatically execute the alg being tested for(int x=0; x < NUM_ALGS; ++x) { cout<<"n----- STARTING ALGORITHM #"<<x+1<<" ----- nn"; timer.Reset(); // place data into the array proj1.Generate(arry, ARRAY_SIZE, seed); // display data in array if its within range if(ARRAY_SIZE <= 25) { proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // start the timer timer.Start(); // determine which alg to execute switch(x) { case minElem: min = proj1.MinElement(arry, ARRAY_SIZE); cout<<"nMin = "<<min<<endl; break; case selection: proj1.SelectionSort(arry, ARRAY_SIZE); break; case bubble: proj1.BubbleSort(arry, ARRAY_SIZE); break; default: cout<<"nThat option doesnt exist...n"; exit(1); break; } // stop the timer timer.Stop(); // display data in array if its within range if(ARRAY_SIZE <= 25) { cout<<endl; proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // display total elapsed time cout<<endl<<"It took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; cout<<"n----- ALGORITHM #"<<x+1<<" DONE! ----- nn"; } return 0;}// http://programmingnotes.org/

**==== 6. THE ALGORITHMS – “include Project1.h” ====**

```
```
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
// =============================================================================// Author: K Perkins// Date: Nov 1, 2013// Taken From: http://programmingnotes.org/// File: Project1.h// Description: This is a simple class which holds the functions for // project 1 // =============================================================================#ifndef PROJECT1_H#define PROJECT1_H #include <iostream>#include <cassert>#include <cstdlib>#include <algorithm> class Project1{public: Project1(){} int MinElement(int arry[], int size) { assert(size > 0); int min = arry[0]; for(int x=1; x < size; ++x) { if(arry[x] < min) { min = arry[x]; } } return min; }// end of MinElement void SelectionSort(int arry[], int size) { for(int x=0; x <= size-2; ++x) { int min = x; for(int y=x+1; y <= size-1; ++y) { if(arry[y] < arry[min]) { min = y; } } std::swap(arry[x], arry[min]); } }// end of SelectionSort void BubbleSort(int arry[], int size) { for(int x=0; x <= size-2; ++x) { for(int y=0; y <= (size-2)-x; ++y) { if(arry[y+1] < arry[y]) { std::swap(arry[y], arry[y+1]); } } } }// end of BubbleSort void Generate(int arry[], int size, int seed) { srand(seed); for(int x=0; x < size; ++x) { arry[x] = rand() % 7281987; } }// end of Generate void Display(int arry[], int size) { for(int x=0; x < size; ++x) { std::cout<<arry[x]<<" "; } }// end of Display ~Project1(){}};#endif // http://programmingnotes.org/

**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 = 20000`

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

Min = 2

It took 0 clicks (0 seconds)

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

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

It took 4350 clicks (4.35 seconds)

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

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

It took 2150 clicks (2.15 seconds)

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