Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 9:06 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Quick Sort
PostPosted: Thu Nov 13, 2008 3:05 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
#include <iostream>

using namespace std;

/*
* Program to read a list of integers from standard input and print them in
* sorted order.
*
* Author: Tom Bennet
*/

/*
* Swap function for sorting.
*/
void swap(int &a, int &b)
{
        int tmp;                // Exchange temp value.

        tmp = a;
        a = b;
        b = tmp;       
}

/*
* Partition function for the recursive quicksort.  It takes a pointer
* to the start of the array, and its size, and partitions it, returning a
* the location of the split point.
*/
int split(int arr[], int size)
{
        int splitval = arr[0];  // Use first value as split value.
        int left = 1;           // Left (low) end scanner.
        int right = size - 1;   // Right (high) end scanner.

        while(1)
        {
                // Scan tward middle until you find a items which are out
                // of place at each end.
                while(left <= right && arr[left] <= splitval)
                        left++;
                while(splitval < arr[right])
                        right--;

                // If they passed each other, we are done.  Otherwise,
                // swap the elements and try again.
                if(left > right)
                        break;
                else
                        swap(arr[left], arr[right]);
        }

        /* Move the pivot into place, and return. */
        swap(arr[0], arr[right]);

        return right;
}

/*
* Recursive quicksort.  It takes a pointer to the data and a size,
* and it sorts the data.
*/
void sort(int data[], int size)
{
        if(size > 1)
        {
                /* Split the array, and recursively sort each part. */
                int splitloc = split(data, size);
                sort(data, splitloc);
                sort(data + splitloc + 1, size - splitloc - 1);
        }
}

/*
* Main program.  Reads the integers into an array, sorts them, and
* prints them out.
*/
const int MAX_NUM_INTS = 100;
int main()
{
        int ints[MAX_NUM_INTS];         // Where the numbers go.
               
        // Read them in.
        int i;
        for(i = 0; i < MAX_NUM_INTS && cin >> ints[i]; ++i);
        int numints = i;

        // Sort them.
        sort(ints, numints);

        // Print them.
        cout << "==================" << endl;
        for(int i = 0; i < numints; ++i)
                cout << ints[i] << endl;
        cout << "==================" << endl;
}


Integer Quick Sort


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Library Sort
 sort words in c++
 Help with selection sort for strings?
 Array sort
 Sort a list
 String Sort
 C++ Selection Sort
 cisco press - Cisco ccvp cvoice quick reference sheets - sep
 Ccnp Quick Reference Sheets
 Ccnp Iscw Quick Reference Sheets

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team