Question: My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random
My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program.
The main goal of this program is time analysis by using bubble sort and binary search algorithms.
Please do the following task;
1. Replace the 1000 random integers with 10,000 random integers
After change please answer the following question
2. what will be happen, if an array holds 10,000 random integers between 1-1000 in this program? (Run the program first then answer the question)
//.cpp
#include
using namespace std;
//function bubble sort to sort the array void bubbleSort(int a[]) { int i, j; int temp;
for (i = 1; i < 1000; i++) { for (j = 0; j < 1000 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } }
//binary search fuction to search the element in the array void binarySearch(int arr[], int search) { int i, first, last, middle; first = 0; last = 1000 - 1; middle = (first + last) / 2;
while (first <= last) { if (arr[middle] < search) { first = middle + 1; } else if (arr[middle] == search) { cout << search << " found at location " << middle + 1; break; } else { last = middle - 1; } middle = (first + last) / 2; } if (first > last) { cout << "Not found! " << search << " is not present in the list."; } }
int main() {
int array[1000]; int search, n = 0; long timeElapsed;
//start time and end time stored here clock_t start, end; srand((unsigned) time(0));
for (int i = 0; i < 1000; i++) { array[i] = (rand() % 1000) + 1; } while (n < 4) { //count the start time start = clock(); bubbleSort(array); //count the end time end = clock(); //time taken by sorting timeElapsed = end - start; cout << "Time taken by sorting :" << timeElapsed << " milliseconds" << endl; n++; } cout << "Enter the number you want to search" << endl; cin >> search;
//count the time before searching start = clock(); binarySearch(array, search); //count the time after searching end = clock(); timeElapsed = end - start;
//print the time taken by searching cout << " Time taken by searching: " << timeElapsed << " milliseconds" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
