Sorting Algorithms - QuickSort Tutorial, Example, and Java Code
Sorting Algorithms - QuickSort Tutorial, Example, and Java Code
- Articles
featured articles
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
http://www.mycstutorials.com/articles/sorting/quicksort
2/6
1/30/2014
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
123549687 http://www.mycstutorials.com/articles/sorting/quicksort
4/6
1/30/2014
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
/ /q u i c k s o r ta l lt h ee l e m e n t si
/ /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
http://www.mycstutorials.com/articles/sorting/quicksort
6/6