Lecture 14. Bubble and selection sort - PowerPoint PPT Presentation

1 / 101
About This Presentation
Title:

Lecture 14. Bubble and selection sort

Description:

Lecture 14. Bubble and selection sort * – PowerPoint PPT presentation

Number of Views:278
Avg rating:3.0/5.0
Slides: 102
Provided by: Qin48
Category:

less

Transcript and Presenter's Notes

Title: Lecture 14. Bubble and selection sort


1
Lecture 14. Bubble and
selection sort
2
Recap
  • If sorting algorithm using main memory only , it
    is called internal sorting technique.
  • If sorting algorithm using main memory as well
    virtual memory , it is called external sorting
    technique.
  • If sorting technique using either main or virtual
    and its demand for extra space remain constant
    i.e. demand for space not change with increase or
    decrease in input size.
  • There are several sorting techniques such as
    insertion, bubble, selection and quick sort etc.

3
Sorting
  • Sorting takes an unordered collection and makes
    it an ordered one.

1 2 3 4 5
6
101
12
5
35
42
77
1 2 3 4 5
6
4
What is Bubble Sort
  • Idea
  • Repeatedly pass through the array
  • Swaps adjacent elements that are out of order
  • Easier to implement, but slower than Insertion
    sort

i
1
2
3
n
j
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
12
101
5
35
42
77
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
Swap
12
101
5
35
42
77
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
Swap
12
101
5
35
77
42
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
Swap
12
101
5
77
35
42
9
"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
101
5
12
35
42
No need to swap
10
"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
Swap
77
101
5
12
35
42
11
"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
101
77
5
12
35
42
Largest value correctly placed
12
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
101
77
5
12
35
42
Largest value correctly placed
13
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 well correctly place all N
    elements.

14
Bubbling All the Elements
15
Reducing the Number of Comparisons
16
Reducing the Number of Comparisons
  • On the Nth bubble up, we only need to do MAX-N
    comparisons.
  • For example
  • This is the 4th bubble up
  • MAX is 6
  • Thus we have 2 comparisons to do

17
Already Sorted Array?
  • What if the array 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!

18
An Animated Example
N
8
true
did_swap
to_do
7
index

67
45
23
14
6
33
98
42
1 2 3 4 5 6 7 8
19
An Animated Example
N
8
false
did_swap
to_do
7
index
1
67
45
23
14
6
33
98
42
1 2 3 4 5 6 7 8
20
An Animated Example
N
8
false
did_swap
to_do
7
index
1
Swap
67
45
23
14
6
33
98
42
1 2 3 4 5 6 7 8
21
An Animated Example
N
8
true
did_swap
to_do
7
index
1
Swap
67
45
98
14
6
33
23
42
1 2 3 4 5 6 7 8
22
An Animated Example
N
8
true
did_swap
to_do
7
index
2
67
45
98
14
6
33
23
42
1 2 3 4 5 6 7 8
23
An Animated Example
N
8
true
did_swap
to_do
7
index
2
Swap
67
45
98
14
6
33
23
42
1 2 3 4 5 6 7 8
24
An Animated Example
N
8
true
did_swap
to_do
7
index
2
Swap
67
98
45
14
6
33
23
42
1 2 3 4 5 6 7 8
25
An Animated Example
N
8
true
did_swap
to_do
7
index
3
67
98
45
14
6
33
23
42
1 2 3 4 5 6 7 8
26
An Animated Example
N
8
true
did_swap
to_do
7
index
3
Swap
67
98
45
14
6
33
23
42
1 2 3 4 5 6 7 8
27
An Animated Example
N
8
true
did_swap
to_do
7
index
3
Swap
67
14
45
98
6
33
23
42
1 2 3 4 5 6 7 8
28
An Animated Example
N
8
true
did_swap
to_do
7
index
4
67
14
45
98
6
33
23
42
1 2 3 4 5 6 7 8
29
An Animated Example
N
8
true
did_swap
to_do
7
index
4
Swap
67
14
45
98
6
33
23
42
1 2 3 4 5 6 7 8
30
An Animated Example
N
8
true
did_swap
to_do
7
index
4
Swap
67
14
45
6
98
33
23
42
1 2 3 4 5 6 7 8
31
An Animated Example
N
8
true
did_swap
to_do
7
index
5
67
14
45
6
98
33
23
42
1 2 3 4 5 6 7 8
32
An Animated Example
N
8
true
did_swap
to_do
7
index
5
Swap
67
14
45
6
98
33
23
42
1 2 3 4 5 6 7 8
33
An Animated Example
N
8
true
did_swap
to_do
7
index
5
Swap
98
14
45
6
67
33
23
42
1 2 3 4 5 6 7 8
34
An Animated Example
N
8
true
did_swap
to_do
7
index
6
98
14
45
6
67
33
23
42
1 2 3 4 5 6 7 8
35
An Animated Example
N
8
true
did_swap
to_do
7
index
6
Swap
98
14
45
6
67
33
23
42
1 2 3 4 5 6 7 8
36
An Animated Example
N
8
true
did_swap
to_do
7
index
6
Swap
33
14
45
6
67
98
23
42
1 2 3 4 5 6 7 8
37
An Animated Example
N
8
true
did_swap
to_do
7
index
7
33
14
45
6
67
98
23
42
1 2 3 4 5 6 7 8
38
An Animated Example
N
8
true
did_swap
to_do
7
index
7
Swap
33
14
45
6
67
98
23
42
1 2 3 4 5 6 7 8
39
An Animated Example
N
8
true
did_swap
to_do
7
index
7
Swap
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
40
After First Pass of Outer Loop
N
8
true
did_swap
to_do
7
Finished first Bubble Up
index
8
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
41
The Second Bubble Up
N
8
false
did_swap
to_do
6
index
1
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
42
The Second Bubble Up
N
8
false
did_swap
to_do
6
index
1
No Swap
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
43
The Second Bubble Up
N
8
false
did_swap
to_do
6
index
2
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
44
The Second Bubble Up
N
8
false
did_swap
to_do
6
index
2
Swap
33
14
45
6
67
42
23
98
1 2 3 4 5 6 7 8
45
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
2
Swap
33
45
14
6
67
42
23
98
1 2 3 4 5 6 7 8
46
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
3
33
45
14
6
67
42
23
98
1 2 3 4 5 6 7 8
47
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
3
Swap
33
45
14
6
67
42
23
98
1 2 3 4 5 6 7 8
48
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
3
Swap
33
6
14
45
67
42
23
98
1 2 3 4 5 6 7 8
49
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
4
33
6
14
45
67
42
23
98
1 2 3 4 5 6 7 8
50
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
4
No Swap
33
6
14
45
67
42
23
98
1 2 3 4 5 6 7 8
51
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
5
33
6
14
45
67
42
23
98
1 2 3 4 5 6 7 8
52
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
5
Swap
33
6
14
45
67
42
23
98
1 2 3 4 5 6 7 8
53
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
5
Swap
67
6
14
45
33
42
23
98
1 2 3 4 5 6 7 8
54
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
6
67
6
14
45
33
42
23
98
1 2 3 4 5 6 7 8
55
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
6
Swap
67
6
14
45
33
42
23
98
1 2 3 4 5 6 7 8
56
The Second Bubble Up
N
8
true
did_swap
to_do
6
index
6
Swap
42
6
14
45
33
67
23
98
1 2 3 4 5 6 7 8
57
After Second Pass of Outer Loop
N
8
true
did_swap
to_do
6
Finished second Bubble Up
index
7
42
6
14
45
33
67
23
98
1 2 3 4 5 6 7 8
58
The Third Bubble Up
N
8
false
did_swap
to_do
5
index
1
42
6
14
45
33
67
23
98
1 2 3 4 5 6 7 8
59
The Third Bubble Up
N
8
false
did_swap
to_do
5
index
1
Swap
42
6
14
45
33
67
23
98
1 2 3 4 5 6 7 8
60
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
1
Swap
42
6
23
45
33
67
14
98
1 2 3 4 5 6 7 8
61
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
2
42
6
23
45
33
67
14
98
1 2 3 4 5 6 7 8
62
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
2
Swap
42
6
23
45
33
67
14
98
1 2 3 4 5 6 7 8
63
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
2
Swap
42
23
6
45
33
67
14
98
1 2 3 4 5 6 7 8
64
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
3
42
23
6
45
33
67
14
98
1 2 3 4 5 6 7 8
65
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
3
No Swap
42
23
6
45
33
67
14
98
1 2 3 4 5 6 7 8
66
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
4
42
23
6
45
33
67
14
98
1 2 3 4 5 6 7 8
67
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
4
Swap
42
23
6
45
33
67
14
98
1 2 3 4 5 6 7 8
68
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
4
Swap
42
23
6
33
45
67
14
98
1 2 3 4 5 6 7 8
69
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
5
42
23
6
33
45
67
14
98
1 2 3 4 5 6 7 8
70
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
5
Swap
42
23
6
33
45
67
14
98
1 2 3 4 5 6 7 8
71
The Third Bubble Up
N
8
true
did_swap
to_do
5
index
5
Swap
45
23
6
33
42
67
14
98
1 2 3 4 5 6 7 8
72
The Third Bubble Up
N
8
true
did_swap
to_do
5
Finished third Bubble Up
index
6
45
23
6
33
42
67
14
98
1 2 3 4 5 6 7 8
73
The Fourth Bubble Up
N
8
false
did_swap
to_do
4
index
1
45
23
6
33
42
67
14
98
1 2 3 4 5 6 7 8
74
The Fourth Bubble Up
N
8
false
did_swap
to_do
4
index
1
Swap
45
23
6
33
42
67
14
98
1 2 3 4 5 6 7 8
75
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
1
Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
76
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
2
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
77
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
2
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
78
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
3
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
79
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
3
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
80
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
4
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
81
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
index
4
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
82
The Fourth Bubble Up
N
8
true
did_swap
to_do
4
Finished fourth Bubble Up
index
5
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
83
The Fourth Bubble Up
N
8
false
did_swap
to_do
3
index
1
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
84
The Fifth Bubble Up
N
8
false
did_swap
to_do
3
index
1
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
85
The Fifth Bubble Up
N
8
false
did_swap
to_do
3
index
2
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
86
The Fifth Bubble Up
N
8
false
did_swap
to_do
3
index
2
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
87
The Fifth Bubble Up
N
8
false
did_swap
to_do
3
index
3
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
88
The Fifth Bubble Up
N
8
false
did_swap
to_do
3
index
3
No Swap
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
89
After Fifth Pass of Outer Loop
N
8
false
did_swap
to_do
3
Finished fifth Bubble Up
index
4
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
90
Finished Early
N
8
false
did_swap
to_do
3
We didnt do any swapping,so all of the other
elementsmust be correctly placed. We can skip
the last twopasses of the outer loop.
index
4
45
23
14
33
42
67
6
98
1 2 3 4 5 6 7 8
91
Bubble Sort (Example-2)
  • Bubble sort time O(n2)

92
Algorithm of Bubble Sort
  • Alg. BUBBLESORT(A)
  • for i ? 1 to lengthA
  • do for j ? lengthA downto i 1
  • do if Aj lt Aj -1
  • then exchange Aj ? Aj-1

i
93
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.

94
Bubble-Sort Running Time
Alg. BUBBLESORT(A) for i ? 1 to lengthA do
for j ? lengthA downto i 1 do if
Aj lt Aj -1 then exchange Aj ?
Aj-1
c1
c2
c3
Comparisons ? n2/2
c4
Exchanges ? n2/2
T(n)
c1(n1)
c2
c3
c4
?(n)
(c2 c2 c4)
  • Thus,T(n) ?(n2)

95
Selection Sort
  • Idea
  • Find the smallest element in the array
  • Exchange it with the element in the first
    position
  • Find the second smallest element and exchange it
    with the element in the second position
  • Continue until the array is sorted
  • Disadvantage
  • Running time depends only slightly on the amount
    of order in the file

96
Example
97
Selection Sort
  • Alg. SELECTION-SORT(A)
  • n ? lengthA
  • for j ? 1 to n - 1
  • do smallest ? j
  • for i ? j 1 to n
  • do if Ai lt Asmallest
  • then smallest ? i
  • exchange Aj ? Asmallest

98
Analysis of Selection Sort
  • Alg. SELECTION-SORT(A)
  • n ? lengthA
  • for j ? 1 to n - 1
  • do smallest ? j
  • for i ? j 1 to n
  • do if Ai lt Asmallest
  • then smallest ? i
  • exchange Aj ? Asmallest

cost times c1 1 c2 n c3 n-1
c4 c5 c6 n-1 c7
99
Home work
  • Discuss the stability and in place factors for
    bubble, insertion and selection sort.

100
Summary
  • In bubble sort, N-1 Phases and N-Phase_No
    comparisions are performed to order data either
    in ascending or descending order.
  • Flag (Boolean variable can be used to reduce or
    trim the unwanted comparisons.
  • In Selection sort, for each phase you will take a
    min number and then perform comparison to check
    or find other smallest value.

101
In Next Lecturer
  • In next lecture, we will discuss about quick and
    merge sorting techniques.
Write a Comment
User Comments (0)
About PowerShow.com