Advertisement
Guest User

Untitled

a guest
May 26th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. //Selection sort
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <time.h>
  6.  
  7. //#define maxsize 10
  8.  
  9. void selection_sort(int [],int);
  10. clock_t BeginTimer();
  11. clock_t EndTimer(clock_t);
  12.  
  13. int compare=0,move =0;
  14.  
  15. void main()
  16. {
  17. double begin;
  18. float elapTicks;
  19. float elapMilli, elapSeconds, elapMinutes;
  20.  
  21.  
  22.  
  23. int num_elements = 0, data[100000],i;
  24.  
  25. /*Read in values from data.txt*/
  26.  
  27. FILE *input; /*declare a file */
  28. input = fopen("data.txt", "r"); /*read the file*/
  29.  
  30. printf("The random numbers in data.txt are:\n\n");
  31.  
  32. while (fscanf(input,"%d", &data[num_elements])!= EOF) /* read from file*/
  33. {
  34. printf("%d\t",data[num_elements]); /*display to the screen */
  35. num_elements++;
  36. }
  37.  
  38. printf ("Timer set to: %.2f\n", begin); // print the initialised timer (0)
  39. begin = BeginTimer();
  40.  
  41. /*Called the selection sort function */
  42. selection_sort(data, num_elements);
  43.  
  44.  
  45.  
  46.  
  47. // variable definitions on to calculate time taken
  48. elapTicks = EndTimer(begin); // stop the timer, and calculete the time taken
  49. elapMilli = elapTicks/1000; // milliseconds from Begin to End
  50. elapSeconds = elapMilli/1000; // seconds from Begin to End
  51. elapMinutes = elapSeconds/60; // minutes from Begin to End
  52.  
  53.  
  54. /*Display the sorted list of number */
  55. printf("\nSorted list for selection sort: \n");
  56. for(i=0; i<num_elements; i++)
  57. printf(" %d",data[i]);
  58.  
  59. printf("\n\n\nNumber of data compare is %d",compare);
  60. printf("\nNumber of data move is %d\n",move);
  61.  
  62. printf ("\n\nMilliseconds passed: %.2f\n", elapMilli);
  63. printf ("Seconds passed: %.2f\n", elapSeconds);
  64. printf ("Minutes passed: %.2f\n", elapMinutes);
  65.  
  66.  
  67. getch();
  68. }
  69.  
  70.  
  71. void selection_sort(int data[],int num_elements)
  72. {
  73. int smallest;
  74. int a, b, temp;
  75.  
  76. for(a=0;a<num_elements; a++)
  77. {
  78. smallest = a;
  79. for(b=a+1;b<num_elements; b++)
  80. { if(data [b]<data[smallest])
  81. { smallest=b; }
  82. compare++;
  83.  
  84. }
  85. temp=data[a];
  86. move++;
  87. data[a]=data[smallest];
  88. move++;
  89. data[smallest]=temp;
  90. move++;
  91. }
  92.  
  93. }
  94.  
  95.  
  96. clock_t BeginTimer()
  97. {
  98. //timer declaration
  99. clock_t Begin; //initialize Begin
  100.  
  101. Begin = clock() * CLK_TCK; //start the timer
  102.  
  103. return Begin;
  104. }
  105. clock_t EndTimer(clock_t begin)
  106. {
  107. clock_t End;
  108. End = clock() * CLK_TCK; //stop the timer
  109. return End;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement