SlideShare a Scribd company logo
Different types ofDifferent types of
Sorting TechniquesSorting Techniques
used in Dataused in Data
StructuresStructures
Sorting: DefinitionSorting: Definition
Sorting: an operation that segregates items into
groups according to specified criterion.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
SortingSorting
• Sorting = ordering
• Sorted = ordered based on a
particular way.
• Generally, collections of data are
presented in a sorted manner.
• Examples of Sorting:
o Words in a dictionary are sorted (and case distinctions are
ignored).
o Files in a directory are often listed in sorted order.
o The index of a book is sorted (and case distinctions are
ignored).
Types of Sorting AlgorithmsTypes of Sorting Algorithms
There are many, many different types of sorting
algorithms, but the primary ones are:
●
Bubble Sort
●
Selection Sort
●
Insertion Sort
●
Merge Sort
●
Quick Sort
●
Shell Sort
●
Radix Sort
●
Swap Sort
●
Heap Sort
Bubble Sort: IdeaBubble Sort: Idea
• Idea: bubble in water.
o Bubble in water moves upward. Why?
• How?
o When a bubble moves upward, the water from above will move
downward to fill in the space left by the bubble.
Bubble Sort ExampleBubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7
6, 2, 9, 11, 9, 3, 12, 7
6, 2, 9, 11, 9, 3, 7, 12The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged.
The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.
The twelve is greater than the 9 so they are exchangedThe twelve is greater than the 9 so they are exchanged
The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.
In the third comparison, the 9 is not larger than the 12 so no
exchange is made. We move on to compare the next pair without
any change to the list.
In the third comparison, the 9 is not larger than the 12 so no
exchange is made. We move on to compare the next pair without
any change to the list.
Now the next pair of numbers are compared. Again the 9 is the
larger and so this pair is also exchanged.
Now the next pair of numbers are compared. Again the 9 is the
larger and so this pair is also exchanged.
Bubblesort compares the numbers in pairs from left to right
exchanging when necessary. Here the first number is compared
to the second and as it is larger they are exchanged.
Bubblesort compares the numbers in pairs from left to right
exchanging when necessary. Here the first number is compared
to the second and as it is larger they are exchanged.
The end of the list has been reached so this is the end of the first pass. The
twelve at the end of the list must be largest number in the list and so is now in
the correct position. We now start a new pass from left to right.
The end of the list has been reached so this is the end of the first pass. The
twelve at the end of the list must be largest number in the list and so is now in
the correct position. We now start a new pass from left to right.
Bubble Sort ExampleBubble Sort Example
6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
First Pass
Second Pass
Bubble Sort ExampleBubble Sort Example
2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
Bubble Sort ExampleBubble Sort Example
2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
Bubble Sort ExampleBubble Sort Example
2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12Fifth Pass
Bubble Sort ExampleBubble Sort Example
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12Fifth Pass
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With
other lists this check could save much more work.
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With
other lists this check could save much more work.
Bubble Sort: ExampleBubble Sort: Example
• Notice that at least one element will be in the
correct position each iteration.
40 2 1 43 3 65 0 -1 58 3 42 4
652 1 40 3 43 0 -1 58 3 42 4
65581 2 3 40 0 -1 43 3 42 4
1 2 3 400 65-1 43 583 42 4
1
2
3
4
1 0 -1 32 653 43 5842404
Bubble Sort: ExampleBubble Sort: Example
0 -1 1 2 653 43 58424043
-1 0 1 2 653 43 58424043
6
7
8
1 2 0 3-1 3 40 6543 584245
Bubble Sort: AnalysisBubble Sort: Analysis
• Running time:
o Worst case: O(N2
)
o Best case: O(N)
• Variant:
o bi-directional bubble sort
• original bubble sort: only works to one direction
• bi-directional bubble sort: works back and forth.
Selection Sort: IdeaSelection Sort: Idea
1. We have two group of items:
o sorted group, and
o unsorted group
1. Initially, all items are in the unsorted group. The
sorted group is empty.
o We assume that items in the unsorted group unsorted.
o We have to keep items in the sorted group sorted.
Selection Sort: ContSelection Sort: Cont’’dd
1. Select the “best” (eg. smallest) item from the
unsorted group, then put the “best” item at the
end of the sorted group.
2. Repeat the process until the unsorted group
becomes empty.
4240 2 1 3 3 4 0 -1 655843
40 2 1 43 3 4 0 -1 42 65583
40 2 1 43 3 4 0 -1 58 3 6542
40 2 1 43 3 65 0 -1 58 3 42 4
Selection Sort: ExampleSelection Sort: Example
4240 2 1 3 3 4 0 655843-1
42-1 2 1 3 3 4 0 65584340
42-1 2 1 3 3 4 655843400
42-1 2 1 0 3 4 655843403
Selection Sort: ExampleSelection Sort: Example
1
42-1 2 1 3 4 6558434030
42-1 0 3 4 6558434032
1 42-1 0 3 4 6558434032
1 420 3 4 6558434032-1
1 420 3 4 6558434032-1
Selection Sort: ExampleSelection Sort: Example
Selection Sort: AnalysisSelection Sort: Analysis
• Running time:
o Worst case: O(N2
)
o Best case: O(N2
)
Insertion Sort: IdeaInsertion Sort: Idea
1. We have two group of items:
o sorted group, and
o unsorted group
1. Initially, all items in the unsorted group and the
sorted group is empty.
o We assume that items in the unsorted group unsorted.
o We have to keep items in the sorted group sorted.
1. Pick any item from, then insert the item at the right
position in the sorted group to maintain sorted
property.
2. Repeat the process until the unsorted group
becomes empty.
40 2 1 43 3 65 0 -1 58 3 42 4
2 40 1 43 3 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
40
Insertion Sort: ExampleInsertion Sort: Example
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: ExampleInsertion Sort: Example
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 650 -1 58 3 42 4
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
Insertion Sort: ExampleInsertion Sort: Example
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
1 2 3 40 43 650 58 42 41 2 3 3 43 650-1 5840 43 65
1 2 3 40 43 650 42 41 2 3 3 43 650-1 5840 43 65
Insertion Sort: ExampleInsertion Sort: Example
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
Insertion Sort: AnalysisInsertion Sort: Analysis
• Running time analysis:
o Worst case: O(N2
)
o Best case: O(N)
A Lower BoundA Lower Bound
• Bubble Sort, Selection Sort, Insertion Sort all have
worst case of O(N2
).
• Turns out, for any algorithm that exchanges
adjacent items, this is the best worst case: Ω(N2
)
• In other words, this is a lower bound!
MergesortMergesort
•Mergesort (divide-and-conquer)
o Divide array into two halves.
A L G O R I T H M S
divideA L G O R I T H M S
MergesortMergesort
•Mergesort (divide-and-conquer)
o Divide array into two halves.
o Recursively sort each half.
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
MergesortMergesort
•Mergesort (divide-and-conquer)
o Divide array into two halves.
o Recursively sort each half.
o Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
auxiliary array
smallest smallest
A G L O R H I M S T
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
A
auxiliary array
smallest smallest
A G L O R H I M S T
A
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
G
auxiliary array
smallest smallest
A G L O R H I M S T
A G
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
H
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
I
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
L
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
M
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
O
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
R
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
S
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S
MergingMerging
•Merge.
o Keep track of smallest element in each sorted half.
o Insert smallest of two elements into auxiliary array.
o Repeat until done.
T
Notes on QuicksortNotes on Quicksort
• Quicksort is more widely used than any other sort.
• Quicksort is well-studied, not difficult to implement,
works well on a variety of data, and consumes
fewer resources that other sorts in nearly all
situations.
• Quicksort is O(n*log n) time, and O(log n) additional
space due to recursion.
Quicksort AlgorithmQuicksort Algorithm
• Quicksort is a divide-and-conquer method for
sorting. It works by partitioning an array into parts,
then sorting each part independently.
• The crux of the problem is how to partition the array
such that the following conditions are true:
o There is some element, a[i], where a[i] is in its final
position.
o For all l < i, a[l] < a[i].
o For all i < r, a[i] < a[r].
Quicksort AlgorithmQuicksort Algorithm
(cont)(cont)
• As is typical with a recursive program, once you
figure out how to divide your problem into smaller
subproblems, the implementation is amazingly
simple.
int partition(Item a[], int l, int r);
void quicksort(Item a[], int l, int r)
{ int i;
if (r <= l) return;
i = partition(a, l, r);
quicksort(a, l, i-1);
quicksort(a, i+1, r);
}
Partitioning with i
Sorting algorithms
Sorting algorithms
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
Q U I C K S O R T I S C O O L
partitioned
partition element left
right
unpartitioned
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
swap me
Q U I C K S O R T I S C O O L
partitioned
partition element left
right
unpartitioned
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
Q U I C K S O R T I S C O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
Q U I C K S O R T I S C O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
Q U I C K S O R T I S C O O L
swap me
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
C U I C K S O R T I S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
swap me
partitioned
partition element left
right
unpartitioned
C U I C K S O R T I S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
C U I C K S O R T I S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
C U I C K S O R T I S Q O O L
swap me
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• exchange
• repeat until pointers cross
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
swap me
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
partitioned
partition element left
right
unpartitioned
swap me
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
pointers cross
swap with
partitioning
element
partitioned
partition element left
right
unpartitioned
C I I C K S O R T U S Q O O L
Partitioning in QuicksortPartitioning in Quicksort
o How do we partition the array efficiently?
• choose partition element to be rightmost element
• scan from left for larger element
• scan from right for smaller element
• Exchange and repeat until pointers cross
partitioned
partition element left
right
unpartitioned
partition is
complete
C I I C K L O R T U S Q O O S
Sorting AlgorithmsSorting Algorithms
animations
Bubble Sort:
Pairwise exchange
Selection Sort:
Select the “best” (eg. smallest) item from the unsorted group,
then put the “best” item at the end of the sorted group
Insertion Sort
Pick any item from, then insert the item at the right position
in the sorted group to maintain sorted property.
Merge Sort and quicksort
Divide and conquer

More Related Content

PPT
Sorting algos
Omair Imtiaz Ansari
 
PPT
Sorting
Sameer Memon
 
PPT
Intro To TSQL - Unit 2
iccma
 
PPT
sPen Data Management
Uladzimir Slabin
 
PPTX
Sorting pnk
pinakspatel
 
PPTX
Introduction to Algorithms
pppepito86
 
PPT
Google at a glance 1998 - 2008
Andreas Jaffke
 
PPT
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 
Sorting algos
Omair Imtiaz Ansari
 
Sorting
Sameer Memon
 
Intro To TSQL - Unit 2
iccma
 
sPen Data Management
Uladzimir Slabin
 
Sorting pnk
pinakspatel
 
Introduction to Algorithms
pppepito86
 
Google at a glance 1998 - 2008
Andreas Jaffke
 
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 

Viewers also liked (20)

PPTX
How We Got Where We Are: 40 Years of Planning...
American Astronautical Society
 
PPTX
Why Ben Stein Is Wrong About History & Science
John Lynch
 
PPT
Chap04alg
Munkhchimeg
 
KEY
History of Creationism, Parts II & III
John Lynch
 
PPTX
History of Google Local from 2004-2011
Mike Blumenthal
 
PDF
Algorithms - Aaron Bloomfield
Reggie Niccolo Santos
 
PPTX
CSS 3, Style and Beyond
Samsung Open Source Group
 
PPTX
Was There A Darwinian Revolution
John Lynch
 
PPT
Tri Merge Sorting Algorithm
Ashim Sikder
 
PPT
Dc parent 14 2
mtaft
 
KEY
Ancient Ideas of Creation & Evolution
John Lynch
 
PPTX
Chapter one
mihiretu kassaye
 
PPT
Introduction to Information Technology ch 01_b
Shahi Raz Akhtar
 
KEY
The Chemical Revolution
John Lynch
 
PDF
A history of science (volume 1)
Dipoceanov Esrever
 
KEY
One Long Argument
John Lynch
 
PPTX
Google Algorithm Change History - 2k14-2k16.
Saba SEO
 
PPTX
National Air And Space Museum Washington DC
Shivakumar Patil
 
PPS
Ds 4
Niit Care
 
PPT
Google
vibhabehl
 
How We Got Where We Are: 40 Years of Planning...
American Astronautical Society
 
Why Ben Stein Is Wrong About History & Science
John Lynch
 
Chap04alg
Munkhchimeg
 
History of Creationism, Parts II & III
John Lynch
 
History of Google Local from 2004-2011
Mike Blumenthal
 
Algorithms - Aaron Bloomfield
Reggie Niccolo Santos
 
CSS 3, Style and Beyond
Samsung Open Source Group
 
Was There A Darwinian Revolution
John Lynch
 
Tri Merge Sorting Algorithm
Ashim Sikder
 
Dc parent 14 2
mtaft
 
Ancient Ideas of Creation & Evolution
John Lynch
 
Chapter one
mihiretu kassaye
 
Introduction to Information Technology ch 01_b
Shahi Raz Akhtar
 
The Chemical Revolution
John Lynch
 
A history of science (volume 1)
Dipoceanov Esrever
 
One Long Argument
John Lynch
 
Google Algorithm Change History - 2k14-2k16.
Saba SEO
 
National Air And Space Museum Washington DC
Shivakumar Patil
 
Ds 4
Niit Care
 
Google
vibhabehl
 
Ad

Similar to Sorting algorithms (20)

PPT
Sorting algorithms
Zaid Hameed
 
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
PPT
Sorting algorithms v01
Dusan Vuckovic
 
PPT
3.1 bubble sort
Krish_ver2
 
PPT
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
m.kumarasamy college of engineering
 
PPT
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
PPTX
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
PPT
Sorting algorithm
Muhammad Farhan
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPTX
sorting.pptx
DrRanjeetKumar51721
 
PDF
Sorting
A. S. M. Shafi
 
PPTX
Unit III Version I.pptx
ssuserd602fd
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Sorting algorithms
Zaid Hameed
 
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting algorithms v01
Dusan Vuckovic
 
3.1 bubble sort
Krish_ver2
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
m.kumarasamy college of engineering
 
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
Sorting algorithm
Muhammad Farhan
 
Unit 7 sorting
Dabbal Singh Mahara
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
L 14-ct1120
Zia Ush Shamszaman
 
Data Structure (MC501)
Kamal Singh Lodhi
 
Unit vii sorting
Tribhuvan University
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Sorting algorithms
CHANDAN KUMAR
 
sorting.pptx
DrRanjeetKumar51721
 
Unit III Version I.pptx
ssuserd602fd
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Ad

More from Edward Blurock (20)

PPTX
Ontology for the semantic enhancement, database definition and management and...
Edward Blurock
 
PPTX
Ontology for the Semantic Enhancement, Database Definition and Management and...
Edward Blurock
 
PPTX
Use of Ontologies in Chemical Kinetic Database CHEMCONNECT
Edward Blurock
 
PDF
KEOD23-JThermodynamcsCloud
Edward Blurock
 
PPTX
BlurockPresentation-KEOD2023
Edward Blurock
 
PPTX
KEOD-2023-Poster.pptx
Edward Blurock
 
PDF
ChemConnect: Poster for European Combustion Meeting 2017
Edward Blurock
 
PPTX
ChemConnect: SMARTCATS presentation
Edward Blurock
 
PPTX
EU COST Action CM1404: WG€ - Efficient Data Exchange
Edward Blurock
 
PPTX
ChemConnect: Viewing the datasets in the repository
Edward Blurock
 
PPTX
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
Edward Blurock
 
PDF
Poster: Characterizing Ignition behavior through morphing to generic curves
Edward Blurock
 
PDF
Poster: Very Open Data Project
Edward Blurock
 
PDF
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Edward Blurock
 
PPTX
Characterization Ignition Behavior through Morphing to Generic Ignition Curves
Edward Blurock
 
PPTX
Paradigms
Edward Blurock
 
PPTX
Computability, turing machines and lambda calculus
Edward Blurock
 
PPTX
Imperative programming
Edward Blurock
 
PPTX
Programming Languages
Edward Blurock
 
PPTX
Relational algebra
Edward Blurock
 
Ontology for the semantic enhancement, database definition and management and...
Edward Blurock
 
Ontology for the Semantic Enhancement, Database Definition and Management and...
Edward Blurock
 
Use of Ontologies in Chemical Kinetic Database CHEMCONNECT
Edward Blurock
 
KEOD23-JThermodynamcsCloud
Edward Blurock
 
BlurockPresentation-KEOD2023
Edward Blurock
 
KEOD-2023-Poster.pptx
Edward Blurock
 
ChemConnect: Poster for European Combustion Meeting 2017
Edward Blurock
 
ChemConnect: SMARTCATS presentation
Edward Blurock
 
EU COST Action CM1404: WG€ - Efficient Data Exchange
Edward Blurock
 
ChemConnect: Viewing the datasets in the repository
Edward Blurock
 
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
Edward Blurock
 
Poster: Characterizing Ignition behavior through morphing to generic curves
Edward Blurock
 
Poster: Very Open Data Project
Edward Blurock
 
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Edward Blurock
 
Characterization Ignition Behavior through Morphing to Generic Ignition Curves
Edward Blurock
 
Paradigms
Edward Blurock
 
Computability, turing machines and lambda calculus
Edward Blurock
 
Imperative programming
Edward Blurock
 
Programming Languages
Edward Blurock
 
Relational algebra
Edward Blurock
 

Recently uploaded (20)

PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
High Ground Student Revision Booklet Preview
jpinnuck
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 

Sorting algorithms

  • 1. Different types ofDifferent types of Sorting TechniquesSorting Techniques used in Dataused in Data StructuresStructures
  • 2. Sorting: DefinitionSorting: Definition Sorting: an operation that segregates items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 }
  • 3. SortingSorting • Sorting = ordering • Sorted = ordered based on a particular way. • Generally, collections of data are presented in a sorted manner. • Examples of Sorting: o Words in a dictionary are sorted (and case distinctions are ignored). o Files in a directory are often listed in sorted order. o The index of a book is sorted (and case distinctions are ignored).
  • 4. Types of Sorting AlgorithmsTypes of Sorting Algorithms There are many, many different types of sorting algorithms, but the primary ones are: ● Bubble Sort ● Selection Sort ● Insertion Sort ● Merge Sort ● Quick Sort ● Shell Sort ● Radix Sort ● Swap Sort ● Heap Sort
  • 5. Bubble Sort: IdeaBubble Sort: Idea • Idea: bubble in water. o Bubble in water moves upward. Why? • How? o When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.
  • 6. Bubble Sort ExampleBubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged. The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged. The twelve is greater than the 9 so they are exchangedThe twelve is greater than the 9 so they are exchanged The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged. In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list. In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list. Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged. Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged. Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged. Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged. The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right. The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.
  • 7. Bubble Sort ExampleBubble Sort Example 6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. First Pass Second Pass
  • 8. Bubble Sort ExampleBubble Sort Example 2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons. This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.
  • 9. Bubble Sort ExampleBubble Sort Example 2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed. 2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
  • 10. Bubble Sort ExampleBubble Sort Example 2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. 2, 6, 9, 3, 7, 9, 11, 12Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12Fifth Pass
  • 11. Bubble Sort ExampleBubble Sort Example 2, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass 2, 6, 9, 3, 7, 9, 11, 12Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12Fifth Pass Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12 This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work. This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.
  • 12. Bubble Sort: ExampleBubble Sort: Example • Notice that at least one element will be in the correct position each iteration. 40 2 1 43 3 65 0 -1 58 3 42 4 652 1 40 3 43 0 -1 58 3 42 4 65581 2 3 40 0 -1 43 3 42 4 1 2 3 400 65-1 43 583 42 4 1 2 3 4
  • 13. 1 0 -1 32 653 43 5842404 Bubble Sort: ExampleBubble Sort: Example 0 -1 1 2 653 43 58424043 -1 0 1 2 653 43 58424043 6 7 8 1 2 0 3-1 3 40 6543 584245
  • 14. Bubble Sort: AnalysisBubble Sort: Analysis • Running time: o Worst case: O(N2 ) o Best case: O(N) • Variant: o bi-directional bubble sort • original bubble sort: only works to one direction • bi-directional bubble sort: works back and forth.
  • 15. Selection Sort: IdeaSelection Sort: Idea 1. We have two group of items: o sorted group, and o unsorted group 1. Initially, all items are in the unsorted group. The sorted group is empty. o We assume that items in the unsorted group unsorted. o We have to keep items in the sorted group sorted.
  • 16. Selection Sort: ContSelection Sort: Cont’’dd 1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group. 2. Repeat the process until the unsorted group becomes empty.
  • 17. 4240 2 1 3 3 4 0 -1 655843 40 2 1 43 3 4 0 -1 42 65583 40 2 1 43 3 4 0 -1 58 3 6542 40 2 1 43 3 65 0 -1 58 3 42 4 Selection Sort: ExampleSelection Sort: Example
  • 18. 4240 2 1 3 3 4 0 655843-1 42-1 2 1 3 3 4 0 65584340 42-1 2 1 3 3 4 655843400 42-1 2 1 0 3 4 655843403 Selection Sort: ExampleSelection Sort: Example
  • 19. 1 42-1 2 1 3 4 6558434030 42-1 0 3 4 6558434032 1 42-1 0 3 4 6558434032 1 420 3 4 6558434032-1 1 420 3 4 6558434032-1 Selection Sort: ExampleSelection Sort: Example
  • 20. Selection Sort: AnalysisSelection Sort: Analysis • Running time: o Worst case: O(N2 ) o Best case: O(N2 )
  • 21. Insertion Sort: IdeaInsertion Sort: Idea 1. We have two group of items: o sorted group, and o unsorted group 1. Initially, all items in the unsorted group and the sorted group is empty. o We assume that items in the unsorted group unsorted. o We have to keep items in the sorted group sorted. 1. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property. 2. Repeat the process until the unsorted group becomes empty.
  • 22. 40 2 1 43 3 65 0 -1 58 3 42 4 2 40 1 43 3 65 0 -1 58 3 42 4 1 2 40 43 3 65 0 -1 58 3 42 4 40 Insertion Sort: ExampleInsertion Sort: Example
  • 23. 1 2 3 40 43 65 0 -1 58 3 42 4 1 2 40 43 3 65 0 -1 58 3 42 4 1 2 3 40 43 65 0 -1 58 3 42 4 Insertion Sort: ExampleInsertion Sort: Example
  • 24. 1 2 3 40 43 65 0 -1 58 3 42 4 1 2 3 40 43 650 -1 58 3 42 4 1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1 Insertion Sort: ExampleInsertion Sort: Example
  • 25. 1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1 1 2 3 40 43 650 58 42 41 2 3 3 43 650-1 5840 43 65 1 2 3 40 43 650 42 41 2 3 3 43 650-1 5840 43 65 Insertion Sort: ExampleInsertion Sort: Example 1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
  • 26. Insertion Sort: AnalysisInsertion Sort: Analysis • Running time analysis: o Worst case: O(N2 ) o Best case: O(N)
  • 27. A Lower BoundA Lower Bound • Bubble Sort, Selection Sort, Insertion Sort all have worst case of O(N2 ). • Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N2 ) • In other words, this is a lower bound!
  • 28. MergesortMergesort •Mergesort (divide-and-conquer) o Divide array into two halves. A L G O R I T H M S divideA L G O R I T H M S
  • 29. MergesortMergesort •Mergesort (divide-and-conquer) o Divide array into two halves. o Recursively sort each half. sort A L G O R I T H M S divideA L G O R I T H M S A G L O R H I M S T
  • 30. MergesortMergesort •Mergesort (divide-and-conquer) o Divide array into two halves. o Recursively sort each half. o Merge two halves to make sorted whole. merge sort A L G O R I T H M S divideA L G O R I T H M S A G L O R H I M S T A G H I L M O R S T
  • 31. auxiliary array smallest smallest A G L O R H I M S T MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. A
  • 32. auxiliary array smallest smallest A G L O R H I M S T A MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. G
  • 33. auxiliary array smallest smallest A G L O R H I M S T A G MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. H
  • 34. auxiliary array smallest smallest A G L O R H I M S T A G H MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. I
  • 35. auxiliary array smallest smallest A G L O R H I M S T A G H I MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. L
  • 36. auxiliary array smallest smallest A G L O R H I M S T A G H I L MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. M
  • 37. auxiliary array smallest smallest A G L O R H I M S T A G H I L M MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. O
  • 38. auxiliary array smallest smallest A G L O R H I M S T A G H I L M O MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. R
  • 39. auxiliary array first half exhausted smallest A G L O R H I M S T A G H I L M O R MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. S
  • 40. auxiliary array first half exhausted smallest A G L O R H I M S T A G H I L M O R S MergingMerging •Merge. o Keep track of smallest element in each sorted half. o Insert smallest of two elements into auxiliary array. o Repeat until done. T
  • 41. Notes on QuicksortNotes on Quicksort • Quicksort is more widely used than any other sort. • Quicksort is well-studied, not difficult to implement, works well on a variety of data, and consumes fewer resources that other sorts in nearly all situations. • Quicksort is O(n*log n) time, and O(log n) additional space due to recursion.
  • 42. Quicksort AlgorithmQuicksort Algorithm • Quicksort is a divide-and-conquer method for sorting. It works by partitioning an array into parts, then sorting each part independently. • The crux of the problem is how to partition the array such that the following conditions are true: o There is some element, a[i], where a[i] is in its final position. o For all l < i, a[l] < a[i]. o For all i < r, a[i] < a[r].
  • 43. Quicksort AlgorithmQuicksort Algorithm (cont)(cont) • As is typical with a recursive program, once you figure out how to divide your problem into smaller subproblems, the implementation is amazingly simple. int partition(Item a[], int l, int r); void quicksort(Item a[], int l, int r) { int i; if (r <= l) return; i = partition(a, l, r); quicksort(a, l, i-1); quicksort(a, i+1, r); } Partitioning with i
  • 46. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross Q U I C K S O R T I S C O O L partitioned partition element left right unpartitioned
  • 47. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross swap me Q U I C K S O R T I S C O O L partitioned partition element left right unpartitioned
  • 48. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L
  • 49. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L
  • 50. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L swap me
  • 51. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned C U I C K S O R T I S Q O O L
  • 52. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross swap me partitioned partition element left right unpartitioned C U I C K S O R T I S Q O O L
  • 53. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned swap me C U I C K S O R T I S Q O O L
  • 54. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned swap me C U I C K S O R T I S Q O O L swap me
  • 55. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 56. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 57. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • exchange • repeat until pointers cross partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 58. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 59. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross swap me partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 60. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross partitioned partition element left right unpartitioned swap me C I I C K S O R T U S Q O O L
  • 61. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross partitioned partition element left right unpartitioned swap me C I I C K S O R T U S Q O O L
  • 62. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross partitioned partition element left right unpartitioned swap me C I I C K S O R T U S Q O O L
  • 63. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross pointers cross swap with partitioning element partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L
  • 64. Partitioning in QuicksortPartitioning in Quicksort o How do we partition the array efficiently? • choose partition element to be rightmost element • scan from left for larger element • scan from right for smaller element • Exchange and repeat until pointers cross partitioned partition element left right unpartitioned partition is complete C I I C K L O R T U S Q O O S
  • 65. Sorting AlgorithmsSorting Algorithms animations Bubble Sort: Pairwise exchange Selection Sort: Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group Insertion Sort Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property. Merge Sort and quicksort Divide and conquer