Write a C program to input elements in an array and sort array using pointers. How to sort an array in ascending or descending order using function pointers in C programming. Logic to sort an array using pointers in program.
Below is the step by step descriptive logic to sort an array using pointer.
size and arr.int sortAscending(int * num1, int * num2) and int sortDescending(int * num1, int * num2).
Both the functions are used to compare two elements and arrange it in either ascending or descending order. sortAscending() returns negative value if num1 is less than num2, positive value if num1 is greater than num2 and zero if both are equal.
Similarly, sortDescending() returns negative value if num1 is greater than num2, positive value if num2 is greater than num1 and zero if both are equal.
void sort(int * arr, int size, int (* compare)(int *, int *)).
It takes three parameter, where first parameter is the array to sort, second is size of array and third is a function pointer.
Note: The function pointer passed here will decide relationship between two elements to be sorted. If you want to sort elements in ascending order then pass reference of sortAscending() function, otherwise sortDescending() function.
To sort an array I have used basic sorting algorithm. The algorithm is not efficient when compared to other modern sorting algorithms but its easy to understand and use.
/**
* C program to sort an array using pointers.
*/
#include <stdio.h>
#define MAX_SIZE 100
/* Function declaration */
void inputArray(int * arr, int size);
void printArray(int * arr, int size);
/* Sort function declaration */
int sortAscending(int * num1, int * num2);
int sortDescending(int * num1, int * num2);
void sort(int * arr, int size, int (* compare)(int *, int *));
int main()
{
int arr[MAX_SIZE];
int size;
/*
* Input array size and elements.
*/
printf("Enter array size: ");
scanf("%d", &size);
printf("Enter elements in array: ");
inputArray(arr, size);
printf("\n\nElements before sorting: ");
printArray(arr, size);
// Sort and print sorted array in ascending order.
printf("\n\nArray in ascending order: ");
sort(arr, size, sortAscending);
printArray(arr, size);
// Sort and print sorted array in descending order.
printf("\nArray in descending order: ");
sort(arr, size, sortDescending);
printArray(arr, size);
return 0;
}
/**
* Function to take input in array elements.
*
* @arr Array to store input.
* @size Size of the array.
*/
void inputArray(int * arr, int size)
{
// Pointer to last element of array
int * arrEnd = (arr + size - 1);
// (arr++) Input in current array element and move to next element.
// Till last array element (arr <= arrEnd)
while(arr <= arrEnd)
scanf("%d", arr++);
}
/**
* Function to print all array elements.
*
* @arr Array to print.
* @size Size of the array.
*/
void printArray(int * arr, int size)
{
// Pointer to last element of array
int * arrEnd = (arr + size - 1);
// *(arr++) Print current array element and move to next element.
// Till last array element (arr <= arrEnd)
while(arr <= arrEnd)
printf("%d, ", *(arr++));
}
/**
* Function to compare two succesive elements.
* The function returns difference of first and second integer.
*
* @num1 First integer to compare.
* @num2 Second integer to compare.
*
* @return Difference of num1 and num2.
*/
int sortAscending(int * num1, int * num2)
{
return (*num1) - (*num2);
}
/**
* Function to compare two successive elements.
* The function returns difference of second and first parameter.
*
* @num1 First integer to compare.
* @num2 Second integer to compare.
*
* @return Difference of num2 and num1.
*/
int sortDescending(int * num1, int * num2)
{
return (*num2) - (*num1);
}
/**
* Function to sort an array in ascending or descending order.
* This function is used to sort array in both order ascending or
* descending.
*
* @arr Array to sort.
* @size Size of the array.
* @compare Function pointer returning integer and takes two int *
* parameter. The function is called to get arrangement of
* two successive array elements.
*/
void sort(int * arr, int size, int (* compare)(int *, int *))
{
// Pointer to last array element
int * arrEnd = (arr + size - 1);
// Pointer to current array element
int * curElem = arr;
int * elemToSort;
// Iterate over each array element
while(curElem <= arrEnd)
{
elemToSort = curElem;
// Compare each successive elements with current element
// for proper order.
while(elemToSort <= arrEnd)
{
/*
* Compare if elements are arranged in order
* or not. If elements are not arranged in order
* then swap them.
*/
if(compare(curElem, elemToSort) > 0)
{
*curElem ^= *elemToSort;
*elemToSort ^= *curElem;
*curElem ^= *elemToSort;
}
elemToSort++;
}
// Move current element to next element in array.
curElem++;
}
}
Enter array size: 9
Enter elements in array: 9 8 7 6 5 4 3 2 1
Elements before sorting: 9, 8, 7, 6, 5, 4, 3, 2, 1,
Array in ascending order: 1, 2, 3, 4, 5, 6, 7, 8, 9,
Array in descending order: 9, 8, 7, 6, 5, 4, 3, 2, 1,
In the above program instead of swapping elements normally, I have used bitwise XOR operator to swap two array elements.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.