C program to sort array

Category: C Program

Learn how to write a C program to sort array elements in both ascending and descending order. This article provides detailed explanations and sample code for sorting arrays using the Bubble Sort algorithm.

Sorting array elements is a fundamental task in C programming, essential for organizing data in a specific order. This article will guide you through writing a C program to sort array elements in both ascending and descending order, providing detailed explanations and sample code.

Steps to Sort Array Elements

To sort array elements, we can follow these steps:

  1. Input the Array: Read the array elements from the user.
  2. Sort the Array: Implement a sorting algorithm to sort the elements.
  3. Print the Result: Output the sorted array to the console.

Sorting in Ascending Order

We'll use the Bubble Sort algorithm for simplicity. This algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Example Code for Ascending Order

#include <stdio.h>

int main() {
    int size;

    // Input size and elements of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);
    int arr[size];
    printf("Enter %d elements -\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    // Bubble Sort in Ascending Order
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap the elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    // Print the sorted array
    printf("\nArray sorted in ascending order: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output

Enter the size of the array: 5
Enter 5 elements -
4
2
6
9
3

Array sorted in ascending order: 2 3 4 6 9

Sorting in Descending Order

To sort the array in descending order, we can modify the Bubble Sort algorithm to swap elements if they are in the correct order (i.e., the first element is less than the second element).

Just do this small change to sort elements in descending order.

for (int i = 0; i < size - 1; i++) {
    for (int j = 0; j < size - 1 - i; j++) {
        if (arr[j] < arr[j + 1]) {
            // Swap the elements
            int temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}

Explanation

  1. Input the Array: The user inputs the size of the array and its elements.
  2. Sort the Array:
    • For ascending order, the Bubble Sort algorithm iterates through the array, comparing adjacent elements and swapping them if they are in the wrong order.
    • For descending order, the Bubble Sort algorithm is modified to swap elements if they are in the correct order.
  3. Print the Result: The program prints the elements of the sorted array.

Detailed Steps

  • Step 1: Input the Array
    • The program prompts the user to enter the size of the array.
    • The user inputs the array elements.
  • Step 2: Sort the Array
    • Bubble Sort for Ascending Order: The algorithm runs nested loops. The outer loop runs for the number of elements minus one, and the inner loop compares adjacent elements, swapping them if they are in the wrong order.
    • Bubble Sort for Descending Order: The inner loop is modified to swap elements if the first element is less than the second element.
  • Step 3: Print the Result
    • The program prints the elements of the sorted array.

Advantages of Bubble Sort

  • Simplicity: Bubble Sort is easy to understand and implement.
  • Small Data Sets: It performs well on small data sets.

Disadvantages of Bubble Sort

  • Inefficiency: Bubble Sort has a time complexity of O(n^2), making it inefficient for large arrays.
  • Unoptimized: It performs unnecessary comparisons and swaps, leading to slower performance compared to more advanced sorting algorithms like Quick Sort or Merge Sort.

Recommended Posts