JavaScript
  Home arrow JavaScript arrow Page 4 - Quicksort
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Quicksort
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-10-20

    Table of Contents:
  • Quicksort
  • Tutorial Approach
  • The Quicksort Algorithm
  • Quicksort by Observation
  • Computer Implementation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Quicksort - Quicksort by Observation


    (Page 4 of 5 )

    Let us sort the list above by observation, before we see how to program it on the computer. 

    The list is:


    Q W E R T Y U I O P


    Let us make R the pivot. We can equally make Q, whose rightful spot is position 4, the pivot. We simple have to choose something for the central position. So by choosing R and exchanging the positions of R and Y, we have:


    Q W E Y T R5 U I O P


    When any element obtains its rightful position, we shall indicate that with a superscript as in the case of R above.

    We now have to see that all the elements to the left of R are less than R, and that all the elements to the right of R are greater than R, by exchanging pairs. The following pairs can be exchanged: W and P, and Y and O. Note that you can still exchange W and O, and Y and P. Exchanging W for P and Y for O, we have


    [Q P E O T] (R5) [U I Y W] ------------result I


    We will indicate the sub-lists with square brackets, and the pivot with normal brackets. 

    If you chose O as the pivot for the left sub-list and repeated the first and second steps in the algorithm, you would have something like this:


    [I E] (O2) [P Q] ------------result IIa


    If we do a similar thing to the right sub-list above we shall have something like:


    [T] (U7) [Y W]


    Now when a non-pivot element appears alone in a sub-list, we know that the element has arrived at its rightful position. So we have


    [T6] (U7) [Y W] ------------result IIb


    The state at which we have sorted the whole list by observation, so far, is


    [I E] (O2) [P Q] (R5) [T6] (U7) [Y W] --------result III


    Note that some elements have taken their final (rightful) positions. Any element that is a pivot, obtains its rightful position. Any element that is alone in a sub-list also obtains its rightful position.

    You simply have to continue by repeating the first and second steps of the algorithm on the sub-lists. Let us consider the first sub-list, [I E] of result III.

    There are only 2 elements here. Let us choose E as the pivot. Putting E in its rightful position by exchanging it with “I”, we have:


    (E0) [I]


    Now, the position number of E in the main list is zero; remember that array counting begins from zero. There is only one sub-list, the right sub-list. It has just one element, so the element has arrived at its proper position, which is position 1. The former sub-list is:


    (E0) [I1] --------------result IVa


    You can carry out a similar analysis for the other sub-lists and come out with the complete sorted list of


    (E0) [I1] (P2) [Q3] (R5) [T6] (U7) [W8] (Y9) -----result V


    Your brackets may not match my brackets, but the order should be the same.

    More JavaScript Articles
    More By Chrysanthus Forcha


       · It is good to understand the operation of the Quicksort function. I did this in...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT