0

I'm trying to make a insertion/merge sort program, does both, and it needs to accept inputs up to 10 million series long arrays. For merge sort thats fine it takes a few seconds to sort it, but it should take over 6 hours for insertion according to my friends. I'd like to put a timer in to the program to make it just stop after 30 minutes of working and not getting done with the sorting somehow but I'm not sure how, or where I would put it.

Heres my code for my main method and insertion sort, since its the only one that needs a timer. Anyone have any idea what to do or where to start?

void insertionSort(int arr[], int length) {
    int i, j, tmp;
    for (i = 1; i < length; i++) {
        j = i;
        while (j > 0 && arr[j - 1] > arr[j]) {
            tmp = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = tmp;
            j--;
        }
    }
}

int main()
{
    srand (time(0));

    long x;

    cout << "how long will the array be?\n" <<
    "10, 100, 1000, 10000, 100000, 1000000, or 10000000?" << endl;
    cin >> x;

    switch(x){

        case 10:
            x = 10;
            break;

        case 100:
            x = 100;
            break;

        case 1000:
            x = 1000;
            break;

        case 10000:
            x = 10000;
            break;

        case 100000:
            x = 100000;
            break;

        case 1000000:
            x = 1000000;
            break;

        case 10000000:
            x = 10000000;
            break;

        default:
            cout << "Error, incorrect number entered, please try again!" << endl;


    }

    static int ar[10000000];

    for(int i = 0; i < x; i++){

        ar[i] = rand() % 100000001;
    }


    int c= 0;
    cout << "which sorting method would you like to use?\n" <<
    "Insertion(1), merge(2), or quick(3)? \nPlease enter the number beside the one you want to use" << endl;
    cin >> c;

    if(c == 1){
        insertionSort(ar, x);

    }
    else if(c==2){
        for (int i = 1; i < x; i *= 2) {
            for (int j = 0; j < x- i; j += 2*i) {
                int iEnd2 = (2*i < x - j) ? 2*i : x - j;
                Merge(&(ar[j]), i, iEnd2);
            }
        }


    }    else if(c==3){
        quickSort(ar,0,x-1);

    }    else{

        cout << "You did not enter a correct number, please try again" << endl;


    }

    for(int i = 0; i < x; i++){
        cout << ar[i] << endl;
    }

    return 0;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
user2060164
  • 97
  • 1
  • 2
  • 6

3 Answers3

0

Depending on your OS system you can set up special environments for your program to run in such that if it takes too long (or other conditions) the program will terminate. Alternatively you can spawn a thread that just counts down as long as you want. If it finishes and the main thread is still running, you can kill the program from there.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

You could do it with the ctime module easily I guess:

#include <ctime>
#define HALF_HOUR 60 * 30
using namespace std;
...
clock_t start = clock();
clock_t now;
int i, j, tmp;
for (i = 1; i < length; i++) {
    j = i;
    while (j > 0 && arr[j - 1] > arr[j]) {
          now = clock();
          if ((now - start) / CLOCKS_PER_SEC) >= HALF_HOUR)
          {
              cout << "Insert sort timed out." << endl;
              return;
          }
          tmp = arr[j];
          arr[j] = arr[j - 1];
          arr[j - 1] = tmp;
          j--;
    }
MadeOfAir
  • 2,933
  • 5
  • 31
  • 39
0

The easiest way is to implement a timed callback function. That function will be called after specified time interval and you can exit the program inside that function.

More information about how to implement it, can be found in the following link

c++ Implementing Timed Callback function

Community
  • 1
  • 1
SRF
  • 929
  • 1
  • 7
  • 15