C Program to Sort an Array in Ascending Order Using a Function

Introduction

Sorting an array in ascending order involves rearranging the elements of the array so that each element is less than or equal to the next element. This guide will show you how to write a C program that sorts an array in ascending order using a separate function.

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 using a function.
  • Displays the sorted array.

Example:

  • Input: Array = [5, 2, 9, 1, 5, 6]
  • Output: Sorted Array = [1, 2, 5, 5, 6, 9]

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Function to Sort the Array: Define a function that takes the array and its size as parameters and sorts the array using a sorting algorithm (e.g., Bubble Sort).
  3. Write the Main Function: Define the main function, which is the entry point of every C program.
  4. Declare Variables: Declare variables to store the array size and the array elements.
  5. Input the Array Size: Use scanf to take input from the user for the size of the array.
  6. Input the Array Elements: Use a loop to take input from the user for the elements of the array.
  7. Call the Sorting Function: Call the function to sort the array in ascending order.
  8. Display the Sorted Array: Use printf to display the sorted array.

C Program

#include <stdio.h>

/**
 * Function to sort an array in ascending order using Bubble Sort
 */
void sortArray(int arr[], int n) {
    int temp;
    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;
            }
        }
    }
}

int main() {
    // Step 1: Declare variables to hold the array size and elements
    int n;

    // 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: Call the function to sort the array in ascending order
    sortArray(arr, n);

    // Step 6: Display the sorted array
    printf("Sorted array in ascending order: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;  // Step 7: Return 0 to indicate successful execution
}

Explanation

Function: sortArray

  • Parameters:
    • arr[]: The array to be sorted.
    • n: The size of the array.
  • Functionality:
    • The function uses the Bubble Sort algorithm to sort the array in ascending order.
    • It iterates through the array multiple times, comparing adjacent elements and swapping them if they are in the wrong order.
    • After each pass, the largest unsorted element "bubbles" up to its correct position.

Main Function: main

Step 1: Declare Variables

  • The variable n is declared to store the size of the array.

Step 2: Input the Array Size

  • The program prompts the user to enter the size of the array using printf. The scanf function reads the input and stores it in the variable n.

Step 3: Declare the Array

  • The program declares an array arr of size n to hold the elements provided by the user.

Step 4: Input the Array Elements

  • The program uses a for loop to take input for each element of the array. The loop iterates from 0 to n-1, reading the elements using scanf.

Step 5: Call the Sorting Function

  • The program calls the sortArray function, passing the array arr and its size n as arguments. This function sorts the array in ascending order.

Step 6: Display the Sorted Array

  • After the sorting process is complete, the program displays the sorted array using a for loop and the printf function.

Step 7: 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
Sorted array in ascending order: 11 12 22 25 64

Another Example:

Enter the number of elements in the array: 4
Enter 4 elements:
9
1
4
7
Sorted array in ascending order: 1 4 7 9

Conclusion

This C program demonstrates how to sort an array in ascending order using a separate function. It covers basic concepts such as arrays, functions, loops, and sorting algorithms, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top