SlideShare a Scribd company logo
1 of 65
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
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

Viewers also liked

Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceJohn Lynch
 
History of Creationism, Parts II & III
History of Creationism, Parts II & IIIHistory of Creationism, Parts II & III
History of Creationism, Parts II & IIIJohn Lynch
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011 Mike Blumenthal
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian RevolutionJohn Lynch
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmAshim Sikder
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2mtaft
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionJohn Lynch
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bShahi Raz Akhtar
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical RevolutionJohn Lynch
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1) Dipoceanov Esrever
 
One Long Argument
One Long ArgumentOne Long Argument
One Long ArgumentJohn Lynch
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Saba SEO
 
National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DCShivakumar Patil
 

Viewers also liked (20)

How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
History of Creationism, Parts II & III
History of Creationism, Parts II & IIIHistory of Creationism, Parts II & III
History of Creationism, Parts II & III
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & Evolution
 
Chapter one
Chapter oneChapter one
Chapter one
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_b
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical Revolution
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
 
One Long Argument
One Long ArgumentOne Long Argument
One Long Argument
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.
 
National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DC
 
Ds 4
Ds 4Ds 4
Ds 4
 
Google
GoogleGoogle
Google
 

Similar to Sorting algorithms

Similar to Sorting algorithms (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
sorting_part1.ppt
sorting_part1.pptsorting_part1.ppt
sorting_part1.ppt
 
Gmat
GmatGmat
Gmat
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
 
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting
SortingSorting
Sorting
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to others
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
cs1311lecture16wdl.ppt
cs1311lecture16wdl.pptcs1311lecture16wdl.ppt
cs1311lecture16wdl.ppt
 
Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort Python
 
Bubble Sort.ppt
Bubble Sort.pptBubble Sort.ppt
Bubble Sort.ppt
 

More from Edward Blurock

KEOD23-JThermodynamcsCloud
KEOD23-JThermodynamcsCloudKEOD23-JThermodynamcsCloud
KEOD23-JThermodynamcsCloudEdward Blurock
 
BlurockPresentation-KEOD2023
BlurockPresentation-KEOD2023BlurockPresentation-KEOD2023
BlurockPresentation-KEOD2023Edward Blurock
 
ChemConnect: Poster for European Combustion Meeting 2017
ChemConnect: Poster for European Combustion Meeting 2017ChemConnect: Poster for European Combustion Meeting 2017
ChemConnect: Poster for European Combustion Meeting 2017Edward Blurock
 
ChemConnect: SMARTCATS presentation
ChemConnect: SMARTCATS presentationChemConnect: SMARTCATS presentation
ChemConnect: SMARTCATS presentationEdward Blurock
 
EU COST Action CM1404: WG€ - Efficient Data Exchange
EU COST Action CM1404: WG€ - Efficient Data ExchangeEU COST Action CM1404: WG€ - Efficient Data Exchange
EU COST Action CM1404: WG€ - Efficient Data ExchangeEdward Blurock
 
ChemConnect: Viewing the datasets in the repository
ChemConnect: Viewing the datasets in the repositoryChemConnect: Viewing the datasets in the repository
ChemConnect: Viewing the datasets in the repositoryEdward Blurock
 
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...Edward Blurock
 
Poster: Characterizing Ignition behavior through morphing to generic curves
Poster: Characterizing Ignition behavior through morphing to generic curvesPoster: Characterizing Ignition behavior through morphing to generic curves
Poster: Characterizing Ignition behavior through morphing to generic curvesEdward Blurock
 
Poster: Very Open Data Project
Poster: Very Open Data ProjectPoster: Very Open Data Project
Poster: Very Open Data ProjectEdward Blurock
 
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISATPoster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISATEdward Blurock
 
Characterization Ignition Behavior through Morphing to Generic Ignition Curves
Characterization Ignition Behavior through Morphing to Generic Ignition CurvesCharacterization Ignition Behavior through Morphing to Generic Ignition Curves
Characterization Ignition Behavior through Morphing to Generic Ignition CurvesEdward Blurock
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculusEdward Blurock
 
Imperative programming
Imperative programmingImperative programming
Imperative programmingEdward Blurock
 
Database normalization
Database normalizationDatabase normalization
Database normalizationEdward Blurock
 
Generalization abstraction
Generalization abstractionGeneralization abstraction
Generalization abstractionEdward Blurock
 

More from Edward Blurock (20)

KEOD23-JThermodynamcsCloud
KEOD23-JThermodynamcsCloudKEOD23-JThermodynamcsCloud
KEOD23-JThermodynamcsCloud
 
BlurockPresentation-KEOD2023
BlurockPresentation-KEOD2023BlurockPresentation-KEOD2023
BlurockPresentation-KEOD2023
 
KEOD-2023-Poster.pptx
KEOD-2023-Poster.pptxKEOD-2023-Poster.pptx
KEOD-2023-Poster.pptx
 
ChemConnect: Poster for European Combustion Meeting 2017
ChemConnect: Poster for European Combustion Meeting 2017ChemConnect: Poster for European Combustion Meeting 2017
ChemConnect: Poster for European Combustion Meeting 2017
 
ChemConnect: SMARTCATS presentation
ChemConnect: SMARTCATS presentationChemConnect: SMARTCATS presentation
ChemConnect: SMARTCATS presentation
 
EU COST Action CM1404: WG€ - Efficient Data Exchange
EU COST Action CM1404: WG€ - Efficient Data ExchangeEU COST Action CM1404: WG€ - Efficient Data Exchange
EU COST Action CM1404: WG€ - Efficient Data Exchange
 
ChemConnect: Viewing the datasets in the repository
ChemConnect: Viewing the datasets in the repositoryChemConnect: Viewing the datasets in the repository
ChemConnect: Viewing the datasets in the repository
 
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
ChemConnect: Characterizing CombusAon KineAc Data with ontologies and meta-­‐...
 
Poster: Characterizing Ignition behavior through morphing to generic curves
Poster: Characterizing Ignition behavior through morphing to generic curvesPoster: Characterizing Ignition behavior through morphing to generic curves
Poster: Characterizing Ignition behavior through morphing to generic curves
 
Poster: Very Open Data Project
Poster: Very Open Data ProjectPoster: Very Open Data Project
Poster: Very Open Data Project
 
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISATPoster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
Poster: Adaptive On-­‐the-­‐fly Regression Tabula@on: Beyond ISAT
 
Characterization Ignition Behavior through Morphing to Generic Ignition Curves
Characterization Ignition Behavior through Morphing to Generic Ignition CurvesCharacterization Ignition Behavior through Morphing to Generic Ignition Curves
Characterization Ignition Behavior through Morphing to Generic Ignition Curves
 
Paradigms
ParadigmsParadigms
Paradigms
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
Imperative programming
Imperative programmingImperative programming
Imperative programming
 
Programming Languages
Programming LanguagesProgramming Languages
Programming Languages
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Database normalization
Database normalizationDatabase normalization
Database normalization
 
Generalization abstraction
Generalization abstractionGeneralization abstraction
Generalization abstraction
 
Overview
OverviewOverview
Overview
 

Recently uploaded

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

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
  • 44.
  • 45.
  • 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