VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-sort-an-array-using-pointers/

⇱ C program to sort an array using pointers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C program to sort an array using pointers

Last Updated : 11 Jul, 2025

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. 👁 Image
 

Below is the implementation of the above approach:  

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