0

I am currently encountering an issue with the timer in my sorting algorithm, I am using a bubble sort.

I require the timer to record the time when my algorithm runs where as at the moment it is taking in to consideration user time to open and enter values in to the program (which I do not want)

I decided to move the timer start after where it takes user input and am getting 0 as real time, which is not correct.

Does anyone know how to fix this?

(please note the code below will not build - just focus on the timer and where it starts/ends)

#include <stdio.h>
#include <time.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

    clock_t t;     
    t = clock();    // timer starts here

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  t = clock() - t; // ends timer

      printf("Sorted list in ascending order:\n");

      for ( c = 0 ; c < n ; c++ )
         printf("%d\n", array[c]);



      return 0;
        }
  • try to hard code or generate using `rand()` a larger input (1000 and more) instead of typing it, then let you algorithm run. – A4L Mar 13 '13 at 22:18
  • The `clock` function has too coarse a resolution. Use a millisecond or better resolution timer, *and* increase the size of the array until you get one second or more of running time. – n. m. could be an AI Mar 13 '13 at 22:22

0 Answers0