SlideShare a Scribd company logo
Sorting
What makes it hard?
Chapter 7 in DS&AA
Chapter 8 in DS&PS
Insertion Sort
• Algorithm
– Conceptually, incremental add element to sorted array
or list, starting with an empty array (list).
– Incremental or batch algorithm.
• Analysis
– In best case, input is sorted: time is O(N)
– In worst case, input is reverse sorted: time is O(N2).
– Average case is (loose argument) is O(N2)
• Inversion: elements out of order
– critical variable for determining algorithm time-cost
– each swap removes exactly 1 inversion
Inversions
• What is average number of inversions, over all inputs?
• Let A be any array of integers
• Let revA be the reverse of A
• Note: if (i,j) are in order in A they are out of order in revA.
And vice versa.
• Total number of pairs (i,j) is N*(N-1)/2 so average
number of inversions is N*(N-1)/4 which is O(N2)
• Corollary: any algorithm that only removes a single
inversion at a time will take time at least O(N2)!
• To do better, we need to remove more than one inversion
at a time.
BubbleSort
• Most frequently used sorting algorithm
• Algorithm:
for j=n-1 to 1 …. O(n)
for i=0 to j ….. O(j)
if A[i] and A[i+1] are out of order, swap them
(that’s the bubble) …. O(1)
• Analysis
– Bubblesort is O(n^2)
• Appropriate for small arrays
• Appropriate for nearly sorted arrays
• Comparision versus swaps ?
Shell Sort: 1959 by Shell
• Motivated by inversion result - need to move far
elements
• Still quadratic
• Only in text books
• Historical interest and theoretical interest - not fully
understood.
• Algorithm: (with schedule 1, 3, 5)
– bubble sort things spaced 5 apart
– bubble sort things 3 apart
– bubble sort things 1 apart
• Faster than insertion sort, but still O(N^2)
• No one knows the best schedule
Divide and Conquer: Merge Sort
• Let A be array of integers of length n
• define Sort (A) recursively via auxSort(A,0,N) where
• Define array[] Sort(A,low, high)
– if (low == high) return
– Else
• mid = (low+high)/2
• temp1 = sort(A,low,mid)
• temp2 = sort(A,mid,high)
• temp3 = merge(temp1,temp2)
Merge
• Int[] Merge(int[] temp1, int[] temp2)
– int[] temp = new int[ temp1.length+temp2.length]
– int i,j,k
– repeat
• if (temp1[i]<temp2[j]) temp[k++]=temp1[i++]
• else temp[k++] = temp2[j++]
– for all appropriate i, j.
• Analysis of Merge:
– time: O( temp1.length+temp2.length)
– memory: O(temp1.length+temp2.length)
Analysis of Merge Sort
• Time
– Let N be number of elements
– Number of levels is O(logN)
– At each level, O(N) work
– Total is O(N*logN)
– This is best possible for sorting.
• Space
– At each level, O(N) temporary space
– Space can be freed, but calls to new costly
– Needs O(N) space
– Bad - better to have an in place sort
– Quick Sort (chapter 8) is the sort of choice.
Quicksort: Algorithm
• QuickSort - fastest algorithm
• QuickSort(S)
– 1. If size of S is 0 or 1, return S
– 2. Pick element v in S (pivot)
– 3. Construct L = all elements less than v and
R = all elements greater than v.
– 4. Return QuickSort(L), then v, then QuickSort(R)
• Algorithm can be done in situ (in place).
• On average runs in O(NlogN), but can take O(N2) time
– depends on choice of pivot.
Quicksort: Analysis
• Worst Case:
– T(N) = worst case sorting time
– T(1) = 1
– if bad pivot, T(N) = T(N-1)+N
– Via Telescope argument (expand and add)
– T(N) = O(N^2)
• Average Case (text argument)
– Assume equally likely subproblem sizes
• Note: chance of picking ith is 1/N
– T(N) average cost to sort
Analysis continued
– T(left branch) = T(right branch) (average) so
– T(N) = 2* ( T(0)+T(1)….T(N-1) )/N + N, where N is
cost of partitioning
– Multiply by N:
• NT(N) = 2(T(0)+…+T(N-1)) +N^2 (*)
– Subtract N-1 case of (*)
• NT(N) - (N-1)T(N-1) = 2T(N-1) +2N-1
– Rearrange and drop -1
• NT(N) = (N+1)T(N-1) + 2N -1
– Divide by N(N+1)
• T(N)/(N+1) = T(N-1) + 2/(N+1)
Last Step
• Substitute N-1, N-2,... 3 for N
– T(N-1)/N = T(N-2)/(N-1) + 2/N
– …
– T(2)/3 = T(1)/2 +2/3
• Add
– T(N)/(N+1) = T(1)/2+ 2(1/3+1/4 + ..+1/(N+1)
– = 2( 1+1/2 +…) -5/2 since T(1) = 0
– = O(logN)
• Hence T(N) = N logN
• In literature, more accurate proof.
• For better results, choose pivot as median of 3 random
values.
Quickselect: Algorithm
• Problem: find the kth smallest item
• Algorithm: modify Quicksort
– let |S| be the number of elements in S.
• QuickSelect(S, k)
– if |S| = 1, return element in S
– Pick element p in S (the pivot)
– Partition S via p as in QuickSort into L and R
– if k < |L| return QuickSelect(L,k)
– if k = |L|+1, return pivot
– otherwise return QuickSelect(R, k - |L|-1)
Quickselect: Analysis
• Worst Case is O(N^2)
• Average Case: analysis similar to quicksort’s.
• Here T(N) = 1*(T(0)+T(1)+…+T(N-1))/N + N
• Multiply by N
– NT(N) = T(0)+T(1) +T(N-1) + N^2
• Substitute with N = N-1 and subtract:
– NT(N) -(N-1)T(N-1) = T(N-1) + 2N -1
• Rearrange and divide by N
– T(N)= T(N-1)+2
– T(N) = T(N-2) + 4….. = T(1)+2*N = O(N)
• Average Case: Linear.
Bucket Sort
• A linear time sort algorithm!
• Need to know the possible values.
• Example 1: to sort N integers less than M.
– Make array A of size M
– Read each integer i and update, A[i]++
• Example 2: 200 names
– make array of size 26*26 = 676
– Using first 2 letters of each name, put it in [char-char]
bucket (usually a short ordered linked list)
– Collect them up
Radix Sorting (card sorting)
• Uses linked lists
• Idea: Multiple passes of Bucket Sort
• Trick: Iteratively sort by last index, next to last, etc.
• Example
ed ca xa cd xd bd
pass1: a:{ca, xa} d:{ed, cd, xd, bd}
ca xa ed cd xd bd
pass 2: b{bd} c: {ca, cd} e: {ed} x:{xa, xd}
bd ca cd ed xa xd
• Complexity: O(N* number of passes)
– number of passes = length of key
External Sorting (Tape or CD)
• Idea: merge sort (2-way)
• Suppose memory size is M (enough to sort internally)
• Ta1, Ta2, Tb1, Tb2 are tape drives
• Data on Ta1 (initially)
• Pass 1:
– read M records
– sort and write to Tb1, Tb2 alternatively
(each run of M records on Tb1, Tb2 is sorted)
• Pass 2:
– merge sort Tb1 and Tb2 onto Ta1 and Ta2
• Note this takes O(1) memory
– Each run of 2*M records is sorted
External Sorting
• Continuing merging, alternating writing to ta1, ta2.
• Number of passes is log(N/M)
• Time comlexity is O( N/M *log(M)) for first pass
• O(N) for subsequent passes
• Total: O(max(N log(N/M), N/M*log(M))
• With more tapes, can reduce time by doing k-way merge
rather than 2-way merge
• Replace Log base 2 with log base k
• A trickier algorithm (Polyphase) can do it with fewer
tapes.
• Who uses tapes? Algorithm works for CDs
Lower Bound for Sorting
• Theorem: if you sort by comparisons, then must use at
least log(N!) comparisons. Hence N logN algorithm.
• Proof:
– N items can be rearranged in N! ways.
– Consider a decision tree where each internal node is a
comparison.
– Each possible array goes down one path
– Number of leaves N!
– minimum depth of a decision tree is log(N!)
– log(N!) = log1+log2+…+log(N) is O(N logN)
– Proof: use partition trick
• sum log(N/2) + log(N/2+1)….log(N) >N/2*log(N/2)
Summary
• For online sorting, use heapsort.
– Online : get elements one at at time
– Offline or Batch: have all elements available
• For small collections, bubble sort is fine
• For large collections, use quicksort
• You may hybridize the algorithms, e.g
– use quicksort until the size is below some k
– then use bubble sort
• Sorting is important and well-studied and often
inefficiently done.
• Libraries often contain sorting routines, but beware:
the quicksort routine in Visual C++ seems to run in
quadratic time. Java sorts in Collections are fine.

More Related Content

What's hot

Sorting
SortingSorting
Sorting
Gopi Saiteja
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
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
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
irdginfo
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Delowar Hossain
 
Sorting
SortingSorting
Sorting
Zaid Shabbir
 
Sorting
SortingSorting
Sorting
Ghaffar Khan
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Merge sort
Merge sortMerge sort
Merge sort
Sindhoo Oad
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Eleonora Ciceri
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
Al Amin
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Muhammad Sarfraz
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 
Merge sort
Merge sortMerge sort
Merge sort
Rojin Khadka
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 

What's hot (20)

Sorting
SortingSorting
Sorting
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Merge sort
Merge sortMerge sort
Merge sort
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 

Similar to Quicksort

pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
Pradeep Bisht
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
Premjeet Roy
 
sorting
sortingsorting
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
Deepak John
 
Time complexity
Time complexityTime complexity
Time complexity
Kartik Chandra Mandal
 
Introduction
IntroductionIntroduction
Introduction
pilavare
 
Data Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptxData Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptx
Subrata Kumer Paul
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Recurrences
RecurrencesRecurrences
Recurrences
Ala' Mohammad
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Reetesh Gupta
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
AliAhmad38278
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
Sam Light
 
Recurrences
RecurrencesRecurrences
Recurrences
DEVTYPE
 

Similar to Quicksort (20)

pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
sorting
sortingsorting
sorting
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Time complexity
Time complexityTime complexity
Time complexity
 
Introduction
IntroductionIntroduction
Introduction
 
Data Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptxData Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptx
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Quick sort
Quick sortQuick sort
Quick sort
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
lecture 10
lecture 10lecture 10
lecture 10
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Recurrences
RecurrencesRecurrences
Recurrences
 

Recently uploaded

TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 

Recently uploaded (20)

TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 

Quicksort

  • 1. Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS
  • 2. Insertion Sort • Algorithm – Conceptually, incremental add element to sorted array or list, starting with an empty array (list). – Incremental or batch algorithm. • Analysis – In best case, input is sorted: time is O(N) – In worst case, input is reverse sorted: time is O(N2). – Average case is (loose argument) is O(N2) • Inversion: elements out of order – critical variable for determining algorithm time-cost – each swap removes exactly 1 inversion
  • 3. Inversions • What is average number of inversions, over all inputs? • Let A be any array of integers • Let revA be the reverse of A • Note: if (i,j) are in order in A they are out of order in revA. And vice versa. • Total number of pairs (i,j) is N*(N-1)/2 so average number of inversions is N*(N-1)/4 which is O(N2) • Corollary: any algorithm that only removes a single inversion at a time will take time at least O(N2)! • To do better, we need to remove more than one inversion at a time.
  • 4. BubbleSort • Most frequently used sorting algorithm • Algorithm: for j=n-1 to 1 …. O(n) for i=0 to j ….. O(j) if A[i] and A[i+1] are out of order, swap them (that’s the bubble) …. O(1) • Analysis – Bubblesort is O(n^2) • Appropriate for small arrays • Appropriate for nearly sorted arrays • Comparision versus swaps ?
  • 5. Shell Sort: 1959 by Shell • Motivated by inversion result - need to move far elements • Still quadratic • Only in text books • Historical interest and theoretical interest - not fully understood. • Algorithm: (with schedule 1, 3, 5) – bubble sort things spaced 5 apart – bubble sort things 3 apart – bubble sort things 1 apart • Faster than insertion sort, but still O(N^2) • No one knows the best schedule
  • 6. Divide and Conquer: Merge Sort • Let A be array of integers of length n • define Sort (A) recursively via auxSort(A,0,N) where • Define array[] Sort(A,low, high) – if (low == high) return – Else • mid = (low+high)/2 • temp1 = sort(A,low,mid) • temp2 = sort(A,mid,high) • temp3 = merge(temp1,temp2)
  • 7. Merge • Int[] Merge(int[] temp1, int[] temp2) – int[] temp = new int[ temp1.length+temp2.length] – int i,j,k – repeat • if (temp1[i]<temp2[j]) temp[k++]=temp1[i++] • else temp[k++] = temp2[j++] – for all appropriate i, j. • Analysis of Merge: – time: O( temp1.length+temp2.length) – memory: O(temp1.length+temp2.length)
  • 8. Analysis of Merge Sort • Time – Let N be number of elements – Number of levels is O(logN) – At each level, O(N) work – Total is O(N*logN) – This is best possible for sorting. • Space – At each level, O(N) temporary space – Space can be freed, but calls to new costly – Needs O(N) space – Bad - better to have an in place sort – Quick Sort (chapter 8) is the sort of choice.
  • 9. Quicksort: Algorithm • QuickSort - fastest algorithm • QuickSort(S) – 1. If size of S is 0 or 1, return S – 2. Pick element v in S (pivot) – 3. Construct L = all elements less than v and R = all elements greater than v. – 4. Return QuickSort(L), then v, then QuickSort(R) • Algorithm can be done in situ (in place). • On average runs in O(NlogN), but can take O(N2) time – depends on choice of pivot.
  • 10. Quicksort: Analysis • Worst Case: – T(N) = worst case sorting time – T(1) = 1 – if bad pivot, T(N) = T(N-1)+N – Via Telescope argument (expand and add) – T(N) = O(N^2) • Average Case (text argument) – Assume equally likely subproblem sizes • Note: chance of picking ith is 1/N – T(N) average cost to sort
  • 11. Analysis continued – T(left branch) = T(right branch) (average) so – T(N) = 2* ( T(0)+T(1)….T(N-1) )/N + N, where N is cost of partitioning – Multiply by N: • NT(N) = 2(T(0)+…+T(N-1)) +N^2 (*) – Subtract N-1 case of (*) • NT(N) - (N-1)T(N-1) = 2T(N-1) +2N-1 – Rearrange and drop -1 • NT(N) = (N+1)T(N-1) + 2N -1 – Divide by N(N+1) • T(N)/(N+1) = T(N-1) + 2/(N+1)
  • 12. Last Step • Substitute N-1, N-2,... 3 for N – T(N-1)/N = T(N-2)/(N-1) + 2/N – … – T(2)/3 = T(1)/2 +2/3 • Add – T(N)/(N+1) = T(1)/2+ 2(1/3+1/4 + ..+1/(N+1) – = 2( 1+1/2 +…) -5/2 since T(1) = 0 – = O(logN) • Hence T(N) = N logN • In literature, more accurate proof. • For better results, choose pivot as median of 3 random values.
  • 13. Quickselect: Algorithm • Problem: find the kth smallest item • Algorithm: modify Quicksort – let |S| be the number of elements in S. • QuickSelect(S, k) – if |S| = 1, return element in S – Pick element p in S (the pivot) – Partition S via p as in QuickSort into L and R – if k < |L| return QuickSelect(L,k) – if k = |L|+1, return pivot – otherwise return QuickSelect(R, k - |L|-1)
  • 14. Quickselect: Analysis • Worst Case is O(N^2) • Average Case: analysis similar to quicksort’s. • Here T(N) = 1*(T(0)+T(1)+…+T(N-1))/N + N • Multiply by N – NT(N) = T(0)+T(1) +T(N-1) + N^2 • Substitute with N = N-1 and subtract: – NT(N) -(N-1)T(N-1) = T(N-1) + 2N -1 • Rearrange and divide by N – T(N)= T(N-1)+2 – T(N) = T(N-2) + 4….. = T(1)+2*N = O(N) • Average Case: Linear.
  • 15. Bucket Sort • A linear time sort algorithm! • Need to know the possible values. • Example 1: to sort N integers less than M. – Make array A of size M – Read each integer i and update, A[i]++ • Example 2: 200 names – make array of size 26*26 = 676 – Using first 2 letters of each name, put it in [char-char] bucket (usually a short ordered linked list) – Collect them up
  • 16. Radix Sorting (card sorting) • Uses linked lists • Idea: Multiple passes of Bucket Sort • Trick: Iteratively sort by last index, next to last, etc. • Example ed ca xa cd xd bd pass1: a:{ca, xa} d:{ed, cd, xd, bd} ca xa ed cd xd bd pass 2: b{bd} c: {ca, cd} e: {ed} x:{xa, xd} bd ca cd ed xa xd • Complexity: O(N* number of passes) – number of passes = length of key
  • 17. External Sorting (Tape or CD) • Idea: merge sort (2-way) • Suppose memory size is M (enough to sort internally) • Ta1, Ta2, Tb1, Tb2 are tape drives • Data on Ta1 (initially) • Pass 1: – read M records – sort and write to Tb1, Tb2 alternatively (each run of M records on Tb1, Tb2 is sorted) • Pass 2: – merge sort Tb1 and Tb2 onto Ta1 and Ta2 • Note this takes O(1) memory – Each run of 2*M records is sorted
  • 18. External Sorting • Continuing merging, alternating writing to ta1, ta2. • Number of passes is log(N/M) • Time comlexity is O( N/M *log(M)) for first pass • O(N) for subsequent passes • Total: O(max(N log(N/M), N/M*log(M)) • With more tapes, can reduce time by doing k-way merge rather than 2-way merge • Replace Log base 2 with log base k • A trickier algorithm (Polyphase) can do it with fewer tapes. • Who uses tapes? Algorithm works for CDs
  • 19. Lower Bound for Sorting • Theorem: if you sort by comparisons, then must use at least log(N!) comparisons. Hence N logN algorithm. • Proof: – N items can be rearranged in N! ways. – Consider a decision tree where each internal node is a comparison. – Each possible array goes down one path – Number of leaves N! – minimum depth of a decision tree is log(N!) – log(N!) = log1+log2+…+log(N) is O(N logN) – Proof: use partition trick • sum log(N/2) + log(N/2+1)….log(N) >N/2*log(N/2)
  • 20. Summary • For online sorting, use heapsort. – Online : get elements one at at time – Offline or Batch: have all elements available • For small collections, bubble sort is fine • For large collections, use quicksort • You may hybridize the algorithms, e.g – use quicksort until the size is below some k – then use bubble sort • Sorting is important and well-studied and often inefficiently done. • Libraries often contain sorting routines, but beware: the quicksort routine in Visual C++ seems to run in quadratic time. Java sorts in Collections are fine.