Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.
Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.
C Program
//The Bubble sort method to sort a given list of integers in ascending order.
#include <stdio.h>
int main() {
int arr[50], n, i;
void bubbleSort(int [], int );
// Input number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
// Input elements
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Call bubble sort function
bubbleSort(arr, n);
// Print sorted array
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
// Bubble sort function
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
OUTPUT
Enter number of elements: 9
Enter 9 integers:
4 5 6 1 7 2 8 3 9
Sorted list in ascending order:
1 2 3 4 5 6 7 8 9
๐ป Run This C Program
Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.
The selected C program is loaded automatically. Edit the code,
provide input through STDIN and click Run.
The compiler is embedded using OneCompiler. Your program runs in
the embedded C environment; Study Glance does not execute C code
directly on your web server.