C Program to Sort an Array using a Pointer

In this tutorial, i am going to show you how to write a program to sort an array with the help of pointer in c.

C Program to Sort an Array using a Pointer

#include <stdio.h>
void SortArray(int Size, int* parr)
{
	int i, j, temp;	
	for (i = 0; i < Size; i++)
	{
		for (j = i + 1; j < Size; j++)
		{
			if(*(parr + j) < *(parr + i))
			{
				temp = *(parr + i);
				*(parr + i) = *(parr + j);
				*(parr + j) = temp;
			}			
		}
	}
	printf("\nSorted Array Elements using Pointer = ");
	for(i = 0; i < Size; i++)
	{
		printf("%d  ", *(parr + i));
	}	
}
int main()
{
	int Size;
	printf("\nEnter Array Size to Sort using Pointers = ");
	scanf("%d", &Size);
	int arr[Size];
	printf("\nPlease Enter %d elements of an Array = ", Size);
	for (int i = 0; i < Size; i++)
	{
		scanf("%d", &arr[i]);
    }  	
	SortArray(Size, arr);   
	printf("\n");	
}

The result of the above c program; is as follows:

Enter Array Size to Sort using Pointers = 5
Please Enter 5 elements of an Array = 3 5 7 1 9
Sorted Array Elements using Pointer = 1  3  5  7  9

Recommended C Programs

Leave a Comment