0% found this document useful (0 votes)
21 views86 pages

L4 - Bubble Sort

Uploaded by

mf1776136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views86 pages

L4 - Bubble Sort

Uploaded by

mf1776136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

Review

Introduction to Recursion
Recursive Definition
Recursive Algorithms
Finding a Recursive Solution
Example Recursive Function
Recursive Programming
Rules for Recursive Function
Example Tower of Hanoi
Other examples

1
Bubble Sort
Bubble Sort
Bubble Sort Algorithm
Time Complexity
Best case
Average case
Worst case
Examples

2
Bubble Sort
In bubble sort, each element is compared
with its adjacent element
If the first element is larger than the
second one, then the positions of the
elements are interchanged otherwise it is
not changed
Then next element is compared with its
adjacent element and the same process is
repeated for all the elements in the array
until we get a sorted array

3
Let A be a linear array of n numbers
Suppose the list of numbers A[1], A[2],
………… A[n] is an element of array A
Sorting of A means rearranging the
elements of A so that they are in order
Here we are dealing with ascending order.
i.e., A[1] < A[2] < A[3]< ...... A[n]
The bubble sort algorithm works as follows:

4
Step 1: Compare A[1] and A[2] and arrange
them in the ascending order so that A[1] <
A[2]
If A[1] is greater than A[2] then interchange
the position of data by swap = A[1]; A[1] =
A[2]; A[2] = swap
Then compare A[2] and A[3] and arrange
them so that A[2] < A[3]
Continue the process until we compare A[N –
1] with A[N]
Step1 contains n – 1 comparisons i.e., the
largest element is “bubbled up” to the nth
5 position
Step 2: Repeat step 1 with one less
comparisons that is, now stop comparison
at A [n – 1] and possibly rearrange A[N – 2]
and A[N – 1] and so on
In the first pass, step 2 involves n–2
comparisons and the second largest
element will occupy A[n-1]
And in the second pass, step 2 involves n –
3 comparisons and the 3rd largest element
will occupy A[n – 2] and so on
After n – 1 steps, the array will be a sorted
array in increasing (or ascending) order
6
Algorithm
Let A be a linear array of n numbers. Swap is a
temporary variable for swapping (or interchange)
the position of the numbers
1. Input n numbers of an array A
2. Initialize i = 0 and repeat through step 4 if (i < n)
3. Initialize j = 0 and repeat through step 4 if (j < n
– i – 1)
4. If (A[j] > A[j + 1])
(a) Swap = A[j]
(b) A[j] = A[j + 1]
(c) A[j + 1] = Swap
5. Display the sorted numbers of array A
6. Exit.
7
Time Complexity
The time complexity for bubble sort is
calculated in terms of the number of
comparisons f (n) (or of number of loops)
Here two loops (outer loop and inner loop)
iterates (or repeated) the comparisons
The number of times the outer loop iterates
is determined by the number of elements in
the list which is asked to sort
The inner loop is iterated one less than the
number of elements in the list
f(n)=n (n-1)=O(n2)

8
Best Case
In this case the inner loop will iterate with
the ‘if’ condition evaluating time, that is the
swap procedure is never called
In best case outer loop will terminate after
one iteration, i.e., it involves performing
one pass, which requires n–1 comparisons
f (n) = O(n)

9
Worst Case
In this case the array will be an inverted list
(i.e., 5, 4, 3, 2, 1, 0)
Here to move first element to the end of
the array, n–1 times the swapping
procedure is to be called
Every other element in the list will also
move one location towards the start or end
of the loop on every iteration
Thus n times the outer loop will iterate and
n (n-1) times the inner loop will iterate to
sort an inverted array
1 f(n) = (n(n – 1))/2 = O(n 2)
0
Average Case
Average case is very difficult to analyze
than the other cases
In this case the input data are randomly
placed in the list
The exact time complexity can be
calculated only if we know the number of
iterations, comparisons and swapping
In general, the complexity of average case
is:
f(n) = (n(n–1))/2 = O(n 2)

1
1
An Example

N 8 swap true

to_do 7

index

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
2
An Example

N 8 swap false

to_do 7

index 1

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
3
An Example

N 8 swap false

to_do 7

index 1

Swap

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
4
An Example

N 8 swap true

to_do 7

index 1

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
5
An Example

N 8 swap true

to_do 7

index 2

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
6
An Example

N 8 swap true

to_do 7

index 2

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

1
7
An Example

N 8 swap true

to_do 7

index 2

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

1
8
An Example

N 8 swap true

to_do 7

index 3

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

1
9
An Example

N 8 swap true

to_do 7

index 3

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

2
0
An Example

N 8 swap true

to_do 7

index 3

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

2
1
An Example

N 8 swap true

to_do 7

index 4

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

2
2
An Example

N 8 swap true

to_do 7

index 4

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

2
3
An Example

N 8 swap true

to_do 7

index 4

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

2
4
An Example

N 8 swap true

to_do 7

index 5

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

2
5
An Example

N 8 swap true

to_do 7

index 5

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

2
6
An Example

N 8 swap true

to_do 7

index 5

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

2
7
An Example

N 8 swap true

to_do 7

index 6

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

2
8
An Example

N 8 swap true

to_do 7

index 6

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

2
9
An Example

N 8 swap true

to_do 7

index 6

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

3
0
An Example

N 8 swap true

to_do 7

index 7

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

3
1
An Example

N 8 swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

3
2
An Example

N 8 swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

3
3
After First Pass of Outer Loop

N 8 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

3
4
The Second “Bubble Up”

N 8 swap false

to_do 6

index 1

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

3
5
The Second “Bubble Up”

N 8 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

3
6
The Second “Bubble Up”

N 8 swap false

to_do 6

index 2

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

3
7
The Second “Bubble Up”

N 8 swap false

to_do 6

index 2

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

3
8
The Second “Bubble Up”

N 8 swap true

to_do 6

index 2

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

3
9
The Second “Bubble Up”

N 8 swap true

to_do 6

index 3

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

4
0
The Second “Bubble Up”

N 8 swap true

to_do 6

index 3

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

4
1
The Second “Bubble Up”

N 8 swap true

to_do 6

index 3

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

4
2
The Second “Bubble Up”

N 8 swap true

to_do 6

index 4

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

4
3
The Second “Bubble Up”

N 8 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

4
4
The Second “Bubble Up”

N 8 swap true

to_do 6

index 5

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

4
5
The Second “Bubble Up”

N 8 swap true

to_do 6

index 5

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

4
6
The Second “Bubble Up”

N 8 swap true

to_do 6

index 5

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

4
7
The Second “Bubble Up”

N 8 swap true

to_do 6

index 6

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

4
8
The Second “Bubble Up”

N 8 swap true

to_do 6

index 6

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

4
9
The Second “Bubble Up”

N 8 swap true

to_do 6

index 6

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
0
After Second Pass of Outer
Loop

N 8 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

5
1
The Third “Bubble Up”

N 8 swap false

to_do 5

index 1

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
2
The Third “Bubble Up”

N 8 swap false

to_do 5

index 1

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
3
The Third “Bubble Up”

N 8 swap true

to_do 5

index 1

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
4
The Third “Bubble Up”

N 8 swap true

to_do 5

index 2

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
5
The Third “Bubble Up”

N 8 swap true

to_do 5

index 2

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

5
6
The Third “Bubble Up”

N 8 swap true

to_do 5

index 2

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

5
7
The Third “Bubble Up”

N 8 swap true

to_do 5

index 3

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

5
8
The Third “Bubble Up”

N 8 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

5
9
The Third “Bubble Up”

N 8 swap true

to_do 5

index 4

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

6
0
The Third “Bubble Up”

N 8 swap true

to_do 5

index 4

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

6
1
The Third “Bubble Up”

N 8 swap true

to_do 5

index 4

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

6
2
The Third “Bubble Up”

N 8 swap true

to_do 5

index 5

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

6
3
The Third “Bubble Up”

N 8 swap true

to_do 5

index 5

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

6
4
The Third “Bubble Up”

N 8 swap true

to_do 5

index 5

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

6
5
After Third Pass of Outer Loop

N 8 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

6
6
The Fourth “Bubble Up”

N 8 swap false

to_do 4

index 1

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

6
7
The Fourth “Bubble Up”

N 8 swap false

to_do 4

index 1

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

6
8
The Fourth “Bubble Up”

N 8 swap true

to_do 4

index 1

Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

6
9
The Fourth “Bubble Up”

N 8 swap true

to_do 4

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

7
0
The Fourth “Bubble Up”

N 8 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

7
1
The Fourth “Bubble Up”

N 8 swap true

to_do 4

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

7
2
The Fourth “Bubble Up”

N 8 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

7
3
The Fourth “Bubble Up”

N 8 swap true

to_do 4

index 4

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

7
4
The Fourth “Bubble Up”

N 8 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

7
5
After Fourth Pass of Outer
Loop

N 8 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

7
6
The Fifth “Bubble Up”

N 8 swap false

to_do 3

index 1

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

7
7
The Fifth “Bubble Up”

N 8 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

7
8
The Fifth “Bubble Up”

N 8 swap false

to_do 3

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

7
9
The Fifth “Bubble Up”

N 8 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

8
0
The Fifth “Bubble Up”

N 8 swap false

to_do 3

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

8
1
The Fifth “Bubble Up”

N 8 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

8
2
After Fifth Pass of Outer Loop

N 8 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

8
3
Finished “Early”

N 8 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

8
4
More Examples

8
5
Summary
Bubble Sort
Bubble Sort Algorithm
Time Complexity
Best case
Average case
Worst case
Examples

8
6

You might also like