Introduction
Sorting an array involves arranging the elements in a specific order. This guide will show you how to write a C program to sort an array in both ascending and descending order using the Bubble Sort algorithm.
Problem Statement
Create a C program that:
- Takes the size of the array as input from the user.
- Takes the elements of the array as input.
- Sorts the array in ascending order and displays it.
- Sorts the array in descending order and displays it.
Example:
- Input: Array = [5, 2, 9, 1, 5, 6]
- Output:
- Ascending Order = [1, 2, 5, 5, 6, 9]
- Descending Order = [9, 6, 5, 5, 2, 1]
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the array size, the array elements, and temporary variables for swapping.
- Input the Array Size: Use
scanfto take input from the user for the size of the array. - Input the Array Elements: Use a loop to take input from the user for the elements of the array.
- Sort the Array in Ascending Order: Use the Bubble Sort algorithm to sort the array in ascending order.
- Display the Array in Ascending Order: Use
printfto display the sorted array. - Sort the Array in Descending Order: Use the Bubble Sort algorithm to sort the array in descending order.
- Display the Array in Descending Order: Use
printfto display the sorted array.
C Program
#include <stdio.h>
/**
* C Program to Sort an Array in Ascending and Descending Order
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the array size, elements, and a temporary variable for swapping
int n, temp;
// Step 2: Prompt the user to enter the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Step 3: Declare an array to hold the elements
int arr[n];
// Step 4: Input the array elements
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Step 5: Sort the array in ascending order using Bubble Sort
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Step 6: Display the array in ascending order
printf("Array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Step 7: Sort the array in descending order using Bubble Sort
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] < arr[j+1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Step 8: Display the array in descending order
printf("Array in descending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0; // Step 9: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
nis declared to store the size of the array.tempis used as a temporary variable for swapping elements during sorting.
Step 2: Input the Array Size
- The program prompts the user to enter the size of the array using
printf. Thescanffunction then reads the input and stores it in the variablen.
Step 3: Declare the Array
- The program declares an array
arrof sizento hold the elements provided by the user.
Step 4: Input the Array Elements
- The program uses a
forloop to take input for each element of the array. The loop iterates from 0 ton-1, reading the elements usingscanf.
Step 5: Sort the Array in Ascending Order
- The program uses the Bubble Sort algorithm to sort the array in ascending order:
- The outer loop iterates through the entire array
n-1times. - The inner loop compares each element with its adjacent element and swaps them if they are in the wrong order. The smallest element "bubbles" up to its correct position at the beginning of each pass.
- The outer loop iterates through the entire array
Step 6: Display the Array in Ascending Order
- After the sorting process is complete, the program displays the sorted array using a
forloop and theprintffunction.
Step 7: Sort the Array in Descending Order
- The program uses the Bubble Sort algorithm again, but this time to sort the array in descending order:
- The outer loop iterates through the entire array
n-1times. - The inner loop compares each element with its adjacent element and swaps them if they are in the wrong order. The largest element "bubbles" up to its correct position at the beginning of each pass.
- The outer loop iterates through the entire array
Step 8: Display the Array in Descending Order
- After the sorting process is complete, the program displays the sorted array in descending order using a
forloop and theprintffunction.
Step 9: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example:
Enter the number of elements in the array: 5
Enter 5 elements:
64
25
12
22
11
Array in ascending order: 11 12 22 25 64
Array in descending order: 64 25 22 12 11
Another Example:
Enter the number of elements in the array: 4
Enter 4 elements:
9
1
4
7
Array in ascending order: 1 4 7 9
Array in descending order: 9 7 4 1
Conclusion
This C program demonstrates how to sort an array in both ascending and descending order using the Bubble Sort algorithm. It covers basic concepts such as arrays, loops, and sorting algorithms, making it a useful example for beginners learning C programming.