SlideShare a Scribd company logo
1 of 38
QuickSort Algorithm
Using Divide and Conquer for Sorting
Soumen Santra
TECHNO INTERNATIONAL NEW TOWN
QuickSort 2
Topics Covered
• QuickSort algorithm
– analysis
• Randomized Quick Sort
• A Lower Bound on Comparison-Based
Sorting
QuickSort 3
Quick Sort
• Divide and conquer idea: Divide problem into
two smaller sorting problems.
• Divide:
– Select a splitting element (pivot)
– Rearrange the array (sequence/list)
QuickSort 4
Quick Sort
• Result:
– All elements to the left of pivot are smaller
or equal than pivot, and
– All elements to the right of pivot are greater
or equal than pivot
– pivot in correct place in sorted array/list
• Need: Clever split procedure.
QuickSort 5
Quick Sort
Divide: Partition into subarrays (sub-lists)
Conquer: Recursively sort 2 subarrays
Combine: Trivial
QuickSort 6
QuickSort (Hoare 1962)
Problem: Sort n keys in nondecreasing order
Inputs: Positive integer n, array of keys S indexed from 1 to n
Output: The array S containing the keys in nondecreasing
order.
quicksort ( low, high )
1. if high > low
2. then partition(low, high, pivotIndex)
3. quicksort(low, pivotIndex -1)
4. quicksort(pivotIndex +1, high)
QuickSort 7
Partition array for Quicksort
partition (low, high, pivot)
1. pivotitem = S [low]
2. k = low
3. for j = low +1 to high
4. do if S [ j ] < pivotitem
5. then k = k + 1
6. exchange S [ j ] and S [ k ]
7. pivot = k
8. exchange S[low] and S[pivot]
Quicksort example
13
81
92
43 31
65
57
26
75
0
13
81
92
43 31
65
57
26
75
0
13
43
31 57
26 0 81 92 75
65
13 43
31 57
26
0 81 92
75
13 43
31 57
26
0 65 81 92
75
Select pivot
partition
Recursive call Recursive call
Merge
9
Idea of Quick Sort
1) Select: pick an element
2) Divide: rearrange
elements so that x goes to
its final position E
3) Recurse and Conquer:
recursively sort
10
Quick-Sort Tree
11
Quick-Sort Tree
12
Quick-Sort Tree
13
Quick-Sort Tree
14
Quick-Sort Tree
15
Quick-Sort Tree
16
Quick-Sort Tree
17
Quick-Sort Tree
18
Quick-Sort Tree
19
Quick-Sort Tree
20
Quick-Sort Tree
21
Quick-Sort Tree
Skipping
22
... Finally
QuickSort 23
Worst Case Intuition
n-1
0 n-2
0 n-3
0 n-4
0 1
.
.
.
0 0
n-1
n-2
n-3
n-4
1
0
t(n) =

k = 1
n
k = (n+1)n/2
Total =
QuickSort 24
Recursion Tree for Best Case
n
n/2 n/2
n/4 n/4
n
n
n
n
n/4 n/4
n/8
.
.
>
n/8
n/8
.
.
>
n/8
n/8
.
.
>
n/8
n/8
.
.
>
n/8
Sum =(n lgn)
Nodes contain problem size
Partition Comparisons
QuickSort 25
Recursion Tree for
Magic pivot function that Partitions a “list”
into 1/9 and 8/9 “lists”
n
n/9 8n/9
8n/81 64n/81
<n
n/81 8n/81
256n/729
.
.
>
.
.
>
.
.
>
9n/729
.
.
>
n/729
0/1
0/1
0/1
...
(log9 n)
(log9/8 n)
<n
0/1
n
n
n
n
QuickSort 26
Intuition for the Average case
worst partition followed by the best partition
Vs
n
n-1
1
(n-1)/2
(n-1)/2
n
1+(n-1)/2 (n-1)/2
This shows a bad split can be “absorbed” by a good split.
Therefore we feel running time for the average case is
O(n lg n)
QuickSort 27
Recurrence equation:
T(n) = max ( T(q-1) + T(n - q) )+  (n)
0  q  n-1
A(n) = (1/n)  (A(q -1) + A(n - q ) ) +  (n)
n
Worst case
Average case
q = 1
QuickSort 28
Sorts and extra memory
• When a sorting algorithm does not require more
than (1) extra memory we say that the algorithm
sorts in-place.
• The textbook implementation of Mergesort
requires (n) extra space
• The textbook implementation of Heapsort is
in-place.
• Our implement of Quick-Sort is in-place except
for the stack.
QuickSort 29
Quicksort - enhancements
• Choose “good” pivot (random, or mid value
between first, last and middle)
• When remaining array small use insertion
sort
QuickSort 30
Randomized algorithms
• Uses a randomizer (such as a random number
generator)
• Some of the decisions made in the algorithm are
based on the output of the randomizer
• The output of a randomized algorithm could change
from run to run for the same input
• The execution time of the algorithm could also vary
from run to run for the same input
QuickSort 31
Randomized Quicksort
• Choose the pivot randomly (or randomly permute
the input array before sorting).
• The running time of the algorithm is independent
of input ordering.
• No specific input elicits worst case behavior.
– The worst case depends on the random number
generator.
• We assume a random number generator Random.
A call to Random(a, b) returns a random number
between a and b.
QuickSort 32
RQuicksort-main procedure
// S is an instance "array/sequence"
// terminate recursionquicksort ( low, high )
1. if high > low
2a. then i=random(low, high);
2b. swap(S[high], S[I]);
2c. partition(low, high, pivotIndex)
3. quicksort(low, pivotIndex -1)
4. quicksort(pivotIndex +1, high)
QuickSort 33
Randomized Quicksort Analysis
• We assume that all elements are distinct (to make
analysis simpler).
• We partition around a random element, all
partitions from 0:n-1 to n-1:0 are equally likely
• Probability of each partition is 1/n.
QuickSort 34
Average case time complexity
( )
)
log
(
)
(
)
1
(
)
1
(
)
1
(
)
0
(
)
(
)
(
2
)
(
)
0
(
)...
1
(
)
1
(
...
)
0
(
1
)
(
))
(
)
1
(
(
1
)
(
1
0
1
n
n
n
T
T
T
n
k
T
n
n
T
n
T
n
T
T
n
n
k
n
T
k
T
n
n
T
n
k
n
k





























QuickSort 35
Summary of Worst Case Runtime
• exchange/insertion/selection sort = (n 2)
• mergesort = (n lg n )
• quicksort = (n 2 )
– average case quicksort = (n lg n )
• heapsort = (n lg n )
QuickSort 36
Sorting
• So far, our best sorting algorithms can run in
(n lg n) in the worst case.
• CAN WE DO BETTER??
QuickSort 37
Exchange Sort
1. for (i = 1; i  n -1; i++)
2. for (j = i + 1; j  n ; j++)
3. if ( S[ j ] < S[ i ])
4. swap(S[ i ] ,S[ j ])
At end of i = 1 : S[1] = minS[i]
At end of i = 2 : S[2] = minS[i]
At end of i = 3 : S[3] = minS[i]
1 i  n
2 i  n
n- 1 i  n
THANK YOU
GIVE FEEDBACK

More Related Content

What's hot

Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Mahesh Tibrewal
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sortAaron Joaquin
 
3 concurrency controlone_v3
3 concurrency controlone_v33 concurrency controlone_v3
3 concurrency controlone_v3ashish61_scs
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsAbdullah Al-hazmy
 

What's hot (20)

Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sort
 
3 concurrency controlone_v3
3 concurrency controlone_v33 concurrency controlone_v3
3 concurrency controlone_v3
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Quicksort
QuicksortQuicksort
Quicksort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
 
Quick sort
Quick sortQuick sort
Quick sort
 
SCHEDULING DAGs WITHOUT CONSIDERING COMMUNICATION
SCHEDULING DAGs WITHOUT CONSIDERING COMMUNICATIONSCHEDULING DAGs WITHOUT CONSIDERING COMMUNICATION
SCHEDULING DAGs WITHOUT CONSIDERING COMMUNICATION
 
Lecture 23 24-time_response
Lecture 23 24-time_responseLecture 23 24-time_response
Lecture 23 24-time_response
 

Similar to Quick Sort

Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxnikshaikh786
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptxrediet43
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmMahbubur Rahman
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxsgrishma559
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 

Similar to Quick Sort (20)

Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
Merge sort
Merge sortMerge sort
Merge sort
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 

More from Soumen Santra

Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Soumen Santra
 
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxPPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxSoumen Santra
 
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Soumen Santra
 
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Soumen Santra
 
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Soumen Santra
 
A Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologyA Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologySoumen Santra
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Soumen Santra
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxSoumen Santra
 
Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Soumen Santra
 
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Soumen Santra
 
Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Soumen Santra
 
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsPURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsSoumen Santra
 
Carrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CACarrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CASoumen Santra
 
RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)Soumen Santra
 
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION  SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION Soumen Santra
 
Threads Basic : Features, Types & Implementation
Threads Basic : Features, Types  & ImplementationThreads Basic : Features, Types  & Implementation
Threads Basic : Features, Types & ImplementationSoumen Santra
 
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...Soumen Santra
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationSoumen Santra
 

More from Soumen Santra (20)

Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
 
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxPPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
 
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
 
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
 
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
 
Merge sort
Merge sortMerge sort
Merge sort
 
A Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologyA Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance Technology
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 
Java Basic PART I
Java Basic PART IJava Basic PART I
Java Basic PART I
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with Linux
 
Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)
 
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
 
Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)
 
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsPURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
 
Carrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CACarrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CA
 
RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)
 
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION  SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
 
Threads Basic : Features, Types & Implementation
Threads Basic : Features, Types  & ImplementationThreads Basic : Features, Types  & Implementation
Threads Basic : Features, Types & Implementation
 
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
 

Recently uploaded

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Recently uploaded (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 

Quick Sort

  • 1. QuickSort Algorithm Using Divide and Conquer for Sorting Soumen Santra TECHNO INTERNATIONAL NEW TOWN
  • 2. QuickSort 2 Topics Covered • QuickSort algorithm – analysis • Randomized Quick Sort • A Lower Bound on Comparison-Based Sorting
  • 3. QuickSort 3 Quick Sort • Divide and conquer idea: Divide problem into two smaller sorting problems. • Divide: – Select a splitting element (pivot) – Rearrange the array (sequence/list)
  • 4. QuickSort 4 Quick Sort • Result: – All elements to the left of pivot are smaller or equal than pivot, and – All elements to the right of pivot are greater or equal than pivot – pivot in correct place in sorted array/list • Need: Clever split procedure.
  • 5. QuickSort 5 Quick Sort Divide: Partition into subarrays (sub-lists) Conquer: Recursively sort 2 subarrays Combine: Trivial
  • 6. QuickSort 6 QuickSort (Hoare 1962) Problem: Sort n keys in nondecreasing order Inputs: Positive integer n, array of keys S indexed from 1 to n Output: The array S containing the keys in nondecreasing order. quicksort ( low, high ) 1. if high > low 2. then partition(low, high, pivotIndex) 3. quicksort(low, pivotIndex -1) 4. quicksort(pivotIndex +1, high)
  • 7. QuickSort 7 Partition array for Quicksort partition (low, high, pivot) 1. pivotitem = S [low] 2. k = low 3. for j = low +1 to high 4. do if S [ j ] < pivotitem 5. then k = k + 1 6. exchange S [ j ] and S [ k ] 7. pivot = k 8. exchange S[low] and S[pivot]
  • 8. Quicksort example 13 81 92 43 31 65 57 26 75 0 13 81 92 43 31 65 57 26 75 0 13 43 31 57 26 0 81 92 75 65 13 43 31 57 26 0 81 92 75 13 43 31 57 26 0 65 81 92 75 Select pivot partition Recursive call Recursive call Merge
  • 9. 9 Idea of Quick Sort 1) Select: pick an element 2) Divide: rearrange elements so that x goes to its final position E 3) Recurse and Conquer: recursively sort
  • 23. QuickSort 23 Worst Case Intuition n-1 0 n-2 0 n-3 0 n-4 0 1 . . . 0 0 n-1 n-2 n-3 n-4 1 0 t(n) =  k = 1 n k = (n+1)n/2 Total =
  • 24. QuickSort 24 Recursion Tree for Best Case n n/2 n/2 n/4 n/4 n n n n n/4 n/4 n/8 . . > n/8 n/8 . . > n/8 n/8 . . > n/8 n/8 . . > n/8 Sum =(n lgn) Nodes contain problem size Partition Comparisons
  • 25. QuickSort 25 Recursion Tree for Magic pivot function that Partitions a “list” into 1/9 and 8/9 “lists” n n/9 8n/9 8n/81 64n/81 <n n/81 8n/81 256n/729 . . > . . > . . > 9n/729 . . > n/729 0/1 0/1 0/1 ... (log9 n) (log9/8 n) <n 0/1 n n n n
  • 26. QuickSort 26 Intuition for the Average case worst partition followed by the best partition Vs n n-1 1 (n-1)/2 (n-1)/2 n 1+(n-1)/2 (n-1)/2 This shows a bad split can be “absorbed” by a good split. Therefore we feel running time for the average case is O(n lg n)
  • 27. QuickSort 27 Recurrence equation: T(n) = max ( T(q-1) + T(n - q) )+  (n) 0  q  n-1 A(n) = (1/n)  (A(q -1) + A(n - q ) ) +  (n) n Worst case Average case q = 1
  • 28. QuickSort 28 Sorts and extra memory • When a sorting algorithm does not require more than (1) extra memory we say that the algorithm sorts in-place. • The textbook implementation of Mergesort requires (n) extra space • The textbook implementation of Heapsort is in-place. • Our implement of Quick-Sort is in-place except for the stack.
  • 29. QuickSort 29 Quicksort - enhancements • Choose “good” pivot (random, or mid value between first, last and middle) • When remaining array small use insertion sort
  • 30. QuickSort 30 Randomized algorithms • Uses a randomizer (such as a random number generator) • Some of the decisions made in the algorithm are based on the output of the randomizer • The output of a randomized algorithm could change from run to run for the same input • The execution time of the algorithm could also vary from run to run for the same input
  • 31. QuickSort 31 Randomized Quicksort • Choose the pivot randomly (or randomly permute the input array before sorting). • The running time of the algorithm is independent of input ordering. • No specific input elicits worst case behavior. – The worst case depends on the random number generator. • We assume a random number generator Random. A call to Random(a, b) returns a random number between a and b.
  • 32. QuickSort 32 RQuicksort-main procedure // S is an instance "array/sequence" // terminate recursionquicksort ( low, high ) 1. if high > low 2a. then i=random(low, high); 2b. swap(S[high], S[I]); 2c. partition(low, high, pivotIndex) 3. quicksort(low, pivotIndex -1) 4. quicksort(pivotIndex +1, high)
  • 33. QuickSort 33 Randomized Quicksort Analysis • We assume that all elements are distinct (to make analysis simpler). • We partition around a random element, all partitions from 0:n-1 to n-1:0 are equally likely • Probability of each partition is 1/n.
  • 34. QuickSort 34 Average case time complexity ( ) ) log ( ) ( ) 1 ( ) 1 ( ) 1 ( ) 0 ( ) ( ) ( 2 ) ( ) 0 ( )... 1 ( ) 1 ( ... ) 0 ( 1 ) ( )) ( ) 1 ( ( 1 ) ( 1 0 1 n n n T T T n k T n n T n T n T T n n k n T k T n n T n k n k                             
  • 35. QuickSort 35 Summary of Worst Case Runtime • exchange/insertion/selection sort = (n 2) • mergesort = (n lg n ) • quicksort = (n 2 ) – average case quicksort = (n lg n ) • heapsort = (n lg n )
  • 36. QuickSort 36 Sorting • So far, our best sorting algorithms can run in (n lg n) in the worst case. • CAN WE DO BETTER??
  • 37. QuickSort 37 Exchange Sort 1. for (i = 1; i  n -1; i++) 2. for (j = i + 1; j  n ; j++) 3. if ( S[ j ] < S[ i ]) 4. swap(S[ i ] ,S[ j ]) At end of i = 1 : S[1] = minS[i] At end of i = 2 : S[2] = minS[i] At end of i = 3 : S[3] = minS[i] 1 i  n 2 i  n n- 1 i  n