0 оценок0% нашли этот документ полезным (0 голосов)
140 просмотров1 страница
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.
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.
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.
#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]); }