What Is Bubble Sort ?
What Is Bubble Sort ?
Bubble Sort
AryanOf .
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.
Next we compare 33 and 35. We find that both are in already sorted positions.
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.
#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 ?
"Click on link" to check above question with Explanation with Algorithm and Example.
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
Merge Sort
21:29
Linear Array
15:45
Bubble Sort
14:28
Selection Sort
16:12
AD 3
AD 4
hi