Question: How do i fix the bubblesort function to sort a million random unsorted items of int value, below code works for sizes smaller than 1000,
How do i fix the bubblesort function to sort a million random unsorted items of int value, below code works for sizes smaller than 1000, but does not compile for a million elements?
#include
class Student{ public: int id; string name; };
const int ARR_SIZE=1000000; const int ID_MIN= 1000; const int ID_MAX= 9999;
void sortbubble(Student list[],int length){ Student temp; for(long i = 0; i < length; i++){ for(long j = 0; j < length-i-1; j++) { if (list[j].id > list[j+1].id) { temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; } } } }
int main() { double t1, t2; Student * arr= new Student [ARR_SIZE]; int input; srand((int)time(0)); // initialize the random number generator for (int i=0; i cout<< "Enter 1 for bubble sort or another int to exit"<< endl; cin >> input; while(input) { if (input ==1){ t1 = clock(); sortbubble(arr,ARR_SIZE); t2 = clock(); cout << "Bubble Sort\t: " << (t2 - t1)/ CLOCKS_PER_SEC<< " sec "; } else{ return 0; } cin >> input; } delete [] arr; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
