+ Reply to Thread
Results 1 to 9 of 9

Thread: Array Sorting Algorithms III

  1. #1
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Array Sorting Algorithms III

    This tutorial is another installment of the "Array Sorting Algorithms" series. This installment will teach you one of the fastest methods of sorting arrays and arrays.

    In the second installment of the series, Array Sorting Algorithms II, I showed the selection sort algorithm as a replacement for the simpler bubble sort. The selection sort is great, but lets say you really need speed. You need your array sorted as fast as possible. Then you need the Quick sort algorithm. Quicksort works by using a "divide-and-conquer" strategy. Lets take a look at the pseudo code for quick sort (taken from wikipedia).

    Code:
     function quicksort(array)
         var list less, greater
         if length(array) = 1  
             return array  
         select and remove a pivot value pivot from array
         for each x in array
             if x = pivot then append x to less
             else append x to greater
         return concatenate(quicksort(less), pivot, quicksort(greater))
    The quick sort code is similar in difficulty to the selection sort. Open a new C++ file and enter the following code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void quicksort(int[]);
    
    int main(void)
    {
        return 0;
    }
    
    void quicksort(int[] unsorted)
    {
    
    }
    Right now we have the basic information included for what we want to do. We include iostream and the std namespace as well as an empty main statement. We also declared a prototype for our sort function and started it off as an empty function. Now lets fill in our sort algorithm function based on the pseudo code.

    Code:
        int i = left; j = right;
        int tmp;
        int pivot = unsorted[(left + right) / 2];
        
        //Partition function: divide array in half
        
        while(i <= j)
        {
            while(unsorted[i] < pivot)
                i++;
            while(unsorted[j] >pivot)
                j--;
            if (i <= j)
            {
                tmp = unsorted[i];
                unsorted[i] = unsorted[j];
                unsorted[j] = tmp;
                i++;
                j--;
            }
        }
        
        if (left < j)
            quicksort(unsorted, left, j);
        if (i < right)
            quicksort(unsorted, i, right);
    The function starts by declaring a few variables. A start and end to the sort area, a temporary variable for when two numbers need to be switched, and a pivot point. Then we move onto the partition function which divides the array into two sections and sorts them if necessary. After that, based on whether the array needs to be re-partitioned and more sorting needs to occur, the function calls itself recursively. If no sorting needs to occur, the sorted array is returned.

    To test it out, lets fill in the main function. Type in the following code inside the brackets of the main function.

    Code:
        int unsorted[5];
        
        for(int i = 0; i < 5; i++)
        {
             cout << "Enter element number " << i + 1 << " of the array to be sorted: ";
             cin >> unsorted[i];
        }
        
        quicksort(unsorted, 0, 4);
        
        cout << endl << endl << "The sorted array is: ";
        for(int i = 0; i < 5; i++)
        {
             cout << unsorted[i];
             if (i != 4) cout << ", ";
        }
        
        cout << endl << endl;
        system("pause");     
        return 0;
    Now compile the program, input 5 unsorted numbers, and the algorithm will output the sorted data. I wrote this program using arrays because I could not quite get the quicksort algorithm to work with vectors. I think it has something to do with the recursive nature of the algorithm and premature returning. If anyone can solve the problem, please post in the comments or in your own thread on this forum . This code was written using Dev-C++, so it may need some tweaking in other compilers. The source code for this project is attached for convenience. If you would like to contribute to the information presented here, or would like to offer corrections, please post a reply.

    Source Code: quicksort.zip (had a bit of trouble trying to attach to the post, so I decided to just host it at mediafire).

  2. #2
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,780
    Blog Entries
    40

    Re: Array Sorting Algorithms III

    You sort those arrays!

    ++whitey6993.rep;
    Should I get a userbar here?

  3. #3
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Re: Array Sorting Algorithms III

    Thank you

  4. #4
    Administrator James.H is on a distinguished road James.H's Avatar
    Join Date
    Nov 2009
    Location
    London
    Posts
    694
    Blog Entries
    1

    Re: Array Sorting Algorithms III

    Great tut! +rep

  5. #5
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Re: Array Sorting Algorithms III

    Thanks!

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    Re: Array Sorting Algorithms III

    Very nice. +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Re: Array Sorting Algorithms III

    Thanks

  8. #8
    Learning Programmer genux will become famous soon enough genux's Avatar
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    57

    Re: Array Sorting Algorithms III

    very nice tut+
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  9. #9
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Re: Array Sorting Algorithms III

    Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Need help abut understanding array
    By yonghan in forum PHP Forum
    Replies: 5
    Last Post: 10-08-2009, 11:38 PM
  2. Array Sorting Algorithms II
    By whitey6993 in forum C Tutorials
    Replies: 4
    Last Post: 12-30-2008, 12:26 PM
  3. Array Sorting Algorithms I
    By whitey6993 in forum C Tutorials
    Replies: 2
    Last Post: 12-30-2008, 11:24 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts