+ Reply to Thread
Results 1 to 9 of 9

Thread: Array Sorting Algorithms III

  1. #1
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Array Sorting Algorithms III

    You sort those arrays!

    ++whitey6993.rep;
    Wow I changed my sig!

  4. #3
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Array Sorting Algorithms III

    Thank you

  5. #4
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    14

    Re: Array Sorting Algorithms III

    Great tut! +rep

  6. #5
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Array Sorting Algorithms III

    Thanks!

  7. #6
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Array Sorting Algorithms III

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

  8. #7
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Array Sorting Algorithms III

    Thanks

  9. #8
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: Array Sorting Algorithms III

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

  10. #9
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    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. Array Sorting Algorithms V
    By whitey6993 in forum C Tutorials
    Replies: 1
    Last Post: 06-29-2011, 11:33 AM
  2. Array Sorting Algorithms I
    By whitey6993 in forum C Tutorials
    Replies: 4
    Last Post: 11-08-2010, 04:13 PM
  3. Array Sorting Algorithms IV
    By whitey6993 in forum C Tutorials
    Replies: 3
    Last Post: 03-30-2010, 10:30 AM
  4. Array Sorting Algorithms II
    By whitey6993 in forum C Tutorials
    Replies: 4
    Last Post: 12-30-2008, 10:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

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