Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code

The QuickSort Algorithm


As one of the more advanced sorting algorithms, you might think that the Quicksort Algorithm is steeped in complicated theoretical background, but this is not so. Like Insertion Sort, this algorithm has a fairly simple concept at the core, but is made complicated by the constraints of the array structure.
menu

- Articles
featured articles

- QuickSort Tutorial - Linked Lists Tutorial

The basic concept is to pick one of the elements in the array as a pivot value around which the other elements will be rearranged. Everything less than the pivot is moved left of the pivot (into the left partition). Similarly, everything greater than the pivot goes into the right partition. At this point each partition is recursively quicksorted. The Quicksort algorithm is fastest when the median of the array is chosen as the pivot value. That is because the resulting partitions are of very similar size. Each partition splits itself in two and thus the base case is reached very quickly. In practice, the Quicksort algorithm becomes very slow when the array passed to it is already close to being sorted. Because there is no efficient way for the computer to find the median element to use as the pivot, the first element of the array is used as the pivot. So when the array is almost sorted, Quicksort doesn't partition it equally. Instead, the partitions are lopsided like in Figure 2. This means that one of the recursion branches is much deeper than the other, and causes execution time to go up. Thus, it is said that the more random the arrangement of the array, the faster the Quicksort Algorithm finishes.

http://www.mycstutorials.com/articles/sorting/quicksort

1/6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code

Figure 1: The ideal Quicksort on a random array.

http://www.mycstutorials.com/articles/sorting/quicksort

2/6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code

Figure 2: Quicksort on an already sorted array. These are the steps taken to sort an array using QuickSort:
E l e m e n t si nr e di n d i c a t es w a p s . E l e m e n t si nb l u ei n d i c a t ec o m p a r i s o n s . S p e c i a lc o m m e n t a r yi si ng r e e n . 314592687 C a l l i n gq u i c k S o r to ne l e m e n t s0t o8 D e f i n i t i o n s :a" s m a l l "e l e m e n ti so n ew h o s ev a l u ei sl e s st h a n o re q u a lt ot h ev a l u eo ft h ep i v o t .L i k e w i s e ,a" l a r g e "e l e m e n t i so n ew h o s ev a l u ei sl a r g e rt h a nt h a to ft h ep i v o t . A tt h eb e g i n n i n g ,t h ee n t i r ea r r a yi sp a s s e di n t ot h eq u i c k s o r t f u n c t i o na n di se s s e n t i a l l yt r e a t e da so n el a r g ep a r t i t i o n . A tt h i st i m e ,t w oi n d i c e sa r ei n i t i a l i z e d :t h el e f t t o r i g h t s e a r c hi n d e x ,i ,a n dt h er i g h t t o l e f ts e a r c hi n d e x ,k .T h ev a l u e o fii st h ei n d e xo ft h ef i r s te l e m e n ti nt h ep a r t i t i o n ,i nt h i s c a s e0 ,a n dt h ev a l u eo fki s8 ,t h ei n d e xo ft h el a s te l e m e n ti n t h ep a r t i t i o n .T h er e l e v a n c eo ft h e s ev a r i a b l e sw i l lb em a d ea p p a r e n t i nt h ec o d eb e l o w . 314592687 T h ef i r s te l e m e n ti nt h ep a r t i t i o n ,3 ,i sc h o s e na st h ep i v o t e l e m e n t ,a r o u n dw h i c ht w os u b p a r t i t i o n sw i l lb ec r e a t e d .T h ee n d g o a li st oh a v ea l lt h es m a l le l e m e n t sa tt h ef r o n to ft h ep a r t i t i o n , i nn op a r t i c u l a ro r d e r ,f o l l o w e db yt h ep i v o t ,f o l l o w e db yt h el a r g e e l e m e n t s . T od ot h i s ,q u i c k s o r tw i l ls c a nr i g h t w a r d sf o rt h ef i r s tl a r g e e l e m e n t .O n c et h i si sf o u n d ,i tw i l ll o o kf o rt h ef i r s ts m a l l e l e m e n tf r o mt h er i g h t .T h e s et w ow i l lt h e nb es w a p p e d . S i n c eii sc u r r e n t l ys e tt oz e r o ,t h ep i v o ti sa c t u a l l yc o m p a r e d t oi t s e l fi nt h es e a r c ho ft h ef i r s tl a r g ee l e m e n t . 314592687 T h es e a r c hf o rt h ef i r s tl a r g ee l e m e n tc o n t i n u e sr i g h t w a r d s .T h e v a l u eo fig e t si n c r e m e n t e da st h es e a r c hm o v e st ot h er i g h t . 314592687 S i n c e4i sg r e a t e rt h a nt h ep i v o t ,t h er i g h t w a r d ss e a r c h s t o p sh e r e .T h u st h ev a l u eo fir e m a i n s2 . 314592687 N o w ,s t a r t i n gf r o mt h er i g h te n do ft h ea r r a y ,q u i c k s o r ts e a r c h e s f o rt h ef i r s ts m a l le l e m e n t .A n ds oki sd e c r e m e n t e dw i t he a c h s t e pl e f t w a r d st h r o u g ht h ep a r t i t i o n . 314592687 314592687 314592687 S i n c e2i sn o tg r e a t e rt h a nt h ep i v o t ,t h el e f t w a r d ss e a r c hc a ns t o p . 312594687 N o we l e m e n t s4a n d2( a tp o s i t i o n s2a n d5 ,r e s p e c t i v e l y )a r es w a p p e d . 312594687 http://www.mycstutorials.com/articles/sorting/quicksort

3/6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code 312594687 N e x t ,t h er i g h t w a r d ss e a r c hr e s u m e sw h e r ei tl e f to f f :a tp o s i t i o n2 ,w h i c h i ss t o r e di nt h ei n d e xi .


312594687 I m m e d i a t e l yal a r g ee l e m e n ti sf o u n d ,a n dt h er i g h t w a r d ss e a r c hs t o p s w i t hib e i n ge q u a lt o3 . 312594687 N e x tt h el e f t w a r d ss e a r c h ,t o o ,r e s u m e sw h e r ei tl e f to f f :kw a s5 s ot h ee l e m e n ta tp o s i t i o n5i sc o m p a r e dt ot h ep i v o tb e f o r ek i sd e c r e m e n t e da g a i ni ns e a r c ho fas m a l le l e m e n t . 312594687 T h i sc o n t i n u e sw i t h o u ta n ym a t c h e sf o rs o m et i m e . . . 312594687 312594687 T h es m a l le l e m e n ti sf i n a l l yf o u n d ,b u tn os w a pi sp e r f o r m e ds i n c e a tt h i ss t a g e ,ii se q u a lt ok .T h i sm e a n st h a ta l lt h es m a l l e l e m e n t sa r eo no n es i d eo ft h ep a r t i t i o na n da l lt h el a r g ee l e m e n t sa r e o nt h eo t h e r . 213594687 O n l yo n et h i n gr e m a i n st ob ed o n e :t h ep i v o ti ss w a p p e dw i t ht h ee l e m e n t c u r r e n t l ya ti .T h i si sa c c e p t a b l ew i t h i nt h ea l g o r i t h mb e c a u s ei t o n l ym a t t e r st h a tt h es m a l le l e m e n tb et ot h el e f to ft h ep i v o t ,b u tt h e i r r e s p e c t i v eo r d e rd o e s n ' tm a t t e r .N o w ,e l e m e n t s0t o( i-1 )f o r mt h e l e f tp a r t i t i o n( c o n t a i n i n ga l ls m a l le l e m e n t s )a n de l e m e n t sk+1o n w a r d f o r mt h er i g h tp a r t i t i o n( c o n t a i n i n ga l ll a r g ee l e m e n t s . C a l l i n gq u i c k S o r to ne l e m e n t s0t o1 T h er i g h tp a r t i t i o ni sp a s s e di n t ot h eq u i c k s o r tf u n c t i o n . 213594687 2i sc h o s e na st h ep i v o t . I ti sa l s oc o m p a r e dt oi t s e l fi nt h es e a r c hf o ras m a l le l e m e n tw i t h i n t h ep a r t i t i o n . 213594687 T h ef i r s t ,a n di nt h i sc a s eo n l y ,s m a l le l e m e n ti sf o u n d . 213594687 S i n c et h ep a r t i t i o nh a so n l yt w oe l e m e n t s ,t h el e f t w a r d ss e a r c h b e g i n sa tt h es e c o n de l e m e n ta n df i n d s1 . 123594687 T h eo n l ys w a pt ob em a d ei sa c t u a l l yt h ef i n a ls t e pw h e r et h ep i v o t i si n s e r t e db e t w e e nt h et w op a r t i t i o n s .I nt h i sc a s e ,t h el e f tp a r t i t i o n h a so n l yo n ee l e m e n ta n dt h er i g h tp a r t i t i o nh a sz e r oe l e m e n t s . C a l l i n gq u i c k S o r to ne l e m e n t s0t o0 N o wt h a tt h el e f tp a r t i t i o no ft h ep a r t i t i o na b o v ei sq u i c k s o r t e d : t h e r ei sn o t h i n ge l s et ob ed o n e C a l l i n gq u i c k S o r to ne l e m e n t s2t o1 T h er i g h tp a r t i t i o no ft h ep a r t i t i o na b o v ei sq u i c k s o r t e d . I nt h i sc a s et h es t a r t i n gi n d e xi sg r e a t e rt h a nt h ee n d i n gi n d e xd u et ot h e w a yt h e s ea r eg e n e r a t e d :t h er i g h tp a r t i t i o ns t a r t so n ep a s tt h ep i v o t o fi t sp a r e n tp a r t i t i o na n dg o e su n t i lt h el a s te l e m e n to ft h ep a r e n t p a r t i t i o n .S oi ft h ep a r e n tp a r t i t i o ni se m p t y ,t h ei n d i c e sg e n e r a t e d w i l lb eo u to fb o u n d s ,a n dt h u sn oq u i c k s o r t i n gw i l lt a k ep l a c e . C a l l i n gq u i c k S o r to ne l e m e n t s3t o8 T h er i g h tp a r t i t i o no ft h ee n t i r ea r r a yi sn o wb e i n gq u i c k s o r t e d 5i sc h o s e na st h ep i v o t . 123594687 123594687 T h er i g h t w a r d ss c a nf o ral a r g ee l e m e n ti si n i t i a t e d . 9i si m m e d i a t e l yf o u n d . 123594687 T h u s ,t h el e f t w a r d ss e a r c hf o ras m a l le l e m e n tb e g i n s . . . 123594687 123594687 123594687 A tl a s t ,4i sf o u n d .N o t ek=5 .

123549687 http://www.mycstutorials.com/articles/sorting/quicksort

4/6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code 123549687 T h u st h ef i r s tl a r g ea n ds m a l le l e m e n t st ob ef o u n da r es w a p p e d .


123549687 T h er i g h t w a r d ss e a r c hf o ral a r g ee l e m e n tb e g i n sa n e w . 123549687 N o wt h a ti th a sb e e nf o u n d ,t h er i g h t w a r ds e a r c hc a ns t o p . 123549687 S i n c ekw a ss t o p p e da t5 ,t h i si st h ei n d e xf r o mw h i c ht h e l e f t w a r ds e a r c hr e s u m e s . 123549687 123459687 T h el a s ts t e pf o rt h i sp a r t i t i o ni sm o v i n gt h ep i v o ti n t ot h er i g h ts p o t . T h u st h el e f tp a r t i t i o nc o n s i s t so n l yo ft h ee l e m e n ta t3a n dt h er i g h tp a r t i t i o ni s s p a n sp o s i t i o n s5t o8i n c l u s i v e . C a l l i n gq u i c k S o r to ne l e m e n t s3t o3 T h el e f tp a r t i t i o ni sq u i c k s o r t e d( a l t h o u g hn o t h i n gi sd o n e . C a l l i n gq u i c k S o r to ne l e m e n t s5t o8 T h er i g h tp a r t i t i o ni sn o wp a s s e di n t ot h eq u i c k s o r tf u n c t i o n . 123459687 9i sc h o s e na st h ep i v o t . 123459687 T h er i g h t w a r ds e a r c hf o ral a r g ee l e m e n tb e g i n s .

123459687 123459687 N ol a r g ee l e m e n ti sf o u n d .T h es e a r c hs t o p sa tt h ee n do ft h ep a r t i t i o n . 123459687 T h el e f t w a r d ss e a r c hf o ras m a l le l e m e n tb e g i n s ,b u td o e sn o tc o n t i n u e s i n c et h es e a r c hi n d i c e sia n dkh a v ec r o s s e d . 123457689 T h ep i v o ti ss w a p p e dw i t ht h ee l e m e n ta tt h ep o s i t i o nk :t h i si s t h el a s ts t e pi ns p l i t t i n gt h i sp a r t i t i o ni n t ol e f ta n dr i g h ts u b p a r t i t i o n s . C a l l i n gq u i c k S o r to ne l e m e n t s5t o7 T h el e f tp a r t i t i o ni sp a s s e di n t ot h eq u i c k s o r tf u n c t i o n . 123457689 6i sc h o s e na st h ep i v o t . 123457689 T h er i g h t w a r d ss e a r c hf o ral a r g ee l e m e n tb e g i n sf r o mt h el e f t e n do ft h ep a r t i t i o n . 123457689 T h er i g h t w a r d ss e a r c hs t o p sa s8i sf o u n d . 123457689 T h el e f t w a r d ss e a r c hf o ras m a l le l e m e n tb e g i n sf r o mt h er i g h t e n do ft h ep a r t i t i o n . 123457689 N o wt h a t6i sf o u n d ,t h el e f t w a r d ss e a r c hs t o p s .A st h es e a r c h i n d i c e sh a v ea l r e a d yc r o s s e d ,n os w a pi sp e r f o r m e d . 123456789 S ot h ep i v o ti ss w a p p e dw i t ht h ee l e m e n ta tp o s i t i o nk ,t h e l a s te l e m e n tc o m p a r e dt ot h ep i v o ti nt h el e f t w a r d ss e a r c h . C a l l i n gq u i c k S o r to ne l e m e n t s5t o5 T h el e f ts u b p a r t i t i o ni sq u i c k s o r t e d .N o t h i n gi sd o n es i n c ei ti st o os m a l l . C a l l i n gq u i c k S o r to ne l e m e n t s7t o7 L i k e w i s ew i t ht h er i g h ts u b p a r t i t i o n . C a l l i n gq u i c k S o r to ne l e m e n t s9t o8 D u et ot h e" s o r tt h ep a r t i t i o ns t a r t i t n go n et ot h er i g h to ft h ep i v o t " c o n s t r u c t i o no ft h ea l g o r i t h m ,a ne m p t yp a r t i t i o ni sp a s s e di n t ot h eq u i c k s o r t f u n c t i o n .N o t h i n gi sd o n ef o rt h i sb a s ec a s e . 123456789 F i n a l l y ,t h ee n t i r ea r r a yh a sb e e ns o r t e d . http://www.mycstutorials.com/articles/sorting/quicksort

5/6

1/30/2014

Sorting Algorithms - QuickSort Tutorial, Example, and Java code


F i n a l l y ,t h ee n t i r ea r r a yh a sb e e ns o r t e d .

An implementation of the algorithm is shown below:


p u b l i cv o i dq u i c k S o r t ( i n ta r r a y [ ] ) / /p r e :a r r a yi sf u l l ,a l le l e m e n t sa r en o n n u l li n t e g e r s / /p o s t :t h ea r r a yi ss o r t e di na s c e n d i n go r d e r { q u i c k S o r t ( a r r a y ,0 ,a r r a y . l e n g t h-1 ) ; }

/ /q u i c k s o r ta l lt h ee l e m e n t si

p u b l i cv o i dq u i c k S o r t ( i n ta r r a y [ ] ,i n ts t a r t ,i n te n d ) { i n ti=s t a r t ; / /i n d e xo fl e f t t o r i g h ts c a n i n tk=e n d ; / /i n d e xo fr i g h t t o l e f ts c a n i f( e n d-s t a r t> =1 ) { i n tp i v o t=a r r a y [ s t a r t ] ;

/ /c h e c kt h a tt h e r ea r ea tl e a s tt w oe l e m e n

/ /s e tt h ep i v o ta st h ef i r s te l e m e n ti nt h

w h i l e( k>i ) / /w h i l et h es c a ni n d i c e sf r o ml e f ta n dr i g { w h i l e( a r r a y [ i ]< =p i v o t& &i< =e n d& &k>i ) / /f r o mt h el e f t ,l i + + ; / /e l e m e n tg r e a t e r w h i l e( a r r a y [ k ]>p i v o t& &k> =s t a r t& &k> =i )/ /f r o mt h er i g h t , k ; / /e l e m e n tn o tg r e a i f( k>i ) / /i ft h el e f ts e e s w a p ( a r r a y ,i ,k ) ; / /t h er i g h ti n d e x , } s w a p ( a r r a y ,s t a r t ,k ) ; / /a f t e rt h ei n d i c e sh a v ec r o s s e d ,s w a pt h e / /t h el e f tp a r t i t i o nw i t ht h ep i v o t q u i c k S o r t ( a r r a y ,s t a r t ,k-1 ) ;/ /q u i c k s o r tt h el e f tp a r t i t i o n q u i c k S o r t ( a r r a y ,k+1 ,e n d ) ; / /q u i c k s o r tt h er i g h tp a r t i t i o n } e l s e { } } / /i ft h e r ei so n l yo n ee l e m e n ti nt h ep a r t i t i o n ,d on o td oa n ys o r t i n g r e t u r n ; / /t h ea r r a yi ss o r t e d ,s oe x i t

p u b l i cv o i ds w a p ( i n ta r r a y [ ] ,i n ti n d e x 1 ,i n ti n d e x 2 ) / /p r e :a r r a yi sf u l la n di n d e x 1 ,i n d e x 2<a r r a y . l e n g t h / /p o s t :t h ev a l u e sa ti n d i c e s1a n d2h a v eb e e ns w a p p e d { i n tt e m p=a r r a y [ i n d e x 1 ] ; / /s t o r et h ef i r s tv a l u ei nat e m p a r r a y [ i n d e x 1 ]=a r r a y [ i n d e x 2 ] ; / /c o p yt h ev a l u eo ft h es e c o n di n t ot h ef i r s t a r r a y [ i n d e x 2 ]=t e m p ; / /c o p yt h ev a l u eo ft h et e m pi n t ot h es e c o n d }

Written by Eugene Jitomirsky. Improved code courtesy of Pavel.


Copyright 2005-2014 MyCSTutorials.com

http://www.mycstutorials.com/articles/sorting/quicksort

6/6

You might also like