Quicksort

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
/*
#include 
#include 
#include 



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;
    while(i<SIZE){
        cout<<r[i]<<",";
        i++;
    }
}


//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];
    
    while(leftSidepivot)
            rightTemp--;
        
        if(leftTemp<=rightTemp){
            switchValues(r, leftTemp, rightTemp);
            leftTemp++;
            rightTemp--;

        }
        else{
            if(leftSide<rightTemp)
                quicksort(r, leftSide, rightTemp);
            if(leftTemp<rightSide)
                quicksort(r,leftTemp,rightSide);
            return;
        }
    }
}


//Main method for testing the quicksort
int main() {
  
    cout << "Generating random array!" << endl;
    randomArray(r);
    
    cout << "The unsorted array: ";
    printArray(r);
    
    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: ";
    printArray(r);
    cout << endl;
    
    cout << "The sort took " << totalTime << " milliseconds!" << endl;
    
    return 0;
}

Game Sample: Converse Method

This is a method from a game I worked on. The method takes 6 strings, 1 representing a possible prompt, and then the others are possible responses, and returns the char selected by the user.

This method is used in my text based game to format dialogue, take a response, and return the letter answered. The method can be sent empty strings for answers which are not relevant, and will dispose of them, not allowing a user to pick them as an answer.

Screen Shot 2018-01-28 at 11.39.59 AMScreen Shot 2018-01-28 at 11.43.15 AM

char converse(string statement, string responseA, string responseB, string responseC, string responseD, string responseE){

    //char to be returned, representing answer. then sleeps to create pause
    char answer;
    sleep(3);

    //bools to represent that the letter chosen is valid
    bool answerB = false;
    bool answerC = false;
    bool answerD = false;
    bool answerE = false;

    //question posed
    cout << endl << endl << statement << endl << endl << endl;

    //possible responses
    cout << "A. " << responseA << endl;           //if there are multiple possible responses, this will trigger and set the corresponding bool to true. //B if(responseB.length() > 0){
        cout << "B. " << responseB << endl; answerB = true; } //C if(responseC.length() > 0){
        cout << "C. " << responseC << endl; answerC = true; } //D if(responseD.length() > 0){
        cout << "D. " << responseD << endl; answerD = true; } //E if(responseE.length() > 0){
        cout << "E. " << responseE << endl; answerE = true;     } //loop to allow user to make invalid entry while(true){ //stores user's answer in "answer" char cin >> answer;
        answer = tolower(answer);
//swtich statement to return user's response
        switch(answer){
            //if user answers:
            case 'a':
                return 'a';
                break;
            //if user answers:
            case 'b':
                if(answerB == true) return 'b';
                else break;
            //if user answers:
            case 'c':
                if(answerC == true) return 'c';
                else break;
            //if user answers:
            case 'd':
                if(answerD == true) return 'd';
                else break;
            //if user answers:
            case 'e':
                if(answerE == true) return 'e';
                else break;
            //otherwise, loop again
            default:
                break;
        }

        cout << "INVALID RESPONSE. Please choose a valid response" <<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>< endl;
    }
}
</pre>

Game Sample: Speak Method

This method is a small part of a game I worked on. The purpose of the method is to create a life-like output of words, to mimic dialogue and feel more realistic when characters are communicating.

The method takes a string which is the words to be output, an int which represents, in milliseconds, the pause between each word, and another string, speaker, which is the name of the person speaking.

This method works in a fascinating way and really is able to break down a barrier which is often present in text based games, the lack of realistic interaction.

I am not sure if I will post the entirety of this adventure on my site, but I will continue to update on methods which go into the game.

Screen Shot 2018-01-28 at 11.39.59 AM

<pre>
/*
    Method which creates life like speech by dividing up and outputting single words seperated by intervals
 */

void speak(string statement, int pause, string speaker){
    cout << endl;
    //temp string and stringstream are created
    string temp;
    stringstream ss(statement);
    cout << speaker << ":   ";          //Creates pauses between the output of words     while(ss >> temp){
        std::this_thread::sleep_for(std::chrono::milliseconds(pause));
        cout << temp << " " << flush;
    }
}

</pre>