fork(1) download
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. const int sz = 10000000;
  9.  
  10. int arr[sz];
  11. int X, pos;
  12.  
  13. bool check(int index, int X) {
  14. if (index <= 0) {
  15. return arr[index] == X;
  16. }
  17. return (arr[index] == X) && (arr[index - 1] != X);
  18. }
  19.  
  20. int linearSearch(int *arr, int X) {
  21. for (int i=0; i<sz; i++){
  22. if (X == arr[i]) return i;
  23. }
  24. return -1;
  25. }
  26.  
  27. int main(){
  28. for (int i=0; i<sz; i++) arr[i] = i;
  29.  
  30. int startTime = time(0);
  31. for (int t=0; t<1000; t++) {
  32. X = arr[rand() % sz];
  33. pos = linearSearch(arr, X);
  34.  
  35. if (!check(pos, X)) {
  36. cout<<"FAILED";
  37. }
  38. }
  39. cout<<"Total time for search: "<<(time(0) - startTime)<<"(s)";
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 3.43s 42624KB
stdin
Standard input is empty
stdout
Total time for search: 3(s)