JavaScript
  Home arrow JavaScript arrow Page 3 - QuickSort in Action
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 in Action
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-10-27

    Table of Contents:
  • QuickSort in Action
  • Swapping Elements
  • Tackling a Sub-list
  • And Then There was One
  • Code for Quicksort
  • Sorting Strings

  • 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 in Action - Tackling a Sub-list


    (Page 3 of 6 )

    We compare the left and right elements of the sub-list and then swap if necessary. The situation is as follows:


    I

    O

    E

    i


    j


    E

    O

    I

    i


    j


    The left element, E, will be the pivot for the sub-list. We now increment i and decrement j under the same conditions as we did above. We end up with the following situation:


    E

    O

    I

    j

    i



    O is the first element for the variable index i that is higher than E. There is no element for the index variable j that is lower than E, so j ends at the position of E, the first position. We now exchange the left element, E, with the element of the position for j, which is still E. So E is exchanged with itself. The new value for the index variable k is now 0, which is the final position for E in the main list. The sub-list can now be written as:


    (E0) [O I]


    There is only one sub-list now, the right sub-list; there is no left sub-list. Whenever the computer (sorting program) arrives at the situation where there is no left sub-list, it starts sorting the immediate right sub-list, which in our case is [O I].

    There are only two elements in the sub-list. We compare the left and right elements of the sub-list and then swap if necessary. The situation is as follows:


    O

    I

    i

    j


    I

    O

    i

    j


    The left element, I, is the pivot for the sub-list. We now increment i and decrement j under the same conditions as we did above. We end up with the following:


    I

    O

    j

    i


    O is the first element for the variable index i that is higher than I. There is no element for the index variable j that is lower than I, so j ends at the position of I. We now exchange the left element, I, with the element of the position for j, which is still I. So I is exchanged with itself. The new value for the index variable k is now 1, the position for I in the main list. The sub-list can now be written as:

    (I1) [O]

    Again, there is only one sub-list, the right sub-list; there is no left sub-list. Remember, whenever the computer (sorting program) arrives at the situation where there is no left sub-list, it starts sorting the immediate right sub-list, which in our case is [O]. There is only one element in this sub-list.

    More JavaScript Articles
    More By Chrysanthus Forcha


       · Thanks for stopping by to read my article. If you have any comments, questions, or...
     

    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