Download presentation
Presentation is loading. Please wait.
Published byBenjamin Holland Modified over 9 years ago
1
Sorting Sorting takes an unordered array and makes it an ordered one. 5 12 3542 77 101 5 12 35 42 77101 0 1 2 3 4 5
2
Why do we need to sort data ?: -- to arrange names in alphabetical order -- arrange students by grade, etc. -- preliminary step to searching data. Basic steps involved: -- compare two items -- swap the two items or copy one item.
3
Bubble Sort: First Pass Traverse an array of elements – Move from the one end to the other end (red arrow) – At the beginning, the whole array is not sorted, so we say the beginning of the unsorted array is at the start element 0 (yellow arrow) – “Bubble” the smallest value to the beginning using pair- wise comparisons and swapping 5 12 3542 77 101 0 1 2 3 4 5 Swap
4
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 12 3542 77 5 0 1 2 3 4 5
5
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 12 3542 77 5 0 1 2 3 4 5 Swap
6
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 5 3542 77 12 0 1 2 3 4 5
7
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 5 3542 77 12 0 1 2 3 4 5 Swap
8
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 542 77 12 0 1 2 3 4 5
9
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 542 77 12 0 1 2 3 4 5 Swap
10
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 425 77 12 0 1 2 3 4 5
11
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 425 77 12 0 1 2 3 4 5 Swap
12
Bubble Sort End of First Pass Red arrow cannot proceed any further 101 35 4277 5 12 0 1 2 3 4 5 Smallest number
13
Bubble Sort: second pass We start from the end all the way to top (from 5 to 1) 101 35 4277 5 12 0 1 2 3 4 5
14
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 4277 5 12 0 1 2 3 4 5 No Swap
15
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 4277 5 12 0 1 2 3 4 5 Swap
16
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 12 4277 5 35 0 1 2 3 4 5
17
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 12 4277 5 35 0 1 2 3 4 5 Swap
18
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 42 1277 5 35 0 1 2 3 4 5
19
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 42 1277 5 35 0 1 2 3 4 5 Swap
20
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 42 7712 5 35 0 1 2 3 4 5 End of second Pass Second Smallest number
21
Bubble Sort: Third pass We start from the end all the way to start+2 (from 5 to 2) 101 42 7712 5 35 0 1 2 3 4 5
22
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 42 7712 5 35 0 1 2 3 4 5 No Swap
23
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 42 7712 5 35 0 1 2 3 4 5 Swap
24
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 7712 5 42 0 1 2 3 4 5
25
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 35 7712 5 42 0 1 2 3 4 5 Swap
26
Bubble Sort Traverse an array of elements – Move from the one end to the other end – “Bubble” the smallest value to the beginning using pair-wise comparisons and swapping 101 77 3512 5 42 0 1 2 3 4 5 End of Third Pass Third Smallest number
27
Bubble Sort: Fourth Pass We start from the end all the way to start+3 (from 5 to 3) 101 77 3512 5 42 0 1 2 3 4 5 No Swap
28
Bubble Sort: Fourth Pass We start from the end all the way to start+3 (from 5 to 3) 101 77 3512 5 42 0 1 2 3 4 5 Swap
29
Bubble Sort: Fourth Pass We start from the end all the way to start+3 (from 5 to 3) 101 42 3512 5 77 0 1 2 3 4 5 End of Fourth Pass Fourth Smallest number
30
Bubble Sort: Fifth Pass We start from the end all the way to start+4 (from 5 to 4) 101 42 3512 5 77 0 1 2 3 4 5 No Swap
31
Bubble Sort: Fifth Pass We start from the end all the way to start+4 (from 5 to 4) 101 42 3512 5 77 0 1 2 3 4 5 End of Fifth Pass fifth Smallest number
32
Bubble Sort 101 42 3512 5 77 0 1 2 3 4 5 End of Sorting
33
bubbleSort function This is the outer loop where it has two parts of the array: sorted and not sorted. This is similar to the passes we did before (the slow moving yellow arrow) bubblePass Sorts the remaining part of the array that is not sorted. This is the inner loop where it compares two elements and swap them if needed (the fast moving red arrow) Swap This function swaps two elements
34
See the sorting unit under lectures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.