C Program to Sort an Array in Ascending Order Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example: C #include <stdio.h> #include <stdlib.h> // Custom comparator int comp(const void* a, const void* b) { // If a is smaller, positive value will be returned return (*(int*)a - *(int*)b); } int main() { int arr[] = { 2 ,6, 1, 5, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); // Sort the array using qsort qsort(arr, n, sizeof(int), comp); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output1 2 3 4 5 6 Explanation: The comp() function is the comparator that returns difference between the first and second element, otherwise returns 0 or negative value. This will sort the array in ascending order.C language only provides the built-in implementation of quicksort algorithm. If you want to use other sorting algorithm apart from quicksort, you have to manually implement it.Table of ContentUsing Selection SortUsing Bubble SortUsing Selection SortSelection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and places it at its position in the sorted part of the array until the complete array is sorted. C #include <stdio.h> // Selection sort implementation void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int min = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min]) min = j; } if (min != i) { int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } } int main() { int arr[] = { 2 ,6, 1, 5, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); // Perform Selection Sort selectionSort(arr,n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output1 2 3 4 5 6 Using Bubble SortIn Bubble Sort Algorithm, we repeatedly swap adjacent elements if they are in the wrong order. This process is repeated until the array is sorted. C #include <stdio.h> // Bubble sort implementation void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = { 2 ,6, 1, 5, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); // Perform bubble sort bubbleSort(arr,n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output1 2 3 4 5 6 Comment More infoAdvertise with us Next Article C Program to Sort an Array in Ascending Order C code_r Follow Improve Article Tags : C Programs C Language Computer Science Fundamentals DSA C-Arrays C Basic Programs +2 More Similar Reads C Program to Sort the Elements of an Array in Descending Order Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending 3 min read C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h 3 min read How to Sort an Array of Structs Based on a Member in C? In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: myArrayofStructs[] = {{"Person1", 21, 160.5}, {"Person2", 20, 175.0}, {"Person3 2 min read How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read C Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg 3 min read C Program To Sort 2D Array Across Rows Here, we will see how to sort the 2D Array across rows using a C program: Input: 8 5 7 2 7 3 0 1 8 5 3 2 9 4 2 1 Output: 2 5 7 8 0 1 3 7 2 3 5 8 1 2 4 9Approach: In this approach, we use Bubble Sort. First Start iterating through each row of the given 2D array, and sort elements of each row using th 2 min read Sort Matrix in alternating ascending and descending order rowwise Given an N x N matrix, our task is to print the row of the matrix in ascending and descending order alternatively.Examples: Input: 5 7 3 4 9 5 8 2 6 3 8 1 5 8 9 3 Output: 3 4 5 7 9 8 5 2 1 3 6 8 9 8 5 3 Explanation: Here the first row is sorted in ascending order, second row sorted in descending ord 10 min read C Program to Traverse an Array in Reverse Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in 2 min read C Program to Sort a String of Characters Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program.The most straightforward method to sort a string of characters is by using the qsort() function. Let's take 3 min read Like