Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Sorting Algorithms in C with Comparison Counters

Tech May 18 3

1. Bubble Sort Implementation

#include <stdio.h>
#define TERMINATOR -9999
#define CAPACITY 1005

int data[CAPACITY], elements;
int comparisons;

void performBubbleSort() {
    int outer, inner;
    int temporary;
    for(outer = 0; outer < elements-1; outer++) {
        for(inner = 0; inner < elements-outer-1; inner++, ++comparisons) {
            if(data[inner] > data[inner+1]) {
                temporary = data[inner];
                data[inner] = data[inner+1];
                data[inner+1] = temporary;
            }
        }
    }
}

int main() {
    int value;
    printf("Bubble Sort Demonstration\n");
    printf("Enter integer sequence:\n");
    for(elements = 0; scanf("%d", &value) && value != TERMINATOR; data[elements++] = value) {}
    performBubbleSort();
    for(value = 0; value < elements; printf("%d%c", data[value], " \n"[value+1 == elements]), ++value) {}
    printf("Total comparisons made: %d\n", comparisons);
    return 0;
}

2. Element Occurrnece Counter

#include <stdio.h>
#define CAPACITY 1005
#define TERMINATOR -9999

int sequence[CAPACITY], count;

int main() {
    int input, occurrences = 0;
    printf("Enter number sequence ending with -9999:\n");
    for(count = 0; scanf("%d", &input) && input != TERMINATOR; sequence[count++] = input) {}
    for(input = 0; input < count; input++) {
        if(sequence[input] == sequence[count-1]) {
            occurrences++;
        }
    }
    printf("Last element %d appears %d times\n", sequence[count-1], occurrences);
    return 0;
}

3. Insertion Sortt with Comparison Tracking

#include <stdio.h>
#define TERMINATOR -9999
#define CAPACITY 1005

int dataset[CAPACITY], item_count;
int operation_count;

void executeInsertionSort() {
    int i, j, k, current;
    for(operation_count = 0, i = 1; i < item_count; ++i) {
        for(j = i - 1; j >= 0 && dataset[j] > dataset[i]; ++operation_count, --j) {}
        current = dataset[i];
        for(k = i; k > j + 1; dataset[k] = dataset[k - 1], --k) {}
        dataset[j + 1] = current;
    }
}

int main() {
    int num;
    for(item_count = 0; scanf("%d", &num) && num != TERMINATOR; dataset[item_count++] = num) {}
    executeInsertionSort();
    printf("Comparison operations: %d\n", operation_count);
    for(num = 0; num < item_count; printf("%d%c", dataset[num], " \n"[num+1 == item_count]), ++num) {}
    return 0;
}

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.