0% found this document useful (0 votes)
17 views

Bubble Sort

Bubble sort is an algorithm that sorts a list of elements by repeatedly swapping adjacent elements that are out of order. It works by comparing adjacent elements and swapping them if they are in the wrong order, "bubbling up" the largest elements to the end of the list over multiple iterations. To improve efficiency, bubble sort checks if any swaps occurred during an iteration, and can stop early if the list is already sorted. It repeats the bubbling process N-1 times on average to fully sort a list of N elements.

Uploaded by

Lucky
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Bubble Sort

Bubble sort is an algorithm that sorts a list of elements by repeatedly swapping adjacent elements that are out of order. It works by comparing adjacent elements and swapping them if they are in the wrong order, "bubbling up" the largest elements to the end of the list over multiple iterations. To improve efficiency, bubble sort checks if any swaps occurred during an iteration, and can stop early if the list is already sorted. It repeats the bubbling process N-1 times on average to fully sort a list of N elements.

Uploaded by

Lucky
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 97

Bubble Sort

Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 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

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
42Swap77 12 101
77 42 35 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 35Swap35
77 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 12Swap12
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 Swap101
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

Largest value correctly placed


Items of Interest
• Notice that only the largest value is
correctly placed
• All other values are still out of order
• So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


void bubble_sort(int a[], int n)
{
int i = 0, j = 0, tmp;
for (i = 0; i < n-1; i++)
{
// loop n times - 1 per element
for (j = 0; j < n - i - 1; j++)
{ // last i elements are sorted already
if (a[j] > a[j + 1])
{ // swp if order is broken
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
Repeat “Bubble Up” How Many Times?
• If we have N elements…

• And if each time we bubble an element,


we place it in its correct location…

• Then we repeat the “bubble up”


process N – 1 times.

• This guarantees we’ll correctly


place all N elements.
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1

12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Reducing the Number of Comparisons
• On the Nth “bubble up”, we only need to
do MAX-N comparisons.

• For example:
– This is the 4th “bubble up”
– MAX is 6
– Thus we have 2 comparisons to do

1 2 3 4 5 6
12 35 5 42 77 101
procedure Bubblesort(A isoftype in/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1

loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)

Outer loop
Inner loop
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Already Sorted Collections?
• What if the collection was already sorted?
• What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?

• We want to be able to detect this


and “stop early”!
1 2 3 4 5 6
5 12 35 42 77 101
Using a Boolean “Flag”

• We can use a boolean variable to determine if any


swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the


collection is already sorted!

• This boolean “flag” needs to be reset after each


“bubble up.”
did_swap isoftype Boolean
did_swap <- true

loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
An Animated Example

N 8 did_swap true
to_do 7
index

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

Swap

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 1

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop

N 8 did_swap true
to_do 7
index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

No Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 2

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

No Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop

N 8 did_swap true
to_do 6
index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 1

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

No Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop

N 8 did_swap true
to_do 5
index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 1

Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop

N 8 did_swap true
to_do 4
index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop

N 8 did_swap false
to_do 3
index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”

N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Time complexity

• Bubble sort has a time complexity of O(N2) for all the


cases, which makes it very slow for large data sets.
Summary
• “Bubble Up” algorithm will move largest
value to its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
– Can finish early if no swapping occurs
• We reduce the number of elements we
compare each time one is correctly placed

You might also like