Sorting the Data from a File


Create a text file that contains the names and marks of students in a class.Then, read the data from the file, apply the Quick Sort algorithm to sort the students based on their marks in decreasing order, and store the sorted data back into a file

This is a very practical file handling + sorting algorithm problem 👏.



🎯 Goal

You need to:

  1. Create a text file that contains student names and marks.

  2. Read data from that file.

  3. Sort students by marks in decreasing order using Quick Sort.

  4. Write the sorted data back to a new file.


🧠 Algorithm

Input Format (in text file)

Alice 85 Bob 92 Charlie 78 David 95 Eve 88

Steps

  1. Read the data from file

    • Open the input file (e.g., students.txt) in read mode.

    • Read each line → extract name and marks.

    • Store them in an array of structures.

  2. Apply Quick Sort

    • Choose a pivot (first or last element).

    • Partition the array so that:

      • Marks greater than pivot are on the left (for decreasing order).

      • Marks smaller than pivot are on the right.

    • Recursively apply Quick Sort on both halves.

  3. Write back to file

    • Open a new output file (e.g., sorted_students.txt).

    • Write the sorted list to the file.


Time Complexity

  • Best/Average: O(n log n)

  • Worst: O(n²) (rare, depends on pivot selection)

C Program: Quick Sort Students by Marks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100
#define MAX_NAME_LEN 50

// Define a structure for Student
struct Student {
    char name[MAX_NAME_LEN];
    int marks;
};

// Function to swap two students
void swap(struct Student *a, struct Student *b) {
    struct Student temp = *a;
    *a = *b;
    *b = temp;
}

// Partition function for Quick Sort (decreasing order)
int partition(struct Student arr[], int low, int high) {
    int pivot = arr[high].marks;
    int i = low - 1;

    for (int j = low; j < high; j++) {
        if (arr[j].marks > pivot) { // for decreasing order
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

// Quick Sort implementation
void quickSort(struct Student arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int main() {
    FILE *inputFile, *outputFile;
    struct Student students[MAX_STUDENTS];
    int n = 0;

    // Step 1: Read from input file
    inputFile = fopen("students.txt", "r");
    if (inputFile == NULL) {
        printf("Error: Cannot open input file.\n");
        return 1;
    }

    while (fscanf(inputFile, "%s %d", students[n].name, &students[n].marks) == 2) {
        n++;
    }
    fclose(inputFile);

    // Step 2: Sort using Quick Sort
    quickSort(students, 0, n - 1);

    // Step 3: Write sorted data to output file
    outputFile = fopen("sorted_students.txt", "w");
    if (outputFile == NULL) {
        printf("Error: Cannot create output file.\n");
        return 1;
    }

    fprintf(outputFile, "Name\tMarks\n");
    fprintf(outputFile, "------------------\n");

    for (int i = 0; i < n; i++) {
        fprintf(outputFile, "%s\t%d\n", students[i].name, students[i].marks);
    }

    fclose(outputFile);

    printf("Sorting completed. Check 'sorted_students.txt' for results.\n");
    return 0;
}

Sorting completed. Check 'sorted_students.txt' for results.

Input File- students.txt

Alice 85
Bob 92
Charlie 78
David 95
Eve 88

Output File- 
Name Marks
------------------
David 95
Bob 92
Eve 88
Alice 85
Charlie 78

Comments

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Lab Assignments

Singly Linked List - Operations