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:
1 2 |
Enter desired array size(1-100): 10 Please enter array elements(10) : 34 56 76 23 -087 -45 0 76 198 64 |
Output:
1 2 |
Original array: 34 56 76 23 -87 -45 0 76 198 64 Sorted Array(Ascending Order): -87 -45 0 23 34 56 64 76 76 198 |
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
- We have array values[] array with size number of variables.
-
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])
- if values[j] > values[j+1]
-
for j = 0 to size-i-1
- Once the above loop is terminated, values[] array will be sorted in ascending order.
Pseudo code for the bubble sort in c language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void bubbleSort(int values[], int size) { 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]) } } } } |
📢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.
- Declare the array values with max size of 100. Also declare supporting variables like i, j, size, and temp variables.
- Prompt the user to provide the desired array size and store it in size variable.
- 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.
- Print the original array using another for loop.
- 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 j – j++
- Nested for loop, Iterate from j=0 till the size-i-1
- increment the variable i – i++
- Start the first for loop, Traverse from i=0 till the i<size-1
- Once the above two for loops are completed, Then the values array will be sorted in Ascending order.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* Program to sort the numbers using bubble sort Author: SillyCodes.com */ #include <stdio.h> int main() { // declare the 'values' array int values[100]; int i, j, size, temp; printf("Enter desired array size(1-100): "); scanf("%d", &size); // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("values[%d] : ", i); scanf("%d", &values[i]); } // Print the original array printf("Original Array: "); for (i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); // Sort the array in ascending order // We will use the bubble sort for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (values[j] > values[j + 1]) { // Swap the elements using 'temp' variable temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } // Print the sorted array printf("Sorted Array(Ascending Order): "); for (i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); return 0; } |
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:
1 2 3 4 5 6 |
$ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 34 56 76 23 -087 -45 0 76 198 64 Original array: 34 56 76 23 -87 -45 0 76 198 64 Sorted Array(Ascending Order): -87 -45 0 23 34 56 64 76 76 198 $ |
Test 2: Let’s try a few more example inputs and observe the output.
1 2 3 4 5 6 7 8 9 10 11 |
$ ./a.out Enter desired array size(1-100): 5 Please enter array elements(5) : 48 23 8 124 8 Original array: 48 23 8 124 8 Sorted Array(Ascending Order): 8 8 23 48 124 $ ./a.out Enter desired array size(1-100): 3 Please enter array elements(3) : 4 7 2 Original array: 4 7 2 Sorted Array(Ascending Order): 2 4 7 $ |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
/* Program to sort the numbers using bubble sort Author: SillyCodes.com */ #include <stdio.h> /** * @brief - Read the user input and update the 'values' array * * @param values - 'values' array * @param size - size of the 'values' array */ void read(int values[], int size) { int i; // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("values[%d] : ", i); scanf("%d", &values[i]); } } /** * @brief - Display the 'values' array * * @param values - Array * @param size - Array size */ void display(int values[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); } /** * @brief - Sort the values array in ascending order * * @param values - values array * @param size - size of the array */ void bubbleSort(int values[], int size) { int i, j, temp; // Sort the array in ascending order // We will use the bubble sort for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (values[j] > values[j + 1]) { // Swap the elements using 'temp' variable temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } } int main() { // declare the 'values' array int values[100]; int size; printf("Enter desired array size(1-100): "); scanf("%d", &size); // User input for array elements read(values, size); // Print the original array printf("Original Array: "); display(values, size); // call bubbleSort function bubbleSort(values, size); // Print the sorted array printf("Sorted Array(Ascending Order): "); display(values, size); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ gcc sort-array-asc-func.c $ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 23 43 56 12 -48 47 -68 0 25 15 Original Array: 23 43 56 12 -48 47 -68 0 25 15 Sorted Array(Ascending Order): -68 -48 0 12 15 23 25 43 47 56 $ ./a.out Enter desired array size(1-100): 5 Please enter array elements(5) : 45 85 21 36 97 Original Array: 45 85 21 36 97 Sorted Array(Ascending Order): 21 36 45 85 97 $ ./a.out Enter desired array size(1-100): 7 Please enter array elements(7) : 87 96 -21 -45 0 -54 78 Original Array: 87 96 -21 -45 0 -54 78 Sorted Array(Ascending Order): -54 -45 -21 0 78 87 96 $ |
Related Array Programs:
- Index – C Language Practice Programs
- C Tutorials Index – Step by step tutorials to master C Langauge
- C Program to Read and Print Arrays
- Reverse Print Array Program in C
- C Program to calculate Sum of all array Elements
- C Program to Calculate the Average of all array Elements
- C Program to find minimum element in Array
- C Program to calculate the total number of Positive numbers, Negative numbers, and Zeros in an Array
- C Program to Count Even and Odd numbers in Array
- C Program to Find Maximum Element in Array
- C Program to Reverse the Array Elements
- C Program to Reverse Array Elements using Recursion
3 Responses
[…] C Program to Sort Array Elements in Ascending order […]
[…] C Program to Sort Array Elements in Ascending order […]
[…] C Program to Sort Array Elements in Ascending order […]