C program to sort an array of ten elements in an ascending order

An array is a group of related data items that are stored with a single name. In this article, we'll learn how to sort an array of ten elements in ascending order using the bubble sort technique.

For example, int student[30]; Here, student is an array name which holds 30 collection of data items with a single variable name.

Syntax

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

Array Operations

The common operations performed on arrays are −

  • Searching − Finding whether a particular element is present or not.
  • Sorting − Arranging elements in ascending or descending order.
  • Traversing − Processing every element sequentially.
  • Inserting − Adding elements to an array.
  • Deleting − Removing elements from an array.

Sorting Algorithm Logic

We use the selection sort technique with swapping to sort elements in ascending order. The algorithm compares each element with all subsequent elements and swaps them if they are in the wrong order −

for(i = 0; i < 10-1; i++) {
    for(j = i+1; j < 10; j++) {
        if(element[i] > element[j]) {
            temp = element[i]; // Swapping element[i] with element[j]
            element[i] = element[j];
            element[j] = temp;
        }
    }
}

Example

Below is the complete C program to sort an array of ten elements in ascending order −

#include <stdio.h>

int main() {
    int element[10] = {64, 25, 12, 22, 11, 90, 88, 76, 50, 42};
    int i, j, temp;
    
    printf("Original array: ");
    for(i = 0; i < 10; i++) {
        printf("%d ", element[i]);
    }
    printf("<br>");
    
    // Sorting algorithm
    for(i = 0; i < 10-1; i++) {
        for(j = i+1; j < 10; j++) {
            if(element[i] > element[j]) {
                temp = element[i]; // Swapping element[i] with element[j]
                element[i] = element[j];
                element[j] = temp;
            }
        }
    }
    
    printf("Elements in ascending order: ");
    for(i = 0; i < 10; i++) {
        printf("%d ", element[i]);
    }
    printf("<br>");
    
    return 0;
}
Original array: 64 25 12 22 11 90 88 76 50 42 
Elements in ascending order: 11 12 22 25 42 50 64 76 88 90 

How It Works

  • The outer loop runs from index 0 to n-2 (where n is array size).
  • The inner loop runs from i+1 to n-1 for each iteration of outer loop.
  • If current element is greater than the next element, they are swapped.
  • After each complete pass, the smallest remaining element moves to its correct position.

Conclusion

This program demonstrates array sorting using selection sort with a time complexity of O(n²). The algorithm efficiently arranges ten elements in ascending order through systematic comparison and swapping.

Updated on: 2026-03-15T13:19:54+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements