The Bubble Sort algorithm sorts an array by repeatedly comparing and swapping adjacent elements, moving the largest unsorted element to its correct position in each pass. The process continues for a total of n-1 passes, resulting in a sorted array. While Bubble Sort is easy to implement and stable, it is inefficient for large datasets due to its time complexity.
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 ratings0% found this document useful (0 votes)
15 views6 pages
Bubble Sort
The Bubble Sort algorithm sorts an array by repeatedly comparing and swapping adjacent elements, moving the largest unsorted element to its correct position in each pass. The process continues for a total of n-1 passes, resulting in a sorted array. While Bubble Sort is easy to implement and stable, it is inefficient for large datasets due to its time complexity.
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/ 6
Bubble Sort Algorithm
In Bubble Sort 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.
Total no. of passes: n-1
Total no. of comparisons: n*(n-1)/2 Initial array: [64, 34, 25, 12, 22, 11, 90]
•Compare 34 and 25,swap: [25, 34, 12, 22, 11, 64, 90] •Compare 34 and 12, swap: [25, 12, 34, 22, 11, 64, 90] •Compare 34 and 22, swap: [25, 12, 22, 34, 11, 64, 90] •Compare 34 and 11, swap: [25, 12, 22, 11, 34, 64, 90] •Compare 34 and 64, no swap: [25, 12, 22, 11, 34, 64, 90] Third Pass (i=2): Compare 25 and 12, swap: [12, 25, 22, 11, 34, 64, 90] Compare 25 and 22, swap: [12, 22, 25, 11, 34, 64, 90] Compare 25 and 11, swap: [12, 22, 11, 25, 34, 64, 90] Compare 25 and 34, no swap: [12, 22, 11, 25, 34, 64, 90]
Fourth Pass (i=3):
Compare 12 and 22, no swap: [12, 22, 11, 25, 34, 64, 90] Compare 22 and 11, swap: [12, 11, 22, 25, 34, 64, 90] Compare 22 and 25, no swap: [12, 11, 22, 25, 34, 64, 90] Fifth Pass (i=4): Compare 12 and 11, swap: [11, 12, 22, 25, 34, 64, 90] Compare 12 and 22, no swap: [11, 12, 22, 25, 34, 64, 90]
Sixth Pass (i=5):
Compare 11 and 12, no swap: [11, 12, 22, 25, 34, 64, 90] After these passes, the array is sorted as [11, 12, 22, 25, 34, 64, 90]. 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 which makes it very slow for large data sets. Bubble sort is a comparison-based sorting algorithm. It can limit the efficiency of the algorithm in certain cases.
Application : It is used in a polygon filling algorithm, (Computer Graphics)