0% found this document useful (0 votes)
79 views

C Program For Bubble Sort

The document provides code for implementing bubble sort in C programming language to sort numbers or other data types in ascending order. It includes a function to perform bubble sort that compares adjacent elements and swaps them if they are in the wrong order until the list is fully sorted. While bubble sort is not very efficient due to its high time complexity, it serves as a simple example of a sorting algorithm that can be modified to sort other data types like strings as well.

Uploaded by

rasikasurve
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

C Program For Bubble Sort

The document provides code for implementing bubble sort in C programming language to sort numbers or other data types in ascending order. It includes a function to perform bubble sort that compares adjacent elements and swaps them if they are in the wrong order until the list is fully sorted. While bubble sort is not very efficient due to its high time complexity, it serves as a simple example of a sorting algorithm that can be modified to sort other data types like strings as well.

Uploaded by

rasikasurve
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C program for bubble sort: c programming code for bubble sort to sort numbers or arrange them

in ascending order. You can easily modify it to print numbers in descending order.

Bubble sort algorithm in c


/* Bubble sort code */
 
#include <stdio.h>
 
int main()
{
int array[100], n, c, d, swap;
 
printf("Enter number of elements\n");
scanf("%d", &n);
 
printf("Enter %d integers\n", n);
 
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
 
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
 
printf("Sorted list in ascending order:\n");
 
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
 
return 0;
}
Output of program:
Bubble sort in c language using function
#include <stdio.h>
 
void bubble_sort(long [], long);
 
int main()
{
long array[100], n, c, d, swap;
 
printf("Enter number of elements\n");
scanf("%ld", &n);
 
printf("Enter %ld integers\n", n);
 
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
 
bubble_sort(array, n);
 
printf("Sorted list in ascending order:\n");
 
for ( c = 0 ; c < n ; 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;
}
}
}
}

You can also sort strings using Bubble sort, it is less efficient as its average and
worst case complexity is high, there are many other fast sorting algorithms.

You might also like