Help with running the program

I am trying to get the time for sorting a large array using bubble sort and insertion sort. however, my program always crash when I tried the size of 750000, I think its because of my compiler or operating system. Can someone run the code for me with the input 750000, and tell me the result? Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include <ctime>
#include <time.h>
using namespace std;

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--;
            }
      }
}

void bubbleSort(int arr[], int n) {
      bool swapped = true;
      int j = 0;
      int tmp;
      while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < n - j; i++) {
                  if (arr[i] > arr[i + 1]) {
                        tmp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = tmp;
                        swapped = true;
                  }
            }
      }
}

int main(){
    
   srand(time(NULL)); 
  
   time_t begin, end; 
    
    int size;
    cout<<"Enter the number of items in the arrays: ";
    cin>>size;
   
   int array1[size];
   int array2[size];
   
   for(int i=0; i<size; i++)
   { 
     array1[i] = (rand()%2147483648);
   }
   
   for(int i=0; i<size; i++)
   {
   array2[i]=array1[i];
   }
   
    time(&begin);
    bubbleSort(array1,size);
    time(&end);
    cout<<"Time elapsed: " << difftime(end, begin) << " seconds"<< endl;
 
    time(&begin);
    insertionSort(array2,size);
    time(&end);
    cout<<"Time elapsed: " << difftime(end, begin) << " seconds"<< endl;

    
    return 0;
}



Iam not sure how much you can (max) allocate on stack, did you try to create your arrays dynamically?
1
2
3
4
5
6
int size;
    cout<<"Enter the number of items in the arrays: ";
    cin>>size;
   
   int* array1 = new int[size];
   int* array2 = new int[size];
Read this http://cs.nyu.edu/exact/core/doc/stackOverflow.txt , don't know how much is it accurate.
Last edited on
thanks, but can you just run the program for me and give me the results?
thanks, but can you just run the program for me and give me the results?


Do you not have a compiler?

Few tips:
1. Change all post to prefix increment
2. This:
1
2
3
4
5
6
7
8
9
10
for(int i=0; i<size; i++)
   { 
     array1[i] = (rand()%2147483648);
   }
   
   for(int i=0; i<size; i++)
   {
   array2[i]=array1[i];
   }

to this:
1
2
3
4
5
for(int i=0; i<size; ++i)
   { 
     array1[i] = (rand()%2147483648);
     array2[i]=array1[i];
    }

3. Use std::vector with std::sort, qsort...
4. Use queryperformancecounter/queryperformancefrequency functions instead time_t if avilable
try putting unsigned or long in line 6 of your program

void insertionSort(int arr[], int length) {

void insertionSort(int arr[], unsigned int length) {

i thing for just int(signed) in you can assign value between +65535 to -65536
Last edited on
Topic archived. No new replies allowed.