Sorting: Quick Sort
13th January 2006 - By Paradochs
Information
- Order: O(n²)
- Best Case: O(nlog(n))
- Average: O(nlog(n))
- Memory Usage: O(log(n))
- Stable: No
- Comparison Based: Yes
Description (from wikipedia)
Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes O(n log n) comparisons to sort n items. However, in the worst case, it makes O(n²) comparisons. Typically, quicksort is significantly faster in practice than other O(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of requiring quadratic time. Quicksort is a comparison sort.
Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists.
The steps are:
- Pick an element, called a pivot, from the list.
- Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
- Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
The base case of the recursion are lists of size zero or one, which are always sorted. The algorithm always terminates because it puts at least one element in its final place on each iteration.
C++ Files and Code Listings
This code has a few built in functions to time the sort and count the number of comparisons.
This code was written and compiled successfully in Visual Studio .NET.
/*
Authors:Travis Gadberry
Patrick Hesser
Chris Ladewig
Date: 9 Feb 05
Updated:9 Feb 05
File: QuickSort.h
*/
#include <time.h>
class QuickSort {
public:
QuickSort();
QuickSort(int *, int);
~QuickSort();
int * getArray();
int getN();
bool getSorted();
double getTime();
int getIncrements();
void setArray(int *);
void sort();
private:
void setN(int);
void setSorted(bool);
void resetTime();
void resetIncrements();
void quicksort(int *, int, int);
int partition(int *, int, int);
int * A;
int n;
bool sorted;
double time;
int increments;
};
/*
Authors:Travis Gadberry
Patrick Hesser
Chris Ladewig
Date: 9 Feb 05
Updated:16 Feb 05
File: QuickSort.cpp
*/
#ifndef QUICKSORT_H
#define QUICKSORT_H
#include "QuickSort.h"
#endif
QuickSort::
QuickSort() {
n = 1;
A = new int[n];
A[0] = 0;
sorted = true;
}
QuickSort::
QuickSort(int * newArray, int N) {
n = N;
A = new int[n];
for(int i=0; i<n; i++)
A[i] = newArray[i];
sorted = false;
resetIncrements();
}
QuickSort::
~QuickSort() {
delete(A);
}
int *
QuickSort::
getArray() {
return A;
}
int
QuickSort::
getN() {
return n;
}
bool
QuickSort::
getSorted() {
return sorted;
}
double
QuickSort::
getTime() {
return time;
}
int
QuickSort::
getIncrements() {
return increments;
}
void
QuickSort::
setArray(int * newArray) {
delete(A);
n = sizeof(newArray);
A = new int[n];
sorted = false;
}
void
QuickSort::
sort() {
clock_t t1, t2;
t1 = clock();
quicksort(A, 0, n-1);
t2 = clock();
time = (t2 - t1)/CLOCKS_PER_SEC;
}
void
QuickSort::
quicksort(int * list, int i, int n) {
int p;
if(increments++, i < n) {
p = partition(list, i, n); /* p is the list pivot position */
quicksort(list, i, p-1);
quicksort(list, p+1, n);
}
}
int
QuickSort::
partition(int * list, int i, int j) {
int middle=(i+j)/2; /* middle as the pivot */
int pivot=list[middle];
list[middle]=list[i];
list[i]=pivot;
int p=i;
for(int k=i+1; k<=j; k++)
if(increments++, list[k]<pivot) { /* elements less than pivot */
int temp=list[++p]; /* replaced by list[++p] */
list[p]=list[k];
list[k]=temp;
}
int temp=list[i]; /* pivot in list[p] */
list[i]=list[p];
list[p]=temp;
return p; /* return pivot position */
}
void
QuickSort::
setN(int newN) {
n = newN;
}
void
QuickSort::
setSorted(bool newSorted) {
sorted = newSorted;
}
void
QuickSort::
resetTime() {
time = 0.0;
}
void
QuickSort::
resetIncrements() {
increments = 0;
}