34
Sorting Sorting takes an unordered array and makes it an ordered one. 5 12 35 42 77 101 5 12 35 42 77 101 2 3 4 2 3 4

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

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

Page 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

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

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

Bubble Sort

• End of First Pass• Red arrow cannot proceed any further

1013542775 12

0 1 2 3 4 5

Smallest number

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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

Page 28: 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

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

Page 29: 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

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

Page 30: 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

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

Page 31: 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

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

Page 32: 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

Bubble Sort

1014235125 77

0 1 2 3 4 5

End of Sorting

Page 33: 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

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

Page 34: 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

• See the sorting unit under lectures