Sorting integer data from file and calculate execution time Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisite: Selection Sort In this article, we are going to apply selection sort algorithm, in which the source of input is A FILE CONTAINING 10000 INTEGERS and output will be the total time taken to sort. Important functions to be used: rand(): Used to generate random numbers. fopen(): Used to open file . fscanf(): Used to scan data from file . clock(): Return the number of clock cycle Program to generate random number in a file "random.txt" C // C program to generate random numbers #include <stdio.h> #include <stdlib.h> // Driver program int main(void) { // This program will create same sequence of // random numbers on every program run FILE* fptr; // creating a file "random.txt" in "write" mode fptr = fopen("random.txt", "w"); int i; if (fptr == NULL) { printf("ERROR"); exit(1); } for (i = 0; i < 10000; i++) { // to generate number less than 100000 int val = rand() % 100000; // storing data to file fprintf(fptr, "%d ", val); } // closing the file fclose(fptr); printf("numbers generated successfully !! "); return 0; } clock_t or Clock ticks are units of time of a constant but system-specific length, as those returned by function clock. Algorithm for this program: Open the file using fopen(). Scan the file and copy it to the array using fscanf(). Apply any sorting algorithm that you want. Print to console. Below are the implementation of above algorithm. C #include <stdio.h> #include <time.h> int main() { // data type for calculating time clock_t starttime, endtime; // variable for calculating total time of execution double totaltime; int i = 0, j, n = 0, min, index; // declaring array to store data from file int arr[100000]; // declaring file pointer FILE* fptr; // opening the integer file. fptr = fopen("random.txt", "r"); // scanning integer from file to array while (fscanf(fptr, "%d", &arr[i]) == 1) { // for counting the number of elements n++; // for incrementing the array index i++; } // logic for selection sort.... // starts here... // calculating clock when sorting starts.. starttime = clock(); printf("start time : %f\n", (float)starttime); for (i = 0; i < n - 1; i++) { min = arr[i]; for (j = i + 1; j < n; j++) { if (arr[j] < min) { min = arr[j]; index = j; } } // swapping the smallest number with // the current arr[i]th value int temp = arr[i]; arr[i] = min; arr[index] = temp; } // selection sort logic ends here // calculating clock when sorting ends endtime = clock(); printf("%f\n", (float)endtime); totaltime = ((double)(endtime - starttime)) / CLOCKS_PER_SEC; // printing the sorted array... for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n\nendtime : %f\n", (float)endtime); printf("\n\ntotal time of execution = %f", totaltime); return 0; } References: https://www.geeksforgeeks.org/dsa/selection-sort-algorithm-2/ https://www.geeksforgeeks.org/cpp/how-to-measure-time-taken-by-a-program-in-c/ https://www.geeksforgeeks.org/c/basics-file-handling-c/ Comment More infoAdvertise with us Next Article Time Complexities of all Sorting Algorithms R Rohit_ranjan Follow Improve Article Tags : Sorting Technical Scripter C Programs DSA Technical Scripter 2018 Sorting Quiz C-File Handling +3 More Practice Tags : Sorting Similar Reads Sort Numbers from File Using Unix Pipes in C In this article, we will write a straightforward C program that sorts numerical data from a file using pipes and forks.The sorting process is carried out using the sort command and the execlp function of UNIX. Working of the ProgramWe need to include the following header files to use the relevant fu 3 min read Measure execution time of a function in C++ We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we 3 min read Commonly Asked Data Structure Interview Questions on Sorting Sorting is a fundamental concept in computer science and data structures, often tested in technical interviews. Sorting algorithms are essential for organizing data in a specific order, whether it's ascending or descending. Understanding various sorting techniquesâlike Quick Sort, Merge Sort, Bubble 4 min read Commonly Asked Data Structure Interview Questions on Sorting Sorting is a fundamental concept in computer science and data structures, often tested in technical interviews. Sorting algorithms are essential for organizing data in a specific order, whether it's ascending or descending. Understanding various sorting techniquesâlike Quick Sort, Merge Sort, Bubble 4 min read Time Complexities of all Sorting Algorithms The efficiency of an algorithm depends on two parameters:Time ComplexityAuxiliary SpaceBoth are calculated as the function of input size(n). One important thing here is that despite these parameters, the efficiency of an algorithm also depends upon the nature and size of the input. Time Complexity:T 2 min read Sleep Sort â The King of Laziness / Sorting while Sleeping In this algorithm we create different threads for each of the elements in the input array and then each thread sleeps for an amount of time which is proportional to the value of corresponding array element. Hence, the thread having the least amount of sleeping time wakes up first and the number gets 11 min read Like