This repository contains C implementations and analysis of two classic sorting algorithms: Selection Sort and Insertion Sort. It also includes utilities for generating random data and benchmarking the algorithms.
sort1.c: Implements Selection Sort and reads input data from a file.sort2.c: Implements Insertion Sort and reads input data from a file.file_handling.c: Generates a file (file.txt) with up to 100,000 random integers for testing.Analysis.txt: Detailed time complexity analysis and practical performance comparison of both algorithms.data: Empirical timing data for both sorts on various input sizes.
-
Generate Input Data
- Compile and run
file_handling.cto createfile.txtwith random numbers:gcc file_handling.c -o file_handling ./file_handling
- Compile and run
-
Run Sorting Algorithms
- Compile and run either
sort1.c(Selection Sort) orsort2.c(Insertion Sort):orgcc sort1.c -o sort1 ./sort1gcc sort2.c -o sort2 ./sort2 - When prompted, enter the filename (e.g.,
file.txt) and the number of integers to sort.
- Compile and run either
-
View Analysis
- See
Analysis.txtfor theoretical and empirical analysis of both algorithms.
- See
- Repeatedly selects the minimum element from the unsorted part and moves it to the sorted part.
- Time Complexity: O(n²)
- Builds the sorted array one element at a time by inserting each element into its correct position.
- Time Complexity: O(n²) (but generally faster than Selection Sort for practical data)
- See the
datafile for timing results on various input sizes. - In practice, Insertion Sort (
sort2.c) outperforms Selection Sort (sort1.c) for the tested data. 

- All programs expect the input file to contain one integer per line.
- Maximum supported input size is 100,000 integers.
- Output is printed to the console after sorting.
This project is for educational purposes.