0% found this document useful (0 votes)
32 views221 pages

Bubble Sort: Slide 1

Bubble sort is an algorithm that sorts a collection of elements by repeatedly "bubbling up" the largest value to the end of the collection. It works by traversing the collection from front to back, comparing adjacent elements and swapping them if they are in the wrong order. This process is repeated for a total of N-1 passes, where N is the number of elements, to fully sort the collection. The algorithm uses a boolean flag to check if any swapping occurred on a given pass, and can stop early if the collection is already sorted.

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views221 pages

Bubble Sort: Slide 1

Bubble sort is an algorithm that sorts a collection of elements by repeatedly "bubbling up" the largest value to the end of the collection. It works by traversing the collection from front to back, comparing adjacent elements and swapping them if they are in the wrong order. This process is repeated for a total of N-1 passes, where N is the number of elements, to fully sort the collection. The algorithm uses a boolean flag to check if any swapping occurred on a given pass, and can stop early if the collection is already sorted.

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 221

BUBBLE SORT

Slide 1
SORTING
 Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101

Slide 2
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

77 42 35 12 101 5

Slide 3
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6
42 Swap77
42 35 12 101 5
77

Slide 4
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35Swap35
77 77 12 101 5

Slide 5
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12Swap12
77 77 101 5

Slide 6
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap

Slide 7
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap101
101 5

Slide 8
"BUBBLING UP" THE LARGEST
ELEMENT
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Slide 9
THE “BUBBLE UP” ALGORITHM
index <- 1
last_compare_at <- n – 1

loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop

Slide 10
LB

NO, SWAP ISN’T BUILT IN.


Procedure Swap(a, b isoftype in/out
Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap

Slide 11
ITEMS OF INTEREST
 Notice that only the largest value is
correctly placed
 All other values are still out of order
 So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Slide 12
REPEAT “BUBBLE UP” HOW MANY TIMES?
 If we have N elements…

 And if each time we bubble an element,


we place it in its correct location…

 Then we repeat the “bubble up”


process N – 1 times.

 This guarantees we’ll correctly


place all N elements.

Slide 13
“BUBBLING” ALL THE
ELEMENTS
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1

12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101

Slide 14
REDUCING THE NUMBER OF
COMPARISONS
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101

Slide 15
REDUCING THE NUMBER OF COMPARISONS
 On the Nth “bubble up”, we only need to
do MAX-N comparisons.

 For example:
 Thisis the 4th “bubble up”
 MAX is 6
 Thus we have 2 comparisons to do

1 2 3 4 5 6
12 35 5 42 77 101

Slide 16
ALREADY SORTED
COLLECTIONS?
 What if the collection was already sorted?
 What if only a few elements were out of place
and after a couple of “bubble ups,” the
collection was sorted?

 We want to be able to detect this


and “stop early”!
1 2 3 4 5 6
5 12 35 42 77 101

Slide 17
USING A BOOLEAN “FLAG”
 We can use a boolean variable to
determine if any swapping occurred during
the “bubble up.”

 If no swapping occurred, then we know


that the collection is already sorted!

 This boolean “flag” needs to be reset after


each “bubble up.”

Slide 18
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 19
AN ANIMATED EXAMPLE
N 8 did_swap false
to_do 7
index 1

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 20
AN ANIMATED EXAMPLE
N 8 did_swap false
to_do 7
index 1

Swap

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 21
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 1

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 22
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 2

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 23
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 2

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 24
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 2

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 25
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 3

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 26
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 3

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8

Slide 27
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 3

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8

Slide 28
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 4

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8

Slide 29
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8

Slide 30
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8

Slide 31
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 5

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8

Slide 32
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8

Slide 33
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8

Slide 34
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 6

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8

Slide 35
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8

Slide 36
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8

Slide 37
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 7

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8

Slide 38
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8

Slide 39
AN ANIMATED EXAMPLE
N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 40
AFTER FIRST PASS OF OUTER
LOOP
N 8 did_swap true
to_do 7
index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 41
THE SECOND “BUBBLE UP”
N 8 did_swap false
to_do 6
index 1

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 42
THE SECOND “BUBBLE UP”
N 8 did_swap false
to_do 6
index 1

No Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 43
THE SECOND “BUBBLE UP”
N 8 did_swap false
to_do 6
index 2

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 44
THE SECOND “BUBBLE UP”
N 8 did_swap false
to_do 6
index 2

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 45
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 2

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 46
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 3

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 47
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 3

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8

Slide 48
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 3

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8

Slide 49
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 4

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8

Slide 50
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 4

No Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8

Slide 51
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 5

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8

Slide 52
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8

Slide 53
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8

Slide 54
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 6

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8

Slide 55
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8

Slide 56
THE SECOND “BUBBLE UP”
N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 57
AFTER SECOND PASS OF OUTER
LOOP
N 8 did_swap true
to_do 6
index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 58
THE THIRD “BUBBLE UP”
N 8 did_swap false
to_do 5
index 1

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 59
THE THIRD “BUBBLE UP”
N 8 did_swap false
to_do 5
index 1

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 60
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 1

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 61
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 2

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 62
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 2

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 63
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 2

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 64
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 3

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 65
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 3

No Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 66
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 4

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 67
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8

Slide 68
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8

Slide 69
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 5

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8

Slide 70
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8

Slide 71
THE THIRD “BUBBLE UP”
N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 72
AFTER THIRD PASS OF OUTER
LOOP
N 8 did_swap true
to_do 5
index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 73
THE FOURTH “BUBBLE UP”
N 8 did_swap false
to_do 4
index 1

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 74
THE FOURTH “BUBBLE UP”
N 8 did_swap false
to_do 4
index 1

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 75
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 1

Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 76
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 77
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 78
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 79
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 80
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 4

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 81
THE FOURTH “BUBBLE UP”
N 8 did_swap true
to_do 4
index 4

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 82
AFTER FOURTH PASS OF OUTER
LOOP
N 8 did_swap true
to_do 4
index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 83
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 1

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 84
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 1

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 85
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 86
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 87
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 88
THE FIFTH “BUBBLE UP”
N 8 did_swap false
to_do 3
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 89
AFTER FIFTH PASS OF OUTER
LOOP
N 8 did_swap false
to_do 3
index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 90
FINISHED “EARLY”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8

Slide 91
SUMMARY
 “Bubble Up” algorithm will move largest
value to its correct location (to the right)
 Repeat “Bubble Up” until all elements are
correctly placed:
Maximum of N-1 times
Can finish early if no swapping occurs
 We reduce the number of elements we
compare each time one is correctly placed

Slide 92
QUESTIONS?

Slide 93
SELECTION SORT

94
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).

95
SORTING: CONT’D
Many banks provide statements that
list checks in increasing order (by
check number).
 Ina 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?
 Imaginefinding the phone number of your friend
in your mobile phone, but the phone book is not
sorted.

96
SELECTION SORT: IDEA
1. We have two group of items:
 sorted group, and
 unsorted group

2. 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.

97
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.

98
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 99
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 100
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 101
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 102
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 103
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 104
SELECTION SORT

5 1 3 4 6 2

Comparison

Data Movement

Sorted 105
SELECTION SORT

5 1 3 4 6 2

Largest

Comparison

Data Movement

Sorted 106
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 107
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 108
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 109
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 110
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 111
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 112
SELECTION SORT

5 1 3 4 2 6

Comparison

Data Movement

Sorted 113
SELECTION SORT

5 1 3 4 2 6

Larges
t
Comparison

Data Movement

Sorted 114
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 115
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 116
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 117
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 118
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 119
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 120
SELECTION SORT

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted 121
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 122
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 123
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 124
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 125
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 126
SELECTION SORT

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted 127
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 128
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 129
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 130
SELECTION SORT

2 1 3 4 5 6

Comparison

Data Movement

Sorted 131
SELECTION SORT

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted 132
SELECTION SORT

1 2 3 4 5 6

Comparison

Data Movement

Sorted 133
SELECTION SORT

1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted 134
SELECTION SORT: EXAMPLE
40 2 1 43 3 65 0 -1 58 3 42 4

40 2 1 43 3 4 0 -1 58 3 42 65

40 2 1 43 3 4 0 -1 42 3 58 65

40 2 1 3 3 4 0 -1 42 43 58 65

135
SELECTION SORT: EXAMPLE

40 2 1 3 3 4 0 -1 42 43 58 65

-1 2 1 3 3 4 0 40 42 43 58 65

-1 2 1 3 3 0 4 40 42 43 58 65

-1 2 1 0 3 3 4 40 42 43 58 65

136
SELECTION SORT: EXAMPLE

-1 2 1 0 3 3 4 40 42 43 58 65

-1 0 1 2 3 3 4 40 42 43 58 65

-1 0 1 2 3 3 4 40 42 43 58 65

-1 0 1 2 3 3 4 40 42 43 58 65

-1 0 1 2 3 3 4 40 42 43 58 65

137
SELECTION SORT: ANALYSIS
 Running time:
 Worst case: O(N2)
 Best case: O(N2)

138
INSERTION SORT

Slide
139
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 |

140
INSERTION SORT: IDEA
1. We have two group of items:
 sorted group, and
 unsorted group

2. 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.
3. Pick any item from, then insert the item at
the right position in the sorted group to
maintain sorted property.
141
INSERTION SORT: EXAMPLE

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

142
INSERTION SORT: EXAMPLE

1 2 40 43 3 65 0 -1 58 3 42 4

1 2 3 40 43 65 0 -1 58 3 42 4

1 2 3 40 43 65 0 -1 58 3 42 4

143
INSERTION SORT: EXAMPLE

1 2 3 40 43 65 0 -1 58 3 42 4

0 1 2 3 40 43 65 -1 58 3 42 4

-1
0 1
0 2
1 3
2 40
3 40
43 43
65 65 58 3 42 4

144
INSERTION SORT: EXAMPLE
-1
0 1
0 2
1 3
2 40
3 40
43 43
65 58 65 3 42 4

-1
0 1
0 2
1 3
2 40
3 43
3 40
65 43
43 58 58
65 65 42 4

-1
0 1
0 2
1 3
2 40
3 43
3 40
65 42 43
43 65 58 65 4

-1
0 1
0 2
1 3
2 40
3 43
3 43
65
4 40
42 42
65 43
43 58 58
65 65

145
INSERTION SORT: ANALYSIS
 Running time analysis:
 Worst case: O(N2)
 Best case: O(N)

146
A 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!

147
QUICK SORT

Slide
148
NOTES 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.

149
QUICKSORT 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:
 There is some element, a[i], where
a[i] is in its final position.
 For all l < i, a[l] < a[i].
 For all i < r, a[i] < a[r].

150
151
152
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

partition element unpartitioned left


partitioned right

153
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
swap

me
Q U I C K S O R T I S C O O L

partition element unpartitioned left


partitioned right
154
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
swap

me
Q U I C K S O R T I S C O O L

partition element unpartitioned left


partitioned right
155
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
swap

me
Q U I C K S O R T I S C O O L

partition element unpartitioned left


partitioned right
156
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
swap swap

me me
Q U I C K S O R T I S C O O L

partition element unpartitioned left


partitioned right
157
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

C U I C K S O R T I S Q O O L

partition element unpartitioned left


partitioned right
158
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
swap

me
C U I C K S O R T I S Q O O L

partition element unpartitioned left


partitioned right
159
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
swap

me
C U I C K S O R T I S Q O O L

partition element unpartitioned left


partitioned right
160
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
swap swap

me me
C U I C K S O R T I S Q O O L

partition element unpartitioned left


partitioned right
161
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

C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
162
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

C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
163
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

C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
164
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

C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
165
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
C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
166
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
C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
167
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
C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
168
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
C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
169
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 with

pointers partitionin
cross g element
C I I C K S O R T U S Q O O L

partition element unpartitioned left


partitioned right
170
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
partition
is
C I I C K L O R T U complete
S Q O O S

partition element unpartitioned left


partitioned right
171
QUICKSORT DEMO
 Quicksort illustrates the operation of the
basic algorithm. When the array is
partitioned, one element is in place on the
diagonal, the left subarray has its upper
corner at that element, and the right
subarray has its lower corner at that
element. The original file is divided into two
smaller parts that are sorted independently.
The left subarray is always sorted first, so
the sorted result emerges as a line of black
dots moving right and up the diagonal.

172
MERGESORT

Slide
173
SORTING
 Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101

Slide
174
DIVIDE AND CONQUER
 Divide and Conquer cuts the problem in
half each time, but uses the result of
both halves:
cut the problem in half until the
problem is trivial
solve for both halves
combine the solutions

Slide
175
MERGESORT
 A divide-and-conquer algorithm:
 Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
 Merge the sub-problem solutions together:
 Compare the sub-array’s first elements
 Remove the smallest element and put it into the
result array
 Continue the process until all elements have been
put into the result array

37 23 6 89 15 12 2 19

Slide
176
ALGORITHM
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)

Slide
177
 We don’t really pass in two arrays!

 We pass in one array with indicator variables


which tell us where one set of data starts and
finishes and where the other set of data starts
and finishes.

s1 f1 s2 f2
Slide
178
98 23 45 14 6 67 33 42

Slide
179
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

Slide
180
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

Slide
181
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Slide
182
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge

Slide
183
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23
Merge

Slide
184
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98
Merge

Slide
185
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Slide
186
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge

Slide
187
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge

Slide
188
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

Slide
189
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

Slide
190
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge

Slide
191
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge

Slide
192
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge

Slide
193
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge

Slide
194
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98

Slide
195
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98

Slide
196
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge

Slide
197
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge

Slide
198
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge

Slide
199
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98

Slide
200
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge

Slide
201
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge

Slide
202
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge

Slide
203
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge

Slide
204
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge

Slide
205
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge

Slide
206
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge

Slide
207
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

Slide
208
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
Slide
209
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
Slide
210
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
Slide
211
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
Slide
212
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
Slide
213
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
Slide
214
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
Slide
215
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
Slide
216
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
Slide
217
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Slide
218
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

Slide
219
SUMMARY
 Divide the unsorted collection into two

 Until the sub-arrays only contain one


element

 Then merge the sub-problem solutions


together

Slide
220
QUESTIONS?

Slide
221

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy