Bubble Sort Code in C Language
Write a program in C to sort n integers using bubble sort. Program should ask for number of Elements for sorting and then user need to enter numbers. At the end program should process these numbers and print the result.
Bubble Sort code should have options for both ..
- Ascending Order sorting
- Descending Order Sorting
Bubble sort is the most basic and simple algorithm for sorting.It uses loop for sorting numbers,As the number to be sorted increases time to sort also increases.
Algorithm Bubble Sort
Bubble Sort Code
#include <stdio.h> #include<conio.h> void main() { int array[100], n, i, j, swap; clrscr(); printf(“Enter number of elements\n”); scanf(“%d”, &n); printf(“Enter %d integers\n”, n); for (i = 0; i < n; i++) scanf(“%d”, &array[i]); for (i = 0 ; i < n – 1 ; i++) { for (j = 0 ; j < n – i – 1; j++) { if (array[j] > array[j+1]) /* For decreasing order use < */ { swap = array[j]; array[j] = array[j+1]; array[j+1] = swap; } } } printf(“Sorted list in ascending order:\n”); for ( i = 0 ; i < n ; i++ ) printf(“%d\n”, array[i]); getch(); }
Vishal says
thanks. it was helpful.