Friday, September 27, 2024

File-Based Sorting of Integer Data Using Selection Sort in C++

 #include <iostream>

#include <fstream>


using namespace std;


// Function to sort an array of integers using Selection Sort

void sortData(int arr[], int size) {

    for (int i = 0; i < size - 1; ++i) {

        for (int j = i+1; j < size; ++j) {

            if (arr[i] > arr[j]) {

                // Swap the elements

                int temp = arr[i];

                arr[i] = arr[j];

                arr[j] = temp;

            }

        }

    }

}


int main() {

    ifstream inputFile("input.txt");

    ofstream outputFile("output.txt");


    if (!inputFile) {

        cerr << "Error opening input file!" << endl;

        return 1;

    }


    if (!outputFile) {

        cerr << "Error opening output file!" << endl;

        return 1;

    }


    const int MAX_SIZE = 1000; // Maximum number of integers to handle

    int numbers[MAX_SIZE];

    int count = 0;


    // Read numbers from the input file

    while (inputFile >> numbers[count]) {

        count++;

        if (count >= MAX_SIZE) {

            cerr << "Too many numbers in input file. Max allowed is " << MAX_SIZE << endl;

            return 1;

        }

    }


    // Sort the data

    sortData(numbers, count);


    // Write the sorted numbers to the output file

    for (int i = 0; i < count; ++i) {

        outputFile << numbers[i] << endl;

    }


    // Close the files

    inputFile.close();

    outputFile.close();


    cout << "Data sorted and written to output.txt" << endl;


    return 0;

}

____________________

This C++ program reads a list of integers from a file named input.txt, sorts the numbers using the Selection Sort algorithm, and then writes the sorted numbers to a file named output.txt. Here's a breakdown of what the program does:

Include Libraries:

The program includes <iostream> for input/output operations and <fstream> for file handling.
Function to Sort Data (Selection Sort):

sortData(int arr[], int size) is a function that performs Selection Sort on the input array arr[] of size size. It repeatedly finds the smallest element in the unsorted part of the array and swaps it with the element at the beginning of the unsorted part.
Main Function:

File Handling:
ifstream inputFile("input.txt"); opens the input file for reading.
ofstream outputFile("output.txt"); opens the output file for writing.
It checks whether the files are opened successfully. If not, it prints an error message and terminates.
Reading Data from File:

The program defines a constant MAX_SIZE of 1000 to limit the number of integers it can handle. It reads the integers from input.txt into the array numbers[] until either the file ends or it reaches the limit of MAX_SIZE.
If more than 1000 numbers are present in the input file, an error message is printed, and the program exits.
Sorting the Data:

Once the integers are loaded into the array, the sortData() function is called to sort them using the Selection Sort algorithm.
Writing Data to Output File:

After sorting, the program writes the sorted integers to the output file output.txt.
Closing Files:

The input and output files are properly closed at the end.
Program Completion:

A success message Data sorted and written to output.txt is displayed on the console, indicating that the task was completed.
Key Components:
Input/Output: Reading from input.txt, writing to output.txt.
Selection Sort: Sorting algorithm to arrange the numbers in ascending order.
Error Handling: Checks if files are opened correctly and handles excessive input.
This program efficiently handles sorting and file I/O with simple error handling and basic sorting logic.

No comments:

Post a Comment

NaCl.xyz Generator with Interactive 3D Visualization

NaCl.xyz Generator with Interactive 3D Visualization NaCl.xyz Generator with Interactive 3D Visualization ...