C (programming language)

C program to print given data in ascending order

Sorting and organizing data is a fundamental task in programming. This guide explores writing a C program to print a given set of data in ascending order, showcasing different sorting algorithms and their implementation details.

Understanding Sorting in C

C offers various sorting algorithms with varying strengths and weaknesses. Choosing the appropriate algorithm depends on factors like data size, input type, and performance considerations. Here are some common approaches:

  • Bubble Sort: A simple iterative method that repeatedly swaps adjacent elements if they are in the wrong order, gradually bubbling up the largest elements.
  • Selection Sort: Finds the minimum element in the unsorted part of the array and swaps it with the first element, repeating this process until the entire array is sorted.
  • Insertion Sort: Inserts each element at its correct position in a sorted sub-array, building the sorted array gradually.
  • Merge Sort: Divides the data into smaller sub-arrays, recursively sorts them, and then merges them back together in ascending order.
  • Quick Sort: Uses a pivot element to partition the data into smaller sub-arrays and sorts them recursively for efficient sorting performance.

Implementing Bubble Sort

Let’s explore Bubble Sort as an example:


void bubbleSort(int data[], int size) {
  for (int i = 0; i < size - 1; i++) {
    for (int j = 0; j < size - i - 1; j++) {
      if (data[j] > data[j + 1]) {
        int temp = data[j];
        data[j] = data[j + 1];
        data[j + 1] = temp;

int main() {
  int data[] = {5, 2, 7, 1, 9};
  int size = sizeof(data) / sizeof(data[0]);

  bubbleSort(data, size);

  printf("Data in ascending order: ");
  for (int i = 0; i < size; i++) {
    printf("%d ", data[i]);

  return 0;

This code defines a bubbleSort function that iterates through the data array, comparing adjacent elements and swapping them if they are in the wrong order. The main function demonstrates how to use this function to sort a given array and print the data in ascending order.

Exploring Other Sorting Algorithms

While Bubble Sort is easy to understand and implement, it may not be the most efficient for large datasets. Consider exploring other algorithms like Selection Sort, Insertion Sort, Merge Sort, or Quick Sort based on your specific needs and performance requirements. Each algorithm has its own advantages and disadvantages, and choosing the optimal one depends on the context of your program.

Additional Features

You can further enrich your program by:

  • Handling different data types: Adapt the sorting functions to work with various data types like characters, floats, or custom structures.
  • Reading data from files or user input: Implement functionalities to read data from external sources instead of using pre-defined arrays.
  • Providing sorting options: Allow users to choose between different sorting algorithms based on their needs.

By understanding different sorting algorithms and their implementation in C, you can tackle data organization tasks efficiently and write versatile programs for various practical applications.

Remember, the choice of sorting algorithm and additional features depends on your specific goals and program requirements. Don’t hesitate to experiment and explore different techniques to enhance your understanding and build robust solutions for data sorting in C.

I hope this comprehensive guide provides a valuable resource for writing C programs to print data in ascending order and beyond. Feel free to ask if you have any further questions or need help with implementing specific algorithms or functionalities!


Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

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

Back to top button