sorting_main.c~ 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "sorting.h"
  5. int main ( int argc , char ** argv ){
  6. //check arguments
  7. if (argc != 5 || (argv[1][0] != 'i' && argv[1][0] != 's')){
  8. printf("usage: ./proj1 <i (insertion)|s (selection)> <input file> <sequence output file> <sorted output file>\n");
  9. return EXIT_FAILURE;
  10. }
  11. // values to be printed
  12. double numComparisons = 0;
  13. double numMoves = 0;
  14. // other declarations
  15. long * buffer = NULL;
  16. int size = 0;
  17. clock_t sortingClock = clock();
  18. double sortingTime = 0;
  19. clock_t ioClock = clock();
  20. double ioTime = 0;
  21. // read file into long * array
  22. ioClock = clock();
  23. buffer = Load_File(argv[2], &size);
  24. ioClock = clock() - ioClock;
  25. ioTime = ((double) ioClock) / CLOCKS_PER_SEC;
  26. // save sequence
  27. ioClock = clock();
  28. Print_Seq(argv[3], size);
  29. ioClock = clock() - ioClock;
  30. ioTime = ioTime + ((double) ioClock) / CLOCKS_PER_SEC;
  31. // sort depending on arguments
  32. if(argv[1][0] == 'i'){
  33. sortingClock = clock();
  34. Shell_Insertion_Sort(buffer, size, &numComparisons, &numMoves);
  35. sortingClock = clock() - sortingClock;
  36. sortingTime = ((double) sortingClock) / CLOCKS_PER_SEC;
  37. }
  38. if(argv[1][0] == 's'){
  39. sortingClock = clock();
  40. Shell_Selection_Sort(buffer, size, &numComparisons, &numMoves);
  41. sortingClock = clock() - sortingClock;
  42. sortingTime = ((double) sortingClock) / CLOCKS_PER_SEC;
  43. }
  44. // save sorted sequence
  45. ioClock = clock();
  46. Save_File(argv[4], buffer, size);
  47. ioClock = clock() - ioClock;
  48. ioTime = ioTime + ((double) ioClock) / CLOCKS_PER_SEC;
  49. // print values
  50. printf("\n");
  51. printf("Number of comparisons: %le\n", numComparisons);
  52. printf("Number of moves: %le\n", numMoves);
  53. printf("I/O time: %le\n", ioTime);
  54. printf("Sorting time: %le\n", sortingTime);
  55. printf("\n");
  56. // free memory
  57. free(buffer);
  58. // success
  59. return EXIT_SUCCESS;
  60. }