Hello! Below is my attempt at a quicksort. Sorters are really interesting code because they can be done in so many different ways, which can make such a difference in the speed an array is sorted. Here (No longer online :(, here’s another) is a visual example from my old professor Adam Smith. From this visual guide, you’re able to see when quicksort is most and least effective. Here’s some output:

Screen Shot 2018-02-02 at 12.00.27 PMScreen Shot 2018-02-02 at 12.01.15 PM

//  Quicksort2.cpp
//  Sorter which will sort array of random values in the "quicksort" style.
//  Created by Aidan Takami on 2/1/18.
//  Copyright © 2018 Aidan Takami. All rights reserved.

     Must include iostream, cstdlib, and ctime. Site keeps deleting
     these libraries thinking they are HTML tags

using namespace std;

void printArray(int r[]);
void switchValues(int r[], int, int);
void randmoArray(int r[]);
void quickSort(int r[], int, int);

//Declares the array to be sorted, as well as the const to represent the size of the array
int r[100];
const int SIZE = (sizeof(r)/4);

//Method to print out values of the array
void printArray(int r[])
    int i=0;

//Method to swap the values at the locations of the 2 int args, within the array provided
void switchValues(int r[],int leftSide, int rightSide){
    int temp = r[leftSide];
    r[leftSide] = r[rightSide];
    r[rightSide] = temp;

//Method which will randomize an array of the size of the const int SIZE
void randomArray(int r[]){
    for(int rep = 0; rep < SIZE; rep++){
        r[rep] = int (rand()%100);

//Quicksort method
void quicksort(int r[], int leftSide, int rightSide){
    int leftTemp = leftSide;
    int rightTemp = rightSide;
    int pivot = r[(leftSide+rightSide)/2];
            switchValues(r, leftTemp, rightTemp);

                quicksort(r, leftSide, rightTemp);

//Main method for testing the quicksort
int main() {
    cout << "Generating random array!" << endl;
    cout << "The unsorted array: ";
    cout << endl << endl;
    cout << "Sorting in progress..." << endl;
    const clock_t beginTime = clock();
    quicksort(r, 0, SIZE);
    float totalTime = ( clock () - beginTime ) /  CLOCKS_PER_SEC;
    cout << "The sorted array: ";
    cout << endl;
    cout << "The sort took " << totalTime << " milliseconds!" << endl;
    return 0;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s