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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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