The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Bubble sort is a very simple sorting algorithm of all sorting method. In bubble sort algorithm, sorting is done by swapping two number. The name implies, that each time a greater element is bubbled to the top of the array list.
Here is the program to demonstrate Bubble Sort.
#include <stdio.h>
int n, x[30], i, j;
int main()
{
void sort();
void display();
int i;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
printf("\nEnter %d elements in the array : \n",n);
for(i=0; i < n; i++)
scanf("%d",&x[i]);
sort();
display();
return 0;
}
void display()
{
printf("\nSorted Array in ascending order is : \n");
for(i=0; i < n; ++i)
printf("%5d",x[i]);
}
void sort()
{
int swap = 1,temp;
for(i = 0; i < n && swap == 1; ++i)
{
swap = 0;
for(j = 0; j < n-(i+1); ++j)
if(x[j] > x[j+1])
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
swap = 1;
}
}
}
Enter the size of the array : 5 Enter 5 elements in the array : 4 3 1 2 5 Sorted Array in ascending order is : 1 2 3 4 5
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019