fork download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <unistd.h> // for usleep()
  4.  
  5. int main() {
  6. int arr[10] = {34, 78, 12, 90, 45, 67, 23, 88, 56, 10};
  7. int n = 10; // size of array
  8. int x = 10; // element to search
  9. int i;
  10. clock_t start, end;
  11. double time_taken;
  12.  
  13. start = clock();
  14.  
  15. // Linear Search with delay
  16. for (i = 0; i < n; i++) {
  17. usleep(100000); // delay in microseconds (1,000,000 µs = 1 sec)
  18. if (arr[i] == x) {
  19. printf("Element found at index %d\n", i);
  20. break;
  21. }
  22. }
  23.  
  24. end = clock();
  25.  
  26. time_taken = ((double)(end - start) / CLOCKS_PER_SEC) * 1000.0; // in ms
  27.  
  28. printf("Time taken: %lf ms\n", time_taken);
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5312KB
stdin
45
stdout
Element found at index 9
Time taken: 0.402000 ms