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

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