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

3rd Lecture BSCS6 of Algorithm (Sorting, Bubble Sorting and Analysis of Bubble Sorting) - 31-08

Uploaded by

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

3rd Lecture BSCS6 of Algorithm (Sorting, Bubble Sorting and Analysis of Bubble Sorting) - 31-08

Uploaded by

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

Design & analysis of algorithm

BSCS 6th semester 3nd lecture


(date: 31/08/2023)
By Rahila Aslam

Sorting Algorithms
What is sorting?
Bubble sort and analysis of bubble sort

What is Sorting?

A Sorting Algorithm is used to rearrange a given array or list of elements


according to a comparison operator on the elements. The comparison
operator is used to decide the new order of elements in the respective data
structure.
For Example: The below list of characters is sorted in increasing order of
their ASCII values. That is, the character with a lesser ASCII value will be
placed first than the character with a higher ASCII value.

Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order. This algorithm
is not suitable for large data sets as its average and worst-case time
complexity is quite high.

Bubble Sort Algorithm


In this algorithm,
• traverse from left and compare adjacent elements and the higher
one is placed at right side.
• In this way, the largest element is moved to the rightmost end at
first.
• This process is then continued to find the second largest and place
it and so on until the data is sorted.
How does Bubble Sort Work?
Let us understand the working of bubble sort with the help of the following
illustration:
Input: arr[] = {6, 3, 0, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the
array.

Bubble Sort Algorithm : Placing the largest element at correct position

Second Pass:
Place the second largest element at correct position
Bubble Sort Algorithm : Placing the second largest element at correct
position

Third Pass:
Place the remaining two elements at their correct positions.

Bubble Sort Algorithm : Placing the remaining elements at their correct

positions

Algorithm
In the algorithm given below, suppose arr is an array of n elements. The
assumed swap function in the algorithm will swap the values of given array
elements.

1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort

Complexity Analysis of Bubble Sort:


Time
Complexity: O(N2)
Auxiliary Space: O(1)
Advantages of Bubble Sort:
• Bubble sort is easy to understand and implement.
• It does not require any additional memory space.
• It is a stable sorting algorithm, meaning that elements with the
same key value maintain their relative order in the sorted output.
Disadvantages of Bubble Sort:
• Bubble sort has a time complexity of O(N2) which makes it very
slow for large data sets.
• Bubble sort is a comparison-based sorting algorithm, which
means that it requires a comparison operator to determine the
relative order of elements in the input data set. It can limit the
efficiency of the algorithm in certain cases.

Bubble sort complexity

Now, let's see the time complexity of bubble sort in the best case, average case,
and worst case. We will also see the space complexity of bubble sort.
1. Time Complexity
Case Time Complexity

Best Case O(n)

Average Case O(n2)

Worst Case O(n2)

o Best Case Complexity - It occurs when there is no sorting required, i.e.


the array is already sorted. The best-case time complexity of bubble sort
is O(n).
o Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of bubble sort is O(n2).
o Worst Case Complexity - It occurs when the array elements are
required to be sorted in reverse order. That means suppose you have to
sort the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of bubble sort
is O(n2).

2. Space Complexity
Space Complexity O(1)

Stable YES

o The space complexity of bubble sort is O(1). It is because, in bubble


sort, an extra variable is required for swapping.
o The space complexity of optimized bubble sort is O(2). It is because
two extra variables are required in optimized bubble sort.

You might also like