JavaScript
  Home arrow JavaScript arrow 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 
Sun Developer Network 
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: 3 stars3 stars3 stars3 stars3 stars / 2
    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


    (Page 1 of 6 )

    This is the second part of a two-part article that explains how to use Quicksort. It is a method of sorting a list of elements and putting them in the correct order. Please make sure you have read and understood the first article before you read this one.

    Picking up where we left off in the previous article, we have to obtain a pivot and then put all elements that are less than the pivot on the left side and all elements that are greater than the pivot on the right side.

    We start by comparing the left-most and right-most elements. If the left-most element is greater than the right-most element, then swap them. This gives the following:


    Q

    W

    E

    R

    T

    Y

    U

    I

    O

    P

    i









    j


    P

    W

    E

    R

    T

    Y

    U

    I

    O

    Q

    i









    j



    For simplicity, we shall be calling the left-most element the left element, and the right-most element the right element.

    The present left element will become the pivot for the list. We arrive here after the swapping phase. We then increase the value of i continuously until we come across an element that is greater than the left element. We reduce the value of j continuously until we come across an element that is less than the same left element; remember that the left element will become the pivot. The ith and the jth element so found are in the wrong positions, so we swap them. This gives rise to the following:


    P

    W

    E

    R

    T

    Y

    U

    I

    O

    Q


    i







    j



    P

    O

    E

    R

    T

    Y

    U

    I

    W

    Q


    i







    j



    We continue to increase the value of i and decrease the value of j to find another pair of elements that are in the wrong positions, and then swap. This leads to:


    P

    W

    E

    R

    T

    Y

    U

    I

    O

    Q




    i




    j




    P

    O

    E

    I

    T

    Y

    U

    R

    W

    Q




    i




    j




    We have to continue increasing the value of i, decreasing the value of j and swapping until the index variables reach the right (end) and left (start) elements. However, as soon as the index variables cross, it means we have finished swapping -- all the elements that should be on the left, and all the elements that should be on the right, are in their appropriate positions (or at least on their correct sides).

    More JavaScript Articles
    More By Chrysanthus Forcha


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

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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