C program to sort array in ascending order
|In this C Programming example, we will implement the program to sort an array in ascending order and print the output on the console.
1. Sort array in ascending order
In this program, we need to sort the given array in ascending order i.e. smallest to largest. This task can be accomplished using two for loops, the outer loop will select the element and the inner loop will compare the selected element with the rest of the elements in order.
The sorting algorithm that we will use for purpose is known as selection sort.
Let’s see how the sorted array looks after each sort iteration.
Example: Elements of the original array: a[] = 9 2 80 5 10 // Find the minimum element in arr[0...4] // and place it at beginning 2 9 80 5 10 // Find the minimum element in a[1...4] // and place it at beginning of a[1...4] 2 5 80 9 10 // Find the minimum element in a[2...4] // and place it at beginning of a[2...4] 2 5 9 80 10 // Find the minimum element in a[3...4] // and place it at beginning of a[3...4] 2 5 9 80 10
Helpful topics to understand this program better are-
2. Pseudocode to sort array in ascending order – Selection Sort
Let’s discuss the pseudocode for the program to sort an array in ascending order in C.
The pseudo-code tells us the step-by-step manner in which the element is picked and sorted in order using for loop.
Pseudocode: selectionSort(array a) //Search for the minimum element //and adds to the sorted sub array ..for i in 0 -> a.length - 2 do minIndex = i //Find minimum element in the remaining //sub array and update the minIndex ....for j in (i + 1) -> (a.length - 1) do if a[j] < a[minIndex] minIndex = j //Swap the minimum value found with the //first element of unsorted subarray ....swap(a[i], a[minIndex])
3. Pseudocode to sort array in ascending order – Selection Sort
Let us now implement the above execution of the program to sort an array in ascending order in C.
#include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } void selectionSort(int arr[], int n) { int i, j, minIndex; // After every iteration size of sorted array increases by one for (i = 0; i < n - 1; i++) { // Find the minimum element in unsorted array minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; // Swap the found minimum element with the first element swap(&arr[minIndex], &arr[i]); } } // Function to print an array void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) printf("%d ", arr[i]); printf(""); } // Main function to test above implemented methods int main() { int arr[] = {9, 4, 2, 3, 6, 5, 7, 1, 8, 0}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original Array: \n"); printArray(arr, n); selectionSort(arr, n); printf("\n\nSorted array: \n"); printArray(arr, n); return 0; }
Output Original Array: 9 4 2 3 6 5 7 1 8 0 Sorted array: 0 1 2 3 4 5 6 7 8 9
4. Conclusion
In this C Programming example, we have discussed how to sort an array in ascending order in C.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?