Callback Sort

Callback Sort

Medium C Functions 33 views
Explanation Complexity

Problem Statement

Create a generic function to sort the array that accepts a comparison function as a callback. Implement both ascending and descending using different callbacks.

Input Format

An array of elements, array size, and a comparison function (callback).

Output Format

The sorted array based on the comparison logic (ascending or descending).

Example

Array: [5, 2, 9, 1]
Comparison: ascending
[1, 2, 5, 9]

Constraints

• Array size ≥ 1

• Comparison function must define ordering

Concept Explanation

The sorting function does not decide order itself.
It uses a callback comparison function to decide how two elements should be ordered.

Step-by-Step Explanation

1.Create a generic sort function that receives:

• Array

• Size of array

• Comparison function

2.Inside the sort function, compare two elements using the callback.

3.If the comparison function indicates wrong order:

• Swap the elements.

4.Repeat comparisons until the array is fully sorted.

5.For ascending order:

• Callback returns true when first element is greater than second.

6.For descending order:

• Callback returns true when first element is smaller than second.

7.Pass the required callback to the sort function to get desired order.

Concept Explanation

The sorting function does not decide order itself.
It uses a callback comparison function to decide how two elements should be ordered.

Step-by-Step Explanation

1.Create a generic sort function that receives:

• Array

• Size of array

• Comparison function

2.Inside the sort function, compare two elements using the callback.

3.If the comparison function indicates wrong order:

• Swap the elements.

4.Repeat comparisons until the array is fully sorted.

5.For ascending order:

• Callback returns true when first element is greater than second.

6.For descending order:

• Callback returns true when first element is smaller than second.

7.Pass the required callback to the sort function to get desired order.

Input / Output Format

Input Format
An array of elements, array size, and a comparison function (callback).
Output Format
The sorted array based on the comparison logic (ascending or descending).
Constraints
• Array size ≥ 1

• Comparison function must define ordering

Examples

Input:
Array: [5, 2, 9, 1] Comparison: ascending
Output:
[1, 2, 5, 9]

Example Solution (Public)

C
#include <stdio.h>

/* Comparison function type */
typedef int (*CompareFunc)(int, int);

/* Ascending comparison */
int ascending(int a, int b) {
    return a - b;  // positive if a > b, negative if a < b
}

/* Descending comparison */
int descending(int a, int b) {
    return b - a;  // positive if b > a, negative if b < a
}

/* Generic bubble sort function */
void sort(int arr[], int n, CompareFunc cmp) {
    int i, j, temp;

    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (cmp(arr[j], arr[j + 1]) > 0) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

/* Function to print an array */
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("n");
}

int main() {
    int arr1[] = {5, 2, 9, 1, 7};
    int arr2[] = {5, 2, 9, 1, 7};
    int n = 5;

    printf("Original array: ");
    printArray(arr1, n);

    /* Sort ascending */
    sort(arr1, n, ascending);
    printf("Ascending sort: ");
    printArray(arr1, n);

    /* Sort descending */
    sort(arr2, n, descending);
    printf("Descending sort: ");
    printArray(arr2, n);

    return 0;
}

Official Solution Code

#include <stdio.h>

/* Comparison function type */
typedef int (*CompareFunc)(int, int);

/* Ascending comparison */
int ascending(int a, int b) {
    return a - b;  // positive if a > b, negative if a < b
}

/* Descending comparison */
int descending(int a, int b) {
    return b - a;  // positive if b > a, negative if b < a
}

/* Generic bubble sort function */
void sort(int arr[], int n, CompareFunc cmp) {
    int i, j, temp;

    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (cmp(arr[j], arr[j + 1]) > 0) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

/* Function to print an array */
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("n");
}

int main() {
    int arr1[] = {5, 2, 9, 1, 7};
    int arr2[] = {5, 2, 9, 1, 7};
    int n = 5;

    printf("Original array: ");
    printArray(arr1, n);

    /* Sort ascending */
    sort(arr1, n, ascending);
    printf("Ascending sort: ");
    printArray(arr1, n);

    /* Sort descending */
    sort(arr2, n, descending);
    printf("Descending sort: ");
    printArray(arr2, n);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.