SlideShare a Scribd company logo
1 of 13
FFuunnddaammeennttaallss ooff AAllggoorriitthhmmss 
QQuuiicckk SSoorrtt
 Quick Sort : Based on Divide and Conquer 
paradigm. 
 One of the fastest in-memory sorting algorithms 
(if not the fastest) 
2 
 is a very efficient sorting algorithm 
 designed by C.A.R.Hoare in 1960. 
 Consists of two phases: 
 Partition phase 
 Sort phase 
QQuuiicckkSSoorrtt
3 
SStteeppss...... 
11.. DDiivviiddee:: PPiicckk aa ppiivvoott eelleemmeenntt aanndd rreeaarrrraannggee tthhee 
aarrrraayy ssoo tthhaatt 
 aallll eelleemmeennttss ttoo tthhee lleefftt ooff tthhee ppiivvoott aarree ssmmaalllleerr tthhaann tthhee 
ppiivvoott.. 
 aallll eelleemmeennttss ttoo tthhee rriigghhtt ooff tthhee ppiivvoott aarree llaarrggeerr tthhaann tthhee 
ppiivvoott.. 
22.. CCoonnqquueerr:: RReeccuurrssiivveellyy QQuuiicckkssoorrtt tthhee lleefftt aanndd rriigghhtt 
ssuubbaarrrraayyss.. 
33.. CCoommbbiinnee:: ssiinnccee ssuubbaarrrraayyss aarree ssoorrtteedd iinn ppllaaccee,, nnoo 
wwoorrkk iiss nneeeeddeedd ttoo ccoommbbiinnee tthheemm,, aarrrraayy iiss nnooww 
ssoorrtteedd..
QQuuiicckkssoorrtt –– PPaarrttiittiioonn pphhaassee 
4 
 GGooaallss:: 
 SSeelleecctt ppiivvoott vvaalluuee 
 MMoovvee eevveerryytthhiinngg lleessss tthhaann ppiivvoott vvaalluuee ttoo tthhee lleefftt ooff iitt 
 MMoovvee eevveerryytthhiinngg ggrreeaatteerr tthhaann ppiivvoott vvaalluuee ttoo tthhee rriigghhtt 
ooff iitt
AAllggoorriitthhmm:: QQuuiicckk SSoorrtt 
Procedure QuickSort ( A, l, r ) 
5 
if ( r > l ) then 
j = partition ( A, l, r ); 
QuickSort ( A, l, j - 1 ); 
QuickSort ( A, j + 1 , r ); 
end of if.
r 
9 8 2 3 88 34 5 10 11 0 
5 8 2 3 0 9 34 11 10 88 
6 
Partition 
l 
l j r 
A 
A
7 
AAllggoorriitthhmm:: PPaarrttiittiioonn 
Function Partition (A, l, r ) 
v = a[ l ]; i = l ; j = r+1; 
do 
do i = i +1 
while (a[i] <= v) and (i < n ); 
do j = j – 1 
while (a[ j] >= v) and (j > 2); 
if (i<j) then swap (a[i], a[j]); 
while j >= i; 
a [ l ] = a[ j ]; a[ j ] = v; 
return ( j );
v = a[ l ]; i = l ; j = r+1; 
do 
do i = i +1 while (a[i] <= v) and (i < n ); 
do j = j – 1 while (a[ j] >= v) and (j > 2); 
if (i<j) then swap (a[i], a[j]); 
i v 9 j 
9 8 2 3 88 34 5 10 11 0 
i j 
9 8 2 3 88 34 5 10 11 0 
i j 
9 8 2 3 0 34 5 10 11 88 
9 8 2 3 0 34 5 10 11 8 
88 
i j 
while j >= i; 
a [ l ] = a[ j ]; a[ j ] = v; 
return ( j );
9 
i j 
v = a[ l ]; i = l ; j = r+1; 
do 
9 8 2 3 0 5 34 10 11 88 
j i 
9 8 2 3 0 5 34 10 11 88 
j i 
5 8 2 3 0 9 34 10 11 88 
out of outer 
repeat loop 
do i = i +1 while (a[i] <= v) and (i < n ); 
do j = j – 1 while (a[ j] >= v) and (j > 2); 
if (i<j) then swap (a[i], a[j]); 
while j >= i; 
a [ l ] = a[ j ]; a[ j ] = v; 
return ( j );
WWoorrsstt CCaassee TTiimmee CCoommpplleexxiittyy ooff QQuuiicckkSSoorrtt 
The number of comparisons taken by Quicksort in the worst 
case are O(n²). 
Proof: 
Worst case occurs when Partition results in a zero size and 
an n-1 size partition. 
10
The worst case is when the input is already sorted. 
11 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8
BBeesstt CCaassee TTiimmee CCoommpplleexxiittyy ooff QQuuiicckkSSoorrtt 
The number of comparisons taken by quicksort in the best 
case are O(n lg(n)). 
Proof: 
If the partitioning procedure produces two regions of size n/2, 
quicksort runs much faster, 
Since we are dividing our input(n) with 2, so base case will 
arise after lg(n) iterations. 
Cost of each iteration is O(n), so the best case time 
complexity of quicksort is O(n lg(n)). 
12
RRaannddoommiizzeedd QQuuiicckk SSoorrtt 
 RRaannddoommiizzee tthhee iinnppuutt ddaattaa bbeeffoorree ggiivviinngg iitt ttoo QQuuiicckk 
SSoorrtt.. 
OORR 
 IInn tthhee ppaarrttiittiioonn pphhaassee,, rraatthheerr tthhaann cchhoooossiinngg ffiirrsstt 
vvaalluuee ttoo bbee tthhee ppiivvoott vvaalluuee,, cchhoooossee aa RRAANNDDOOMM 
vvaalluuee ttoo bbee ppiivvoott vvaalluuee.. 
 TThhiiss mmaakkeess QQuuiicckk SSoorrtt rruunn ttiimmee iinnddeeppeennddeenntt ooff 
iinnppuutt oorrddeerriinngg 
 SSoo WWoorrsstt ccaassee wwoonn’’tt hhaappppeenn ffoorr SSOORRTTEEDD iinnppuuttss 
bbuutt mmaayy oonnllyy hhaappppeenn bbyy wwoorrsseenneessss ooff rraannddoomm 
nnuummbbeerr ggeenneerraattoorr.. 
13

More Related Content

Viewers also liked

Yo mama jokes? | Yahoo Answers
Yo mama jokes? | Yahoo AnswersYo mama jokes? | Yahoo Answers
Yo mama jokes? | Yahoo Answerseconomicmystery85
 
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...Alican Bozkurt
 
SAP Business Object Material
SAP Business Object Material SAP Business Object Material
SAP Business Object Material erpsoln
 
Unit 2 Communication
Unit 2 CommunicationUnit 2 Communication
Unit 2 Communicationnigelcollege
 
Overview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPOverview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPgive2asia
 
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue1402 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14Akhil Bharat Mahasabha
 
Lobel LED Leaflet - New launching of LED Products in this Diwali.
Lobel LED Leaflet - New launching of LED Products in this Diwali.  Lobel LED Leaflet - New launching of LED Products in this Diwali.
Lobel LED Leaflet - New launching of LED Products in this Diwali. L'Obel Solar Power System
 
Clinical analysis report 14
Clinical analysis report 14Clinical analysis report 14
Clinical analysis report 14Nikee McEyes
 
Project 2016 progress template 25% completed
Project 2016 progress template  25% completedProject 2016 progress template  25% completed
Project 2016 progress template 25% completedThomas Wheeler
 
Respeaking as a part of translation and interpreting curriculum
Respeaking as a part of translation and interpreting curriculumRespeaking as a part of translation and interpreting curriculum
Respeaking as a part of translation and interpreting curriculumUniversity of Warsaw
 

Viewers also liked (11)

Yo mama jokes? | Yahoo Answers
Yo mama jokes? | Yahoo AnswersYo mama jokes? | Yahoo Answers
Yo mama jokes? | Yahoo Answers
 
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...
Classification of Fonts and Calligraphy Styles based on Complex Wavelet Trans...
 
SAP Business Object Material
SAP Business Object Material SAP Business Object Material
SAP Business Object Material
 
Unit 2 Communication
Unit 2 CommunicationUnit 2 Communication
Unit 2 Communication
 
Overview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPOverview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCP
 
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue1402 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14
02 july-2014 to-08-july-2014-hindu_sabhavarta_year38_issue14
 
Lobel LED Leaflet - New launching of LED Products in this Diwali.
Lobel LED Leaflet - New launching of LED Products in this Diwali.  Lobel LED Leaflet - New launching of LED Products in this Diwali.
Lobel LED Leaflet - New launching of LED Products in this Diwali.
 
Clinical analysis report 14
Clinical analysis report 14Clinical analysis report 14
Clinical analysis report 14
 
Unit2 review
Unit2 reviewUnit2 review
Unit2 review
 
Project 2016 progress template 25% completed
Project 2016 progress template  25% completedProject 2016 progress template  25% completed
Project 2016 progress template 25% completed
 
Respeaking as a part of translation and interpreting curriculum
Respeaking as a part of translation and interpreting curriculumRespeaking as a part of translation and interpreting curriculum
Respeaking as a part of translation and interpreting curriculum
 

quick sort by student of NUML university

  • 2.  Quick Sort : Based on Divide and Conquer paradigm.  One of the fastest in-memory sorting algorithms (if not the fastest) 2  is a very efficient sorting algorithm  designed by C.A.R.Hoare in 1960.  Consists of two phases:  Partition phase  Sort phase QQuuiicckkSSoorrtt
  • 3. 3 SStteeppss...... 11.. DDiivviiddee:: PPiicckk aa ppiivvoott eelleemmeenntt aanndd rreeaarrrraannggee tthhee aarrrraayy ssoo tthhaatt  aallll eelleemmeennttss ttoo tthhee lleefftt ooff tthhee ppiivvoott aarree ssmmaalllleerr tthhaann tthhee ppiivvoott..  aallll eelleemmeennttss ttoo tthhee rriigghhtt ooff tthhee ppiivvoott aarree llaarrggeerr tthhaann tthhee ppiivvoott.. 22.. CCoonnqquueerr:: RReeccuurrssiivveellyy QQuuiicckkssoorrtt tthhee lleefftt aanndd rriigghhtt ssuubbaarrrraayyss.. 33.. CCoommbbiinnee:: ssiinnccee ssuubbaarrrraayyss aarree ssoorrtteedd iinn ppllaaccee,, nnoo wwoorrkk iiss nneeeeddeedd ttoo ccoommbbiinnee tthheemm,, aarrrraayy iiss nnooww ssoorrtteedd..
  • 4. QQuuiicckkssoorrtt –– PPaarrttiittiioonn pphhaassee 4  GGooaallss::  SSeelleecctt ppiivvoott vvaalluuee  MMoovvee eevveerryytthhiinngg lleessss tthhaann ppiivvoott vvaalluuee ttoo tthhee lleefftt ooff iitt  MMoovvee eevveerryytthhiinngg ggrreeaatteerr tthhaann ppiivvoott vvaalluuee ttoo tthhee rriigghhtt ooff iitt
  • 5. AAllggoorriitthhmm:: QQuuiicckk SSoorrtt Procedure QuickSort ( A, l, r ) 5 if ( r > l ) then j = partition ( A, l, r ); QuickSort ( A, l, j - 1 ); QuickSort ( A, j + 1 , r ); end of if.
  • 6. r 9 8 2 3 88 34 5 10 11 0 5 8 2 3 0 9 34 11 10 88 6 Partition l l j r A A
  • 7. 7 AAllggoorriitthhmm:: PPaarrttiittiioonn Function Partition (A, l, r ) v = a[ l ]; i = l ; j = r+1; do do i = i +1 while (a[i] <= v) and (i < n ); do j = j – 1 while (a[ j] >= v) and (j > 2); if (i<j) then swap (a[i], a[j]); while j >= i; a [ l ] = a[ j ]; a[ j ] = v; return ( j );
  • 8. v = a[ l ]; i = l ; j = r+1; do do i = i +1 while (a[i] <= v) and (i < n ); do j = j – 1 while (a[ j] >= v) and (j > 2); if (i<j) then swap (a[i], a[j]); i v 9 j 9 8 2 3 88 34 5 10 11 0 i j 9 8 2 3 88 34 5 10 11 0 i j 9 8 2 3 0 34 5 10 11 88 9 8 2 3 0 34 5 10 11 8 88 i j while j >= i; a [ l ] = a[ j ]; a[ j ] = v; return ( j );
  • 9. 9 i j v = a[ l ]; i = l ; j = r+1; do 9 8 2 3 0 5 34 10 11 88 j i 9 8 2 3 0 5 34 10 11 88 j i 5 8 2 3 0 9 34 10 11 88 out of outer repeat loop do i = i +1 while (a[i] <= v) and (i < n ); do j = j – 1 while (a[ j] >= v) and (j > 2); if (i<j) then swap (a[i], a[j]); while j >= i; a [ l ] = a[ j ]; a[ j ] = v; return ( j );
  • 10. WWoorrsstt CCaassee TTiimmee CCoommpplleexxiittyy ooff QQuuiicckkSSoorrtt The number of comparisons taken by Quicksort in the worst case are O(n²). Proof: Worst case occurs when Partition results in a zero size and an n-1 size partition. 10
  • 11. The worst case is when the input is already sorted. 11 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
  • 12. BBeesstt CCaassee TTiimmee CCoommpplleexxiittyy ooff QQuuiicckkSSoorrtt The number of comparisons taken by quicksort in the best case are O(n lg(n)). Proof: If the partitioning procedure produces two regions of size n/2, quicksort runs much faster, Since we are dividing our input(n) with 2, so base case will arise after lg(n) iterations. Cost of each iteration is O(n), so the best case time complexity of quicksort is O(n lg(n)). 12
  • 13. RRaannddoommiizzeedd QQuuiicckk SSoorrtt  RRaannddoommiizzee tthhee iinnppuutt ddaattaa bbeeffoorree ggiivviinngg iitt ttoo QQuuiicckk SSoorrtt.. OORR  IInn tthhee ppaarrttiittiioonn pphhaassee,, rraatthheerr tthhaann cchhoooossiinngg ffiirrsstt vvaalluuee ttoo bbee tthhee ppiivvoott vvaalluuee,, cchhoooossee aa RRAANNDDOOMM vvaalluuee ttoo bbee ppiivvoott vvaalluuee..  TThhiiss mmaakkeess QQuuiicckk SSoorrtt rruunn ttiimmee iinnddeeppeennddeenntt ooff iinnppuutt oorrddeerriinngg  SSoo WWoorrsstt ccaassee wwoonn’’tt hhaappppeenn ffoorr SSOORRTTEEDD iinnppuuttss bbuutt mmaayy oonnllyy hhaappppeenn bbyy wwoorrsseenneessss ooff rraannddoomm nnuummbbeerr ggeenneerraattoorr.. 13

Editor's Notes

  1. &amp;lt;number&amp;gt;
  2. &amp;lt;number&amp;gt;
  3. &amp;lt;number&amp;gt;
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;
  6. &amp;lt;number&amp;gt;
  7. &amp;lt;number&amp;gt;
  8. &amp;lt;number&amp;gt;