Sorting integer data from file and calculate execution time

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”

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


  • 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:

  1. Open the file using fopen().
  2. Scan the file and copy it to the array using fscanf().
  3. Apply any sorting algorithm that you want.
  4. Print to console.

Below are the implementation of above algorithm.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


References:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.