Download presentation
Presentation is loading. Please wait.
Published byDorothy Fields Modified over 9 years ago
2
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Content derived from: Data Structures Using C++ (D.S. Malik) and Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)c
3
Previously Sorts Already Seen Insertion Sort Selection Sort Bubble Sort may have been briefly mentioned Related Linear (sequential) Search Binary Search
4
Bubble Sort Algorithm Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S)
5
Bubble Sort Algorithm Idea: Start at element[0] Compare it to element[1] If element[0] > element[1] swap them Repeat for element[1] and element[2] Keep repeating up to element[n-1] and element[n] After 1 st time through biggest element is at end Repeat to move the 2 nd biggest to be 2 nd from end … Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S)
6
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 7 2 6 9 3
7
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 7 2 6 9 3
8
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 7 2 6 9 3
9
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 7 6 9 3
10
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 7 6 9 3
11
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 9 3
12
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 9 3
13
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 9 3
14
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 3 9
15
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 3 9 The largest value has now BUBBLED to the end
16
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 5 2 6 7 3 9
17
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 7 3 9
18
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 7 3 9
19
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 7 3 9
20
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 7 3 9
21
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 3 7 9
22
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 3 7 9 The 2 nd largest value has BUBBLED to being the 2 nd from the end
23
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 3 7 9
24
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 3 7 9
25
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 6 3 7 9
26
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 3 6 7 9
27
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 3 6 7 9 The 3 rd largest value has BUBBLED to being the 3 rd from the end
28
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 3 6 7 9
29
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 5 3 6 7 9
30
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 3 5 6 7 9
31
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 3 5 6 7 9 The 4 th largest value has BUBBLED to being the 4 th from the end
32
Bubble Sort: In Action Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) 2 3 5 6 7 9 And the 5 th and 6 th follow in trivial fashion
33
End Example Run Questions on Example Run Through? Next: Complexity Analysis (Big-Oh)
34
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n
35
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2
36
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2
37
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2 + 1
38
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2 + 1 n + n 2 + n 2 + 1
39
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2 + 1 n + n 2 + n 2 + 1 = 2n 2 + n + 1
40
Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2 + 1 2n 2 + n + 1
41
T(n) = O(n 2 ) Complexity of Bubble Sort Algorithm bubbleSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C (take C as typical greater than op>) for ( i = 0; i < S.size(); i++ ) for ( j = 1; j < S.size() – i; j++ ) if ( S.atIndex ( j – 1 ) > S.atIndex ( j ) ) S.swap ( j-1, j ); return(S) n + n 2 + 1 2n 2 + n + 1
42
Suggested Activity: Implement Bubble Sort Using std::vector Implement BubbleSort on an vector of type double See previous slide’s algorithm for basic idea
43
So ends Bubble Sort Onto… more SORTing
44
The End of This Part End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.