Selection Sort and Bubble Sort_Arrays
Selection Sort and Bubble Sort_Arrays
• Selection
• Bubble
• Insertion
Selection Sort
1 2 3 4 5 6
77 42 35 12 101 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
42 Swap42
77 35 12 101 5
77
"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
42 7735Swap35
77 12 101 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
42 35 12 Swap12
77 77 101 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
42 35 12 77 101 5
No need to swap
"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
42 35 12 77 5 Swap 101
101 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
42 35 12 77 5 101
0 1 2 3 4 5
42 35 12 77 5 101
array[j]=array[j+1];
array[j+1]=temp;
}
}
}