0% found this document useful (0 votes)
48 views22 pages

Unit2 Bubblesort

Uploaded by

4ghj9zrgy2
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)
48 views22 pages

Unit2 Bubblesort

Uploaded by

4ghj9zrgy2
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/ 22

Unit2

Bubble Sort
Bubble sort Algorithm
• Bubble sort works on the repeatedly swapping of adjacent elements
until they reach the intended order.
• It is called bubble sort because the movement of array elements is
just like the movement of air bubbles in the water.
• Bubbles in water rise up to the surface;
• similarly, the array elements in bubble sort move to the end in each
iteration.
• It is not suitable for large data sets.
• The average and worst-case complexity of Bubble sort is O(n2)
• where n is a number of items.
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.

• begin BubbleSort(arr)
• for all array elements
• if arr[i] > arr[i+1]
• swap(arr[i], arr[i+1])
• end if
• end for
• return arr
• end BubbleSort
• Start at index zero, compare the element with the next one (a[0] &
a[1] (a is the name of the array)), and swap if a[0] > a[1].
• Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process
until the end of the array.
• After doing this, the largest element is present at the end.
• This whole thing is known as a pass.
• In the first pass, we process array elements from [0,n-1].
• Repeat step one but process array elements [0, n-2] because the last
one, i.e., a[n-1], is present at its correct position.
• After this step, the largest two elements are present at the end.
• Repeat this process n-1 times.
Working of Bubble sort Algorithm
• Now, compare 32 and 35.
• Second Pass
• The same process will be followed for second iteration.
• Third Pass
• The same process will be followed for third iteration.
Another example
• 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).
• 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).
• 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).
• The space complexity of bubble sort is O(1).
• It is because, in bubble sort, an extra variable is required for
swapping.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy