Here, we input elements in an array and sort that array using pointers. We can sort the array using ascending and descending order normally without and with pointer also. Logic to sort an array using pointers in this program we follow both cases.
Like,
Example:
Input:
Input Array Elements Like This: 4, -3, 56, 90, -12, 1
Result:
Array In Ascending Order: -12, -3, 1, 4, 56, 90
Array In Descending Order: 90, 56, 4, 1, -3, -12
Basic Knowledge Before Writing This Code:
- Basic C Programming,
- Array,
- Pointer,
- Function Pointer,
- Functions,
Logic Or Steps To Sort An Array
- Enter the size and elements in an array. Store them in variables like “n” and “a”.
- Then we declare two functions prototypes like
int sortAscending(int * num1, int * num2)
andint sortDescending(int * num1, int * num2)
. These two functions are mainly used to arrange in ascending and descending order. sortAscending() performs ascending and sortDescending() performs descending order. - sortAscending returns a negative value if n1 is less than n2, a positive value if
n1
is greater thann2
and zero if both are equal. - Similarly
sortDescending()
returns a negative value if n1 is greater than n2, a positive value ifn
2 is greater thann
1 and zero if both are equal. - Declare another function to sort an array with a prototype, void sort(int * arr, int size, int (* compare)(int *, int *)).
- It takes three-parameter, where the first parameter is the array to sort, second is the size of the array and third is a function to the pointer.
Note: The function pointer [int (* compare)(int *, int *)] passed here will decide the relationship between two elements to be sorted. If you want to sort elements in ascending order then pass the reference of
sortAscending()
function, otherwisesortDescending()
function.Lingaraj Senapati
Program To Sort An Array Using Pointers:
/** * C program to sort an array using pointers. */ #include <stdio.h> #define SIZE 100 /* Function declaration */ void inArray(int * a, int size); void printArray(int * a, int size); /* Sort function declaration */ int sortAscending(int * num1, int * num2); int sortDescending(int * num1, int * num2); void sort(int * a, int size, int (* compare)(int *, int *)); int main() { int a[SIZE]; int n; /* * Input array size and elements. */ printf("Enter array size: "); scanf("%d", &n); printf("Enter elements in array: "); inArray(a, n); printf("\n\nElements before sorting: "); printArray(a, n); // Sort and print sorted array in ascending order. printf("\n\nArray in ascending order: "); sort(a, n, sortAscending); printArray(a, n); // Sort and print sorted array in descending order. printf("\nArray in descending order: "); sort(a, n, sortDescending); printArray(a, n); return 0; } /** * Function to take input in array elements. * * @arr Array to store input. * @size Size of the array. */ void inArray(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 * n1, int * n2) { return (*n1) - (*n2); } /** * 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 * n1, int * n2) { return (*n2) - (*n1); } /** * 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++; } }
Note: In the above program we can’t use swapping elements normally, I have used bitwise XOR operator to swap two array elements.
Lingaraj Senapati
Output:
Enter array size: 5
Enter Elements in An Array: -2
3
76
-9
54
Elements Before Sorting: -2 3 76 -9 54
Array In Ascending Order: -9 -2 3 54 76
Array In Descending Order: 76 54 3 -2 -9
If you find any issue and interested to rewrite it then contact us.