0

I am trying to make a flow chart for my recursive quick sort. I have got so far but not sure if right.

I am trying to show the detail in just a simple flowchart without any code in the flowchart.

This is what I have made so far but I am not sure it is right. What I am trying to find is that how and when does a quick sort know that it has been sorted. Then I can alter the text in the decision box in the flowchart to make more sense.

enter image description here

Here is my code with commenting if of any relevance.

#include <iostream>
using namespace std;
//this variable is the size of the array (amount of numbers in sort)
int const SIZE = 5;                               


// This function swaps two numbers, arguments for these 2 numbers   
void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}

// This function prints an array, Arguments for the array to be printed and number of elements in array
void PrintArray(int* array, int n)
{
    int i;

    for( i = 0; i < n; i++) cout<<array[i]<<',';
}

// This function splits the array around the pivot
// Arguments; array to be split, pivot to be returned, first element and last element
int SplitArray(int* array, int pivot, int startIndex, int endIndex)
{
    int leftBoundary = startIndex;
    int rightBoundary = endIndex;
    //shuffle pivot until both the boundaries meet
    while(leftBoundary < rightBoundary)            
    {
        //keep moving until a lesser element is found or until the leftBoundary is reached
         while( pivot < array[rightBoundary]          
                && rightBoundary > leftBoundary)      
         {
             //shift left through the split section
              rightBoundary--;                      
         }
         swap(array[leftBoundary], array[rightBoundary]);
         //keep moving through the array until a greater or equal element is found or until the section end is reached
         while( pivot >= array[leftBoundary]          
                && leftBoundary < rightBoundary)     
         {
             //shift right through the split section
              leftBoundary++;                        
         }
         swap(array[rightBoundary], array[leftBoundary]);
    }
    //returns the split point of the array because the left and right boundary are equal
    return leftBoundary;                              
}

//This function quicksorts the data with arguments for array and its first and last element
void QuickSort(int* array, int startIndex, int endIndex)
{
    //set the pivot as the start of split array
    int pivot = array[startIndex];                  
    int splitPoint;
    //if they are equal then there is only one element and the sorting has finished
    if(endIndex > startIndex)                        
    {
        //gets the position of the pivot and sets data in that position to pivot variable
        splitPoint = SplitArray(array, pivot, startIndex, endIndex);
        array[splitPoint] = pivot;
        //**RECURSION - Quick sort first half of the array (left of pivot point)
        QuickSort(array, startIndex, splitPoint-1);  
        //**RECURSION - Quick sort second half of the array (right of pivot point)
        QuickSort(array, splitPoint+1, endIndex);    
    }
}
//beggining of main program, makes use of the functions declared before this
int main()
{
    int array[SIZE];
    //input unsorted array elements
    cout << "This program demonstrates a quick sort using a recursive algorithm" << endl;
    for(int i = 0; i < SIZE; i++)
    {
         cout<<"Enter an integer : ";
         cin>>array[i];
    }
    //output the unsorted list
    cout<<endl<<"The list you input is : "<<endl;
    PrintArray(array, SIZE);
    //sort array from first to last element and output the sorted list
    QuickSort(array,0,SIZE - 1);
    cout<<endl<<"The list has been sorted, now it is : "<<endl;
    PrintArray(array,SIZE);
    //refresh the input stream and 
    cin.sync();
    cin.get();
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Luke
  • 317
  • 2
  • 6
  • 17
  • 2
    Your flowchart seems to be missing the merge step that comes after the `batch size = 1` step – SirGuy Dec 18 '13 at 20:54
  • 1
    http://stackoverflow.com/questions/6691913/how-to-represent-a-recursive-function-with-a-flow-chart – Timothy Dec 18 '13 at 20:54
  • @GuyGreer I assume that is the step where the batches are merged together to form the full sorted array? – Luke Dec 18 '13 at 20:59
  • Yes, it happens at each recursive level after the recursive call. – SirGuy Dec 18 '13 at 21:02
  • Is that in between the loop back to batch size = 1 and recursion? – Luke Dec 18 '13 at 22:03
  • It may be clearer to split into two flow charts, one for just the recursive function whose mission is to sort an array slice, separate from the one for the program that collects input, calls the recursive function to sort the complete array slice, and prints results. – Patricia Shanahan Dec 19 '13 at 00:13

0 Answers0