[go: up one dir, main page]

0% found this document useful (0 votes)
64 views9 pages

What Is Bubble Sort ?

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. It takes an array as input and iterates through comparing pairs of adjacent elements, swapping them if they are in the wrong order. This process is repeated until the array is fully sorted. The document provides an example algorithm in C to implement bubble sort and outputs a sorted array.

Uploaded by

Aircc Airccse
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)
64 views9 pages

What Is Bubble Sort ?

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. It takes an array as input and iterates through comparing pairs of adjacent elements, swapping them if they are in the wrong order. This process is repeated until the array is fully sorted. The document provides an example algorithm in C to implement bubble sort and outputs a sorted array.

Uploaded by

Aircc Airccse
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/ 9

Disclaimer Privacy Policy Contact Us    

Bubble Sort
 AryanOf .

What is Bubble Sort ?


 Bubble sort is a simple sorting algorithm. Bubble sorting is comparison based algorithm.
 In which each pair of adjacent is compared and the elements are swapped if they are not in order.
 Bubble sort starts with very first two elements, comparing them to check them to check which one is greater. 

#How Bubble Sort Works?


We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and
precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, the value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with
27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that here we have reached the end of the array. After one iteration, the array
should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.

Now we should look into some practical aspects of bubble sort.

Example: Program for Bubble Sort

#include <stdio.h>
void bubble_sort(long [], long);

int main()
{
long array[50], n, c, d, swap,zero = 0;
printf("Enter Elements\n");
scanf("%ld", &n);
printf("Enter %ld integers\n", n);
for (c = ; c <1 n-1; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for ( c = 1 ; c < n-1 ; c++ )
printf("%ld\n", array[c]);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}

Output:
 What is Selection Sort ?

 What is Quick Sort ?

 What is Insertion Sort?

 What is Merge Sort?

"Click on link" to check above question with Explanation with Algorithm and Example.

     

YOU MAY LIKE THESE POSTS


Linear Array Insertion Sort Merge Sort
 May 16, 2020  May 14, 2020  May 13, 2020

 NEWER OLDER 

AD 1

AD 2
Play Warframe for free Warframe: You must join Prepare For Battle! Play Join epic space combat!
now! the war! Now!

POPULAR POSTS

Sorting and its Types


 13:43

Merge Sort
 21:29

Linear Array
 15:45

Bubble Sort
 14:28

Selection Sort
 16:12

AD 3

AD 4
hi

DON'T MISS OUT


EMAIL ADDRESS... SUBMIT
Make sure you don't miss interesting happenings by
joining our newsletter program

Created By SoraTemplates | Distributed By Blogger Templates

You might also like