This C code implements bubble sort to sort integers in an array in ascending order. It prompts the user to enter the number of elements and values to store in an array, then uses two nested for loops to compare adjacent elements and swap them if out of order, repeating this process until the array is fully sorted from lowest to highest value. The sorted array is then printed out.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
601 views
Bubble Sort in C
This C code implements bubble sort to sort integers in an array in ascending order. It prompts the user to enter the number of elements and values to store in an array, then uses two nested for loops to compare adjacent elements and swap them if out of order, repeating this process until the array is fully sorted from lowest to highest value. The sorted array is then printed out.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
Bubble sort 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 < { for (d = 0 ; d { if (array[d] { swap array[d] array[d+1] } } }
( n - 1 ); c++) < n - c - 1; d++) > array[d+1]) /* For decreasing order use < */ = array[d]; = array[d+1]; = swap;
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); }