C program to sort an array using pointers Last Updated : 26 Oct, 2022 Comments Improve Suggest changes Like Article Like Report 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 pointing to the base address of the array. Hence in order to sort the array using pointers, we need to access the elements of the array using (pointer + index) format. Below is the implementation of the above approach: C #include <stdio.h> // Function to sort the numbers using pointers void sort(int n, int* ptr) { int i, j, t; // Sort the numbers using pointers for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (*(ptr + j) < *(ptr + i)) { t = *(ptr + i); *(ptr + i) = *(ptr + j); *(ptr + j) = t; } } } // print the numbers for (i = 0; i < n; i++) printf("%d ", *(ptr + i)); } // Driver code int main() { int n = 5; int arr[] = { 0, 23, 14, 12, 9 }; sort(n, arr); return 0; } Output:0 9 12 14 23 Time Complexity: O(n2), where n represents the size of the given array.Auxiliary Space: O(1), no extra space is required, so it is a constant. Comment More infoAdvertise with us Next Article C program to sort an array using pointers C code_r Follow Improve Article Tags : C Programs C Language C-Pointers C Array Programs Similar Reads C Program to Sort an Array in Ascending Order 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 3 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 C Program for Pancake sorting Given an unsorted array, sort the given array. You are allowed to do only following operation on array. flip(arr, i): Reverse array from 0 to i C /* C program for Pancake Sorting */ #include <stdio.h> #include <stdlib.h> /* Reverses arr[0..i] */ void flip(int arr[], int i) { int temp, st 2 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 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 Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read How to Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 2 min read How to Use bsearch with an Array of Struct in C? The bsearch function in C is a standard library function defined in the stdlib.h header file that is used to perform binary search on array-like structure. In this article, we will learn to use bsearch with an array of struct in C. Input: struct Person people[] = { { 1, "Ram" }, { 2, "Rohan" }, { 4, 3 min read C Program For Sorting A Linked List Of 0s, 1s And 2s Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL So 3 min read Return an Array in C In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C 5 min read Like