Quick Sort : Sort « Development « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Development » Sort 
5.22.3.Quick Sort
#include <iostream>
using namespace std;

int *swapnumbers(int rawdata[]int lower, int upper);
int *quicksort(int rawdata[]int first, int last);

int main()
{
    int unsorted[10],i;
    int *sorted;

    unsorted[09;
    unsorted[18;
    unsorted[27;
    unsorted[37;
    unsorted[46;
    unsorted[55;
    unsorted[65;
    unsorted[74;
    unsorted[84;
    unsorted[92;

    sorted = quicksort(unsorted, 010);

    cout << "This is your array sorted:"<< endl;
    for(i = 0; i < 10; i++)
    {
        cout << sorted[i<< endl;
    }

    return 0;
}

int *quicksort(int rawdata[]int first, int last)
{
    int lower = first+1, upper = last;
    int bound;

    rawdata = swapnumbers(rawdata, first, (first+last)/2);
    bound = rawdata[first];

    while(lower <= upper)
    {
        while(rawdata[lower< bound)
            lower++;

        while(bound < rawdata[upper])
            upper--;

        if(lower < upper)
        {
            rawdata = swapnumbers(rawdata, lower++, upper--);
        }
        else
        {
            lower++;
        }
    }

    rawdata = swapnumbers(rawdata, upper, first);

    if(first < upper-1)
    {
        rawdata = quicksort(rawdata, first, upper-1);
    }
    if(upper+< last)
    {
        rawdata = quicksort(rawdata, upper+1, last);
    }
    return(rawdata);
}


int *swapnumbers(int rawdata[]int lower, int upper)
{
    int temp;
    temp = rawdata[lower];
    rawdata[lower= rawdata[upper];
    rawdata[upper= temp;
    return(rawdata);
}
This is your array sorted:
0
2
4
4
5
5
6
7
7
8
5.22.Sort
5.22.1.A Bubble sort
5.22.2.A recursive version of Quicksort for sorting characters
5.22.3.Quick Sort
5.22.4.how to declare your own function and function pointer to be used with qsort( )
5.22.5.Sort Tracer
5.22.6.Selection Sort
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.