C Program to Sort an Array using a Pointer

C Program to Sort an Array using a Pointer

Program to sort an array using pointer in c; Through this tutorial, we will learn how to write a program to sort an array using 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 output 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *