Bubble Sort
Bubble Sort
Sorting
• a sorting algorithm is an algorithm that puts
elements of a list into an order.
• Sorting takes an unordered collection and makes it
an ordered one.
1 2 3 4 5 6
5 12 35 42 77 101
5
12
35
42
77 101
1 2 3 4 5 6
"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
5
12
35
42
77 101
1 2 3 4 5 6
"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
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
"Bubbling Up" the Largest Element
5
12
35
77
42 101
1 2 3 4 5 6
• 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
Swap
35 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
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 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
5
77
12
35
42 101
1 2 3 4 5 6
No 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
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
"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
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed

Bubble Sort.pptx

  • 2.
  • 3.
  • 4.
    Sorting • a sortingalgorithm is an algorithm that puts elements of a list into an order. • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 5 12 35 42 77 101 5 12 35 42 77 101 1 2 3 4 5 6
  • 5.
    "Bubbling Up" theLargest 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 5 12 35 42 77 101 1 2 3 4 5 6
  • 6.
    "Bubbling Up" theLargest 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 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77
  • 7.
    "Bubbling Up" theLargest Element 5 12 35 77 42 101 1 2 3 4 5 6 • 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 Swap 35 77
  • 8.
    "Bubbling Up" theLargest 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 5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77
  • 9.
    "Bubbling Up" theLargest 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 5 77 12 35 42 101 1 2 3 4 5 6 No swap
  • 10.
    "Bubbling Up" theLargest 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 5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101
  • 11.
    "Bubbling Up" theLargest 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 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed