Sorting using function pointer comparator Dry Run in C

Sorting using function pointer comparator is an interactive C dry run visualizer from the Pointersfunctions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Sorting using function pointer comparator Program Code

#include <stdio.h>

typedef int (*Comparator)(int, int);

int compareAscending(int a, int b) {
    return a > b;
}

void bubbleSort(int arr[], int size, Comparator compare) {
    int i, j, temp;
    for(i = 0; i < size - 1; i++) {
        for(j = 0; j < size - i - 1; j++) {
            if(compare(arr[j], arr[j + 1])) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int i;
    int numbers[] = {5, 2, 4, 1, 3};
    int size = 5;
    
    printf("Original array: ");
    for(i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    bubbleSort(numbers, size, compareAscending);
    
    printf("\nSorted array: ");
    for(i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

View the complete Pointersfunctions programs page.

Program Console Sorting using function pointer comparator Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.