jueves, 9 de junio de 2022

C Sorting integer data from file and calculate execution time

https://www.geeksforgeeks.org/sorting-integer-data-from-file-and-calculate-execution-time/


Sorting integer data from file and calculate execution time

  • Last Updated : 05 Nov, 2018

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

  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.

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

No hay comentarios:

Publicar un comentario