Sorting integer data from file and calculate execution time
Last Updated :
11 Jul, 2025
Improve
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:
C
C
References:
- 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
// 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.
- 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.
#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;
}
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;
}
- 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/