SlideShare una empresa de Scribd logo
1 de 127
Different types of Sorting
Techniques used in Data
Structures
Sorting: 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 }
Sorting
 Sorting = ordering.
 Sorted = ordered based on a particular
way.
 Generally, collections of data are presented
in a sorted manner.
 Examples of Sorting:
 Words in a dictionary are sorted (and
case distinctions are ignored).
 Files in a directory are often listed in
sorted order.
 The index of a book is sorted (and case
distinctions are ignored).
Sorting: Cont’d
 Many banks provide statements
that list checks in increasing order
(by check number).
 In a newspaper, the calendar of events
in a schedule is generally sorted by date.
 Musical compact disks in a record store
are generally sorted by recording artist.
 Why?
 Imagine finding the phone number of
your friend in your mobile phone, but the
phone book is not sorted.
Review of Complexity
Most of the primary sorting algorithms
run on different space and time
complexity.
Time Complexity is defined to be the time
the computer takes to run a program (or
algorithm in our case).
Space complexity is defined to be the
amount of memory the computer needs
to run a program.
Complexity (cont.)
Complexity in general, measures the
algorithms efficiency in internal factors
such as the time needed to run an
algorithm.
External Factors (not related to
complexity):
Size of the input of the algorithm
Speed of the Computer
Quality of the Compiler
Types 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: Idea
 Idea: bubble in water.
 Bubble in water moves upward. Why?
 How?
 When a bubble moves upward, the water
from above will move downward to fill in
the space left by the bubble.
Bubble 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 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 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 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 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 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: Example
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
 Notice that at least one element will be
in the correct position each iteration.
1 0 -1 32 653 43 5842404
Bubble 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
Selection Sort: Idea
1. We have two group of items:
 sorted group, and
 unsorted group
1. Initially, all items are in the unsorted
group. The sorted group is empty.
 We assume that items in the unsorted
group unsorted.
 We have to keep items in the sorted
group sorted.
Selection Sort: Cont’d
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.
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted

Largest
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
DONE!
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: 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: 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: Example
Insertion Sort: Idea
 Idea: sorting cards.
 8 | 5 9 2 6 3
 5 8 | 9 2 6 3
 5 8 9 | 2 6 3
 2 5 8 9 | 6 3
 2 5 6 8 9 | 3
 2 3 5 6 8 9 |
Insertion Sort: Idea
1. We have two group of items:
 sorted group, and
 unsorted group
1. Initially, all items in the unsorted group and the sorted
group is empty.
 We assume that items in the unsorted group unsorted.
 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: 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: 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: 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: Example
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
Mergesort
Mergesort (divide-and-conquer)
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
Mergesort
Mergesort (divide-and-conquer)
Divide array into two halves.
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
Mergesort
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
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
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
A
auxiliary array
smallest smallest
A G L O R H I M S T
A
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
G
auxiliary array
smallest smallest
A G L O R H I M S T
A G
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
H
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
I
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
L
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
M
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 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
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 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
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 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
Merging
Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
T
Partitioning in Quicksort
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 Quicksort
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 crossswap me
Q U I C K S O R T I S C O O L
partitioned
partition element left
right
unpartitioned
Partitioning in Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 crossswap me
partitioned
partition element left
right
unpartitioned
C U I C K S O R T I S Q O O L
Partitioning in Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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 Quicksort
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
What is a “heap”?
 Definitions of heap:
1. A large area of memory from which the
programmer can allocate blocks as needed,
and deallocate them (or allow them to be
garbage collected) when no longer needed
2. A balanced, left-justified binary tree in
which no node has a value greater than the
value in its parent
 Heapsort uses the second definition
Balanced binary trees
 Recall:
 The depth of a node is its distance from the root
 The depth of a tree is the depth of the deepest node
 A binary tree of depth n is balanced if all the nodes at depths 0 through
n-2 have two children
Balanced Balanced Not balanced
n-2
n-1
n
Left-justified binary trees
 A balanced binary tree is left-
justified if:
 all the leaves are at the same
depth, or
 all the leaves at depth n+1 are to
the left of all the nodes at depth n
Left-justified Not left-justified
The heap property
 A node has the heap property if the
value in the node is as large as or
larger than the values in its children
 All leaf nodes automatically have the heap
property
 A binary tree is a heap if all nodes in it
have the heap property
12
8 3
Blue node has
heap property
12
8 12
Blue node has
heap property
12
8 14
Blue node does not
have heap property
siftUp
 Given a node that does not have the heap
property, you can give it the heap
property by exchanging its value with the
value of the larger child
 This is sometimes called sifting up
 Notice that the child may have lost the
heap property
14
8 12
Blue node has
heap property
12
8 14
Blue node does not
have heap property
Constructing a heap I
 A tree consisting of a single node is automatically a heap
 We construct a heap by adding nodes one at a time:
 Add the node just to the right of the rightmost node in the
deepest level
 If the deepest level is full, start a new level
 Examples:
Add a new
node here
Add a new
node here
Constructing a heap II
 Each time we add a node, we may destroy the heap property of its
parent node
 To fix this, we sift up
 But each time we sift up, the value of the topmost node in the sift may
increase, and this may destroy the heap property of its parent node
 We repeat the sifting up process, moving up in the tree, until either
 We reach nodes whose values don’t need to be swapped (because
the parent is still larger than both children), or
 We reach the root
Constructing a heap III
8 8
10
10
8
10
8 5
10
8 5
12
10
12 5
8
12
10 5
8
1 2 3
4
Other children are not affected
 The node containing 8 is not affected because its
parent gets larger, not smaller
 The node containing 5 is not affected because its
parent gets larger, not smaller
 The node containing 8 is still not affected because,
although its parent got smaller, its parent is still
greater than it was originally
12
10 5
8 14
12
14 5
8 10
14
12 5
8 10
A sample heap
 Here’s a sample binary tree after it has
been heapified
 Notice that heapified does not mean
sorted
 Heapifying does not change the shape of
the binary tree; this binary tree is
19
1418
22
321
14
119
15
25
1722
Removing the root
 Notice that the largest number is now in
the root
 Suppose we discard the root:
 How can we fix the binary tree so it is
once again balanced and left-justified?
 Solution: remove the rightmost leaf at the
deepest level and use it for the new root
19
1418
22
321
14
119
15
1722
11
The reHeap method I
 Our tree is balanced and left-justified, but
no longer a heap
 However, only the root lacks the heap
property
 We can siftUp() the root
 After doing this, one and only one of its
children may have lost the heap property
19
1418
22
321
14
9
15
1722
11
The reHeap method II
 Now the left child of the root (still the
number 11) lacks the heap property
 We can siftUp() this node
 After doing this, one and only one of its
children may have lost the heap property
19
1418
22
321
14
9
15
1711
22
The reHeap method III
 Now the right child of the left child of the
root (still the number 11) lacks the heap
property:
 We can siftUp() this node
 After doing this, one and only one of its
children may have lost the heap property
—but it doesn’t, because it’s a leaf
19
1418
11
321
14
9
15
1722
22
The reHeap method IV
 Our tree is once again a heap, because
every node in it has the heap property
 Once again, the largest (or a largest) value is
in the root
 We can repeat this process until the tree
becomes empty
19
1418
21
311
14
9
15
1722
22
Sorting
 What do heaps have to do with
sorting an array?
 Here’s the neat part:
 Because the binary tree is balanced and
left justified, it can be represented as an
array
 All our operations on binary trees can be
represented as operations on arrays
 To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
Mapping into an array
 Notice:
 The left child of index i is at index 2*i+1
 The right child of index i is at index 2*i+2
 Example: the children of node 3 (19) are 7
19
1418
22
321
14
119
15
25
1722
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
Removing and replacing the
root
 The “root” is the first element in the array
 The “rightmost node at the deepest level”
is the last element
 Swap them...
 ...And pretend that the last element in the
array no longer exists—that is, the “last
index” is 11 (9)
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
Reheap and repeat
 Reheap the root node (index 0, containing
11)...
 ...And again, remove and replace the root
node
 Remember, though, that the “last” array
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
Analysis I
 Here’s how the algorithm starts:
heapify the array;
 Heapifying the array: we add each of
n nodes
 Each node has to be sifted up, possibly
as far as the root
 Since the binary tree is perfectly balanced,
sifting up a single node takes O(log n) time
 Since we do this n times, heapifying
takes n*O(log n) time, that is, O(n log n)
time
Analysis II
 Here’s the rest of the algorithm:
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
 We do the while loop n times (actually,
n-1 times), because we remove one of
the n nodes each time
 Removing and replacing the root takes
O(1) time
 Therefore, the total time is n times
Analysis III
 To reheap the root node, we have to
follow one path from the root to a
leaf node (and we might stop before
we reach a leaf)
 The binary tree is perfectly balanced
 Therefore, this path is O(log n) long
 And we only do O(1) operations at each
node
 Therefore, reheaping takes O(log n)
times
Analysis IV
 Here’s the algorithm again:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
 We have seen that heapifying takes
O(n log n) time
 The while loop takes O(n log n) time
 The total time is therefore O(n log n) +
O(n log n)
The End
40 2 1 43 3 65 0 -1 58 3 42 4
Original:
5-sort: Sort items with distance 5 element:
40 2 1 43 3 65 0 -1 58 3 42 4
Shell Sort: Idea
Donald Shell (1959): Exchange items that are far apart!
40 2 1 43 3 65 0 -1 58 3 42 4
Original:
40 0 -1 43 3 42 2 1 58 3 65 4
After 5-sort:
2 0 -1 3 1 4 40 3 42 43 65 58
After 3-sort:
Shell Sort: Example
After 1-sort:
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
Shell Sort: Gap Values
 Gap: the distance between items
being sorted.
 As we progress, the gap
decreases. Shell Sort is also
called Diminishing Gap Sort.
 Shell proposed starting gap of
N/2, halving at each step.
 There are many ways of choosing
the next gap.
Shell's Odd Gaps Only Dividing by 2.2
1000 122 11 11 9
2000 483 26 21 23
4000 1936 61 59 54
8000 7950 153 141 114
16000 32560 358 322 269
32000 131911 869 752 575
64000 520000 2091 1705 1249
Shellsort
N
Insertion
Sort
O(N3/2
)? O(N5/4
)? O(N7/6
)?
Shell Sort: Analysis
So we have 3 nested loops, but Shell Sort is still better
than Insertion Sort! Why?
Generic Sort
 So far we have methods to sort
integers. What about Strings?
Employees? Cookies?
 A new method for each class? No!
 In order to be sorted, objects should
be comparable (less than, equal,
greater than).
 Solution:
 use an interface that has a method to
compare two objects.
Other kinds of sort
 Heap sort. We will discuss this after
tree.
 Postman sort / Radix Sort.
 etc.

Más contenido relacionado

Similar a Sorting algos

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsZaid Hameed
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01Dusan Vuckovic
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Unit 12: Probability
Unit 12: ProbabilityUnit 12: Probability
Unit 12: ProbabilityRenegarmath
 
Chapter2.2
Chapter2.2Chapter2.2
Chapter2.2nglaze10
 
Rt graphical representation
Rt graphical representationRt graphical representation
Rt graphical representationRinchen
 
Lesson Presentation - Compare Fractions Final.pptx
Lesson Presentation - Compare Fractions Final.pptxLesson Presentation - Compare Fractions Final.pptx
Lesson Presentation - Compare Fractions Final.pptxEmileZamore
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Scaled v. ordinal v. nominal data(3)
Scaled v. ordinal v. nominal data(3)Scaled v. ordinal v. nominal data(3)
Scaled v. ordinal v. nominal data(3)Ken Plummer
 
De vry math 221 all discussion+ilbs latest 2016 november
De vry math 221 all discussion+ilbs latest 2016 novemberDe vry math 221 all discussion+ilbs latest 2016 november
De vry math 221 all discussion+ilbs latest 2016 novemberlenasour
 
Data Science: The Product Manager's Primer
Data Science: The Product Manager's PrimerData Science: The Product Manager's Primer
Data Science: The Product Manager's PrimerProduct School
 
De vry math 221 all discussion+ilbs latest 2016 november 1
De vry math 221 all discussion+ilbs latest 2016 november 1De vry math 221 all discussion+ilbs latest 2016 november 1
De vry math 221 all discussion+ilbs latest 2016 november 1lenasour
 
Introduction to fractions and concepts
Introduction to fractions and conceptsIntroduction to fractions and concepts
Introduction to fractions and conceptsMartha Ardila Ibarra
 

Similar a Sorting algos (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble 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)
 
Bubblesorting
BubblesortingBubblesorting
Bubblesorting
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Unit 12: Probability
Unit 12: ProbabilityUnit 12: Probability
Unit 12: Probability
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Chapter2.2
Chapter2.2Chapter2.2
Chapter2.2
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Rt graphical representation
Rt graphical representationRt graphical representation
Rt graphical representation
 
Lesson Presentation - Compare Fractions Final.pptx
Lesson Presentation - Compare Fractions Final.pptxLesson Presentation - Compare Fractions Final.pptx
Lesson Presentation - Compare Fractions Final.pptx
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Scaled v. ordinal v. nominal data(3)
Scaled v. ordinal v. nominal data(3)Scaled v. ordinal v. nominal data(3)
Scaled v. ordinal v. nominal data(3)
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
De vry math 221 all discussion+ilbs latest 2016 november
De vry math 221 all discussion+ilbs latest 2016 novemberDe vry math 221 all discussion+ilbs latest 2016 november
De vry math 221 all discussion+ilbs latest 2016 november
 
Data Science: The Product Manager's Primer
Data Science: The Product Manager's PrimerData Science: The Product Manager's Primer
Data Science: The Product Manager's Primer
 
De vry math 221 all discussion+ilbs latest 2016 november 1
De vry math 221 all discussion+ilbs latest 2016 november 1De vry math 221 all discussion+ilbs latest 2016 november 1
De vry math 221 all discussion+ilbs latest 2016 november 1
 
Introduction to fractions and concepts
Introduction to fractions and conceptsIntroduction to fractions and concepts
Introduction to fractions and concepts
 

Más de Omair Imtiaz Ansari

Más de Omair Imtiaz Ansari (7)

Matlab GUI
Matlab GUIMatlab GUI
Matlab GUI
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Circular wait - Operating Systems
Circular wait - Operating SystemsCircular wait - Operating Systems
Circular wait - Operating Systems
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Processor simulator - Computer Archetecture
Processor simulator - Computer ArchetectureProcessor simulator - Computer Archetecture
Processor simulator - Computer Archetecture
 
Communication Skills
Communication SkillsCommunication Skills
Communication Skills
 
Computer Archetecture & Organization
Computer Archetecture & OrganizationComputer Archetecture & Organization
Computer Archetecture & Organization
 

Último

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Último (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

Sorting algos

  • 1. Different types of Sorting Techniques used in Data Structures
  • 2. Sorting: 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. Sorting  Sorting = ordering.  Sorted = ordered based on a particular way.  Generally, collections of data are presented in a sorted manner.  Examples of Sorting:  Words in a dictionary are sorted (and case distinctions are ignored).  Files in a directory are often listed in sorted order.  The index of a book is sorted (and case distinctions are ignored).
  • 4. Sorting: Cont’d  Many banks provide statements that list checks in increasing order (by check number).  In a newspaper, the calendar of events in a schedule is generally sorted by date.  Musical compact disks in a record store are generally sorted by recording artist.  Why?  Imagine finding the phone number of your friend in your mobile phone, but the phone book is not sorted.
  • 5. Review of Complexity Most of the primary sorting algorithms run on different space and time complexity. Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case). Space complexity is defined to be the amount of memory the computer needs to run a program.
  • 6. Complexity (cont.) Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm. External Factors (not related to complexity): Size of the input of the algorithm Speed of the Computer Quality of the Compiler
  • 7. Types 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
  • 8. Bubble Sort: Idea  Idea: bubble in water.  Bubble in water moves upward. Why?  How?  When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.
  • 9. Bubble 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.
  • 10. Bubble 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
  • 11. Bubble 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.
  • 12. Bubble 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
  • 13. Bubble 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
  • 14. Bubble 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.
  • 15. Bubble Sort: Example 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  Notice that at least one element will be in the correct position each iteration.
  • 16. 1 0 -1 32 653 43 5842404 Bubble 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
  • 17. Selection Sort: Idea 1. We have two group of items:  sorted group, and  unsorted group 1. Initially, all items are in the unsorted group. The sorted group is empty.  We assume that items in the unsorted group unsorted.  We have to keep items in the sorted group sorted.
  • 18. Selection Sort: Cont’d 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.
  • 19. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 20. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 21. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 22. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 23. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 24. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 25. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 26. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted  Largest
  • 27. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 28. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 29. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 30. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 31. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 32. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 33. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 34. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted  Largest
  • 35. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 36. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 37. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 38. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 39. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 40. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 41. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 42. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 43. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 44. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 45. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 46. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 47. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 48. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 49. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 50. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 51. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 52. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 53. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted
  • 54. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted DONE!
  • 55. 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: Example
  • 56. 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: Example
  • 57. 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: Example
  • 58. Insertion Sort: Idea  Idea: sorting cards.  8 | 5 9 2 6 3  5 8 | 9 2 6 3  5 8 9 | 2 6 3  2 5 8 9 | 6 3  2 5 6 8 9 | 3  2 3 5 6 8 9 |
  • 59. Insertion Sort: Idea 1. We have two group of items:  sorted group, and  unsorted group 1. Initially, all items in the unsorted group and the sorted group is empty.  We assume that items in the unsorted group unsorted.  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.
  • 60. 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: Example
  • 61. 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: Example
  • 62. 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: Example
  • 63. 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: Example 1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
  • 64. Mergesort Mergesort (divide-and-conquer) 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
  • 65. Mergesort Mergesort (divide-and-conquer) Divide array into two halves. 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
  • 66. Mergesort Mergesort (divide-and-conquer) Divide array into two halves. Recursively sort each half. 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
  • 67. auxiliary array smallest smallest A G L O R H I M S T Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. A
  • 68. auxiliary array smallest smallest A G L O R H I M S T A Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. G
  • 69. auxiliary array smallest smallest A G L O R H I M S T A G Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. H
  • 70. auxiliary array smallest smallest A G L O R H I M S T A G H Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. I
  • 71. auxiliary array smallest smallest A G L O R H I M S T A G H I Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. L
  • 72. auxiliary array smallest smallest A G L O R H I M S T A G H I L Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. M
  • 73. auxiliary array smallest smallest A G L O R H I M S T A G H I L M Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. O
  • 74. auxiliary array smallest smallest A G L O R H I M S T A G H I L M O Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. R
  • 75. auxiliary array first half exhausted smallest A G L O R H I M S T A G H I L M O R Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. S
  • 76. 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 Merging Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. T
  • 77.
  • 78.
  • 79. Partitioning in Quicksort 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
  • 80. Partitioning in Quicksort 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 crossswap me Q U I C K S O R T I S C O O L partitioned partition element left right unpartitioned
  • 81. Partitioning in Quicksort 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
  • 82. Partitioning in Quicksort 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
  • 83. Partitioning in Quicksort 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
  • 84. Partitioning in Quicksort 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
  • 85. Partitioning in Quicksort 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 crossswap me partitioned partition element left right unpartitioned C U I C K S O R T I S Q O O L
  • 86. Partitioning in Quicksort 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
  • 87. Partitioning in Quicksort 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
  • 88. Partitioning in Quicksort 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
  • 89. Partitioning in Quicksort 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
  • 90. Partitioning in Quicksort 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
  • 91. Partitioning in Quicksort 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
  • 92. Partitioning in Quicksort 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
  • 93. Partitioning in Quicksort 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
  • 94. Partitioning in Quicksort 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
  • 95. Partitioning in Quicksort 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
  • 96. Partitioning in Quicksort 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
  • 97. Partitioning in Quicksort 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
  • 98. What is a “heap”?  Definitions of heap: 1. A large area of memory from which the programmer can allocate blocks as needed, and deallocate them (or allow them to be garbage collected) when no longer needed 2. A balanced, left-justified binary tree in which no node has a value greater than the value in its parent  Heapsort uses the second definition
  • 99. Balanced binary trees  Recall:  The depth of a node is its distance from the root  The depth of a tree is the depth of the deepest node  A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Balanced Balanced Not balanced n-2 n-1 n
  • 100. Left-justified binary trees  A balanced binary tree is left- justified if:  all the leaves are at the same depth, or  all the leaves at depth n+1 are to the left of all the nodes at depth n Left-justified Not left-justified
  • 101. The heap property  A node has the heap property if the value in the node is as large as or larger than the values in its children  All leaf nodes automatically have the heap property  A binary tree is a heap if all nodes in it have the heap property 12 8 3 Blue node has heap property 12 8 12 Blue node has heap property 12 8 14 Blue node does not have heap property
  • 102. siftUp  Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child  This is sometimes called sifting up  Notice that the child may have lost the heap property 14 8 12 Blue node has heap property 12 8 14 Blue node does not have heap property
  • 103. Constructing a heap I  A tree consisting of a single node is automatically a heap  We construct a heap by adding nodes one at a time:  Add the node just to the right of the rightmost node in the deepest level  If the deepest level is full, start a new level  Examples: Add a new node here Add a new node here
  • 104. Constructing a heap II  Each time we add a node, we may destroy the heap property of its parent node  To fix this, we sift up  But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node  We repeat the sifting up process, moving up in the tree, until either  We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or  We reach the root
  • 105. Constructing a heap III 8 8 10 10 8 10 8 5 10 8 5 12 10 12 5 8 12 10 5 8 1 2 3 4
  • 106. Other children are not affected  The node containing 8 is not affected because its parent gets larger, not smaller  The node containing 5 is not affected because its parent gets larger, not smaller  The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally 12 10 5 8 14 12 14 5 8 10 14 12 5 8 10
  • 107. A sample heap  Here’s a sample binary tree after it has been heapified  Notice that heapified does not mean sorted  Heapifying does not change the shape of the binary tree; this binary tree is 19 1418 22 321 14 119 15 25 1722
  • 108. Removing the root  Notice that the largest number is now in the root  Suppose we discard the root:  How can we fix the binary tree so it is once again balanced and left-justified?  Solution: remove the rightmost leaf at the deepest level and use it for the new root 19 1418 22 321 14 119 15 1722 11
  • 109. The reHeap method I  Our tree is balanced and left-justified, but no longer a heap  However, only the root lacks the heap property  We can siftUp() the root  After doing this, one and only one of its children may have lost the heap property 19 1418 22 321 14 9 15 1722 11
  • 110. The reHeap method II  Now the left child of the root (still the number 11) lacks the heap property  We can siftUp() this node  After doing this, one and only one of its children may have lost the heap property 19 1418 22 321 14 9 15 1711 22
  • 111. The reHeap method III  Now the right child of the left child of the root (still the number 11) lacks the heap property:  We can siftUp() this node  After doing this, one and only one of its children may have lost the heap property —but it doesn’t, because it’s a leaf 19 1418 11 321 14 9 15 1722 22
  • 112. The reHeap method IV  Our tree is once again a heap, because every node in it has the heap property  Once again, the largest (or a largest) value is in the root  We can repeat this process until the tree becomes empty 19 1418 21 311 14 9 15 1722 22
  • 113. Sorting  What do heaps have to do with sorting an array?  Here’s the neat part:  Because the binary tree is balanced and left justified, it can be represented as an array  All our operations on binary trees can be represented as operations on arrays  To sort: heapify the array; while the array isn’t empty { remove and replace the root;
  • 114. Mapping into an array  Notice:  The left child of index i is at index 2*i+1  The right child of index i is at index 2*i+2  Example: the children of node 3 (19) are 7 19 1418 22 321 14 119 15 25 1722 25 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 12
  • 115. Removing and replacing the root  The “root” is the first element in the array  The “rightmost node at the deepest level” is the last element  Swap them...  ...And pretend that the last element in the array no longer exists—that is, the “last index” is 11 (9) 25 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 12 11 22 17 19 22 14 15 18 14 21 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12
  • 116. Reheap and repeat  Reheap the root node (index 0, containing 11)...  ...And again, remove and replace the root node  Remember, though, that the “last” array 22 22 17 19 21 14 15 18 14 11 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12 9 22 17 19 22 14 15 18 14 21 3 22 25 0 1 2 3 4 5 6 7 8 9 10 11 12 11 22 17 19 22 14 15 18 14 21 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12
  • 117. Analysis I  Here’s how the algorithm starts: heapify the array;  Heapifying the array: we add each of n nodes  Each node has to be sifted up, possibly as far as the root  Since the binary tree is perfectly balanced, sifting up a single node takes O(log n) time  Since we do this n times, heapifying takes n*O(log n) time, that is, O(n log n) time
  • 118. Analysis II  Here’s the rest of the algorithm: while the array isn’t empty { remove and replace the root; reheap the new root node; }  We do the while loop n times (actually, n-1 times), because we remove one of the n nodes each time  Removing and replacing the root takes O(1) time  Therefore, the total time is n times
  • 119. Analysis III  To reheap the root node, we have to follow one path from the root to a leaf node (and we might stop before we reach a leaf)  The binary tree is perfectly balanced  Therefore, this path is O(log n) long  And we only do O(1) operations at each node  Therefore, reheaping takes O(log n) times
  • 120. Analysis IV  Here’s the algorithm again: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; }  We have seen that heapifying takes O(n log n) time  The while loop takes O(n log n) time  The total time is therefore O(n log n) + O(n log n)
  • 122. 40 2 1 43 3 65 0 -1 58 3 42 4 Original: 5-sort: Sort items with distance 5 element: 40 2 1 43 3 65 0 -1 58 3 42 4 Shell Sort: Idea Donald Shell (1959): Exchange items that are far apart!
  • 123. 40 2 1 43 3 65 0 -1 58 3 42 4 Original: 40 0 -1 43 3 42 2 1 58 3 65 4 After 5-sort: 2 0 -1 3 1 4 40 3 42 43 65 58 After 3-sort: Shell Sort: Example After 1-sort: 1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
  • 124. Shell Sort: Gap Values  Gap: the distance between items being sorted.  As we progress, the gap decreases. Shell Sort is also called Diminishing Gap Sort.  Shell proposed starting gap of N/2, halving at each step.  There are many ways of choosing the next gap.
  • 125. Shell's Odd Gaps Only Dividing by 2.2 1000 122 11 11 9 2000 483 26 21 23 4000 1936 61 59 54 8000 7950 153 141 114 16000 32560 358 322 269 32000 131911 869 752 575 64000 520000 2091 1705 1249 Shellsort N Insertion Sort O(N3/2 )? O(N5/4 )? O(N7/6 )? Shell Sort: Analysis So we have 3 nested loops, but Shell Sort is still better than Insertion Sort! Why?
  • 126. Generic Sort  So far we have methods to sort integers. What about Strings? Employees? Cookies?  A new method for each class? No!  In order to be sorted, objects should be comparable (less than, equal, greater than).  Solution:  use an interface that has a method to compare two objects.
  • 127. Other kinds of sort  Heap sort. We will discuss this after tree.  Postman sort / Radix Sort.  etc.