| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "sorting.h"
- int main ( int argc , char ** argv ){
- //check arguments
- if (argc != 5 || (argv[1][0] != 'i' && argv[1][0] != 's')){
- printf("usage: ./proj1 <i (insertion)|s (selection)> <input file> <sequence output file> <sorted output file>\n");
- return EXIT_FAILURE;
- }
- // values to be printed
- double numComparisons = 0;
- double numMoves = 0;
- // other declarations
- long * buffer = NULL;
- int size = 0;
- clock_t sortingClock = clock();
- double sortingTime = 0;
- clock_t ioClock = clock();
- double ioTime = 0;
- // read file into long * array
- ioClock = clock();
- buffer = Load_File(argv[2], &size);
- ioClock = clock() - ioClock;
- ioTime = ((double) ioClock) / CLOCKS_PER_SEC;
- // save sequence
- ioClock = clock();
- Print_Seq(argv[3], size);
- ioClock = clock() - ioClock;
- ioTime = ioTime + ((double) ioClock) / CLOCKS_PER_SEC;
- // sort depending on arguments
- if(argv[1][0] == 'i'){
- sortingClock = clock();
- Shell_Insertion_Sort(buffer, size, &numComparisons, &numMoves);
- sortingClock = clock() - sortingClock;
- sortingTime = ((double) sortingClock) / CLOCKS_PER_SEC;
- }
- if(argv[1][0] == 's'){
- sortingClock = clock();
- Shell_Selection_Sort(buffer, size, &numComparisons, &numMoves);
- sortingClock = clock() - sortingClock;
- sortingTime = ((double) sortingClock) / CLOCKS_PER_SEC;
- }
- // save sorted sequence
- ioClock = clock();
- Save_File(argv[4], buffer, size);
- ioClock = clock() - ioClock;
- ioTime = ioTime + ((double) ioClock) / CLOCKS_PER_SEC;
- // print values
- printf("\n");
- printf("Number of comparisons: %le\n", numComparisons);
- printf("Number of moves: %le\n", numMoves);
- printf("I/O time: %le\n", ioTime);
- printf("Sorting time: %le\n", sortingTime);
- printf("\n");
- // free memory
- free(buffer);
- // success
- return EXIT_SUCCESS;
- }
|