C Program to Sort an Array Using Pointers

Introduction

Sorting an array involves arranging its elements in a specific order, either ascending or descending. In C, you can sort an array using pointers to traverse and manipulate the elements directly. This guide will show you how to write a C program to sort an array using pointers.

Example:

  • Input: Array [5, 2, 9, 1, 5, 6]
  • Output: Sorted array [1, 2, 5, 5, 6, 9]

Problem Statement

Create a C program that:

  • Takes an array of integers as input from the user.
  • Uses pointers to sort the array in ascending order.
  • Displays the sorted array.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Array and Pointer Variables: Declare an integer array to store the elements and pointer variables for sorting.
  3. Input the Array Elements: Use a loop to take input for the array elements from the user.
  4. Sort the Array Using Pointers: Use a sorting algorithm (like Bubble Sort) to sort the array by manipulating the elements through pointers.
  5. Display the Sorted Array: Use a loop to display the sorted array.

C Program to Sort an Array Using Pointers

#include <stdio.h> int main() { // Step 2: Declare the array and pointer variables int arr[100], n; int *ptr1, *ptr2, temp; // Step 3: Input the number of elements and the array elements printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter the elements of the array: "); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Step 4: Sort the array using pointers (Bubble Sort) for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { ptr1 = &arr[j]; ptr2 = &arr[j + 1]; if (*ptr1 > *ptr2) { // Compare the elements pointed by ptr1 and ptr2 // Swap the elements temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } } } // Step 5: Display the sorted array printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; // Return 0 to indicate successful execution } 

Explanation

Step 2: Declare the Array and Pointer Variables

  • The integer array arr is declared to store the elements of the array.
  • The pointers ptr1 and ptr2 are used to traverse and compare the elements of the array.
  • The variable temp is used for swapping elements during sorting.

Step 3: Input the Array Elements

  • The program prompts the user to enter the number of elements in the array and stores this value in n.
  • A for loop is used to take input for each element of the array.

Step 4: Sort the Array Using Pointers

  • The program uses a nested for loop to implement the Bubble Sort algorithm:
    • The outer loop runs n-1 times, where n is the number of elements in the array.
    • The inner loop compares each element with the next element using pointers ptr1 and ptr2.
    • If the element pointed to by ptr1 is greater than the element pointed to by ptr2, the elements are swapped using the temp variable.
  • This process continues until the entire array is sorted in ascending order.

Step 5: Display the Sorted Array

  • The program uses a for loop to display the sorted array.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the number of elements: 6 Enter the elements of the array: 5 2 9 1 5 6 Sorted array: 1 2 5 5 6 9 

Conclusion

This C program demonstrates how to sort an array using pointers. It covers basic concepts such as pointer manipulation, array traversal, and sorting algorithms, making it a useful example for beginners learning C programming.

Leave a Comment

Scroll to Top