Sort Array Elements in Ascending order in C Language

program-to-sort-array-elements-in-ascending-order-in-c-language

Program Description:

Write a Program to Sort Array Elements in Ascending order in C programming language. We can sort the array using the C Library functions such as qsort function. But we are going to implement the bubble sort to sort the array elements in ascending order.

Here are the expected input and output of the program.

Example Input and Output:

Input:

Output:

Prerequisites:

It is required to know the Arrays and Functions in C language to better understand the following program.

📢Related Article: Passing Arrays to Functions in C

Bubble Sort Algorithm – Sort Array Elements in Ascending order in C :

Bubble sort is a basic sorting algorithm that iterates through the array elements repeatedly, compares adjacent elements, and swaps them if any of the elements are out of order.

The above process is repeated until no swaps are required, at which point the Array is considered to be sorted.

Here is the algorithm for the bubble sort

  1. We have array values[] array with size number of variables.
  2. for i = 0 to size-1
    • for j = 0 to size-i-1
      • if values[j] > values[j+1]
        • swap(values[j], values[j+1])
  3. Once the above loop is terminated, values[] array will be sorted in ascending order.

Pseudo code for the bubble sort in c language:

📢The time complexity of the bubble sort it O(n2). So it will be good for small arrays, But not suitable for large arrays.

Sort Array Elements in Ascending order in C using Bubble sort Explanation:

Let’s look at the step-by-step explanation of sorting an array in the c language.

  1. Declare the array values with max size of 100. Also declare supporting variables like i, j, size, and temp variables.
  2. Prompt the user to provide the desired array size and store it in size variable.
  3. Use a For loop to Iterate through the array and update the array elements using the scanf function.
    • Start the loop from zero(0) index and go til the size-1
    • Update the values[i] element with a user-provided number.
  4. Print the original array using another for loop.
  5. Now, We need to sort the values array using the bubble sort. Bubble sort uses two for loops and swaps the adjacent elements if they are in the wrong order.
    • Start the first for loop, Traverse from i=0 till the i<size-1
      • Nested for loop, Iterate from j=0 till the size-i-1
        • At Each iteration, Check if the values[j] is greater than the values[j+1]. If it is true, Then Swap the values[j] and values[j+1] variables.
        • We are using a temporary variable temp to swap the elements. We can also use other methods like Swapping without using temporary variables
      • Increment the variable jj++
    • increment the variable ii++
  6. Once the above two for loops are completed, Then the values array will be sorted in Ascending order.
  7. Print the sorted Array on the console using another for loop.

Program to Sort Array Elements in Ascending order in C using bubble sort:

Here is the program to sort the array using bubble sort in the c programming language.

Program Output:

Let’s Compile the above C Program using the GCC compiler (any compiler)

gcc sort-array-asc.c

Run the executable file.

Test 1:

sort-array-in-ascending-order-in-c-program-output

Test 2: Let’s try a few more example inputs and observe the output.

As we can see from the above output, The program is sorting the provided array elements in ascending order.

Sort an Array in C using Functions (bubbleSort Function):

Let’s rewrite the above program to use the user-defined function instead of using only the main() function. The functions improve the code readability and debugging and modularity. So let’s divide the above program into a few functions, which do a specific task.

Here is the Array sorting program using functions. We have used three user-defined functions in the following program (expect main() function).

display() Function:

The display() function takes two formal arguments named values array and its size. It simply displays the all elements of the array on the console.

Prototype of the display() function – void display(int values[], int size)

The display() function doesn’t return anything back to the user. so it’s return type is void

read() Function:

read() function is used to read the user input and update the array elements. The read() function also takes two formal arguments values[] array and the size of values array.

Prototype details of the read() function – void read(int values[], int size)

The read() function, updates the values array as values array passed as the address, So all the changes made in the read() function are visible in the main() function.

bubbleSort() Function:

This is the function, where the actual sorting happens. The bubbleSort() function also takes two formal arguments similar to the above two functions – values[] array and size.

Prototype of the bubbleSort() function – void bubbleSort(int values[], int size)

The bubbleSort function uses the bubble sort algorithm to compare and swap the elements and creates a sorted array. bubbleSort function also changes the input values array(Array is passed as address/reference). So all changes made in the bubbleSortfunction are visible in the main() function.

Program Output:

Let’s compile and Run the program.

sort-array-in-c-using-bubble-sort-function-program-output

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C Program to Sort Array Elements in Ascending order […]

  2. […] C Program to Sort Array Elements in Ascending order […]

  3. […] C Program to Sort Array Elements in Ascending order […]

Leave a Reply