Upload
benjamin-holland
View
220
Download
1
Embed Size (px)
DESCRIPTION
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 Swap
Citation preview
Sorting
• Sorting takes an unordered array and makes it an ordered one.
512354277 101
5 12 35 42 77 101
0 1 2 3 4 5
0 1 2 3 4 5
• 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.
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
512354277 101
0 1 2 3 4 5
Swap
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
10112354277 5
0 1 2 3 4 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
10112354277 5
0 1 2 3 4 5
Swap
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
1015354277 12
0 1 2 3 4 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
1015354277 12
0 1 2 3 4 5
Swap
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
1013554277 12
0 1 2 3 4 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
1013554277 12
0 1 2 3 4 5
Swap
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
1013542577 12
0 1 2 3 4 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
1013542577 12
0 1 2 3 4 5
Swap
Bubble Sort
• End of First Pass• Red arrow cannot proceed any further
1013542775 12
0 1 2 3 4 5
Smallest number
Bubble Sort: second pass
• We start from the end all the way to top(from 5 to 1)
1013542775 12
0 1 2 3 4 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
1013542775 12
0 1 2 3 4 5
No Swap
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
1013542775 12
0 1 2 3 4 5
Swap
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
1011242775 35
0 1 2 3 4 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
1011242775 35
0 1 2 3 4 5
Swap
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
1014212775 35
0 1 2 3 4 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
1014212775 35
0 1 2 3 4 5
Swap
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
1014277125 35
0 1 2 3 4 5
End of second PassSecond Smallest number
Bubble Sort: Third pass
• We start from the end all the way to start+2(from 5 to 2)
1014277125 35
0 1 2 3 4 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
1014277125 35
0 1 2 3 4 5
No Swap
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
1014277125 35
0 1 2 3 4 5
Swap
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
1013577125 42
0 1 2 3 4 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
1013577125 42
0 1 2 3 4 5
Swap
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
1017735125 42
0 1 2 3 4 5
End of Third PassThirdSmallest number
Bubble Sort: Fourth Pass
• We start from the end all the way to start+3(from 5 to 3)
1017735125 42
0 1 2 3 4 5
No Swap
Bubble Sort: Fourth Pass
• We start from the end all the way to start+3(from 5 to 3)
1017735125 42
0 1 2 3 4 5
Swap
Bubble Sort: Fourth Pass
• We start from the end all the way to start+3(from 5 to 3)
1014235125 77
0 1 2 3 4 5
End of Fourth PassFourthSmallest number
Bubble Sort: Fifth Pass
• We start from the end all the way to start+4(from 5 to 4)
1014235125 77
0 1 2 3 4 5
No Swap
Bubble Sort: Fifth Pass
• We start from the end all the way to start+4(from 5 to 4)
1014235125 77
0 1 2 3 4 5
End of Fifth Pass fifthSmallest number
Bubble Sort
1014235125 77
0 1 2 3 4 5
End of Sorting
bubbleSort functionThis 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)
bubblePassSorts 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)
SwapThis function swaps two elements
• See the sorting unit under lectures