Question

My following program has an array which holds 1000 random integers between 1-1000. Now I need...

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 <cstdlib>
#include <ctime>
#include <iostream>

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 << "\nTime taken by searching: " << timeElapsed << " milliseconds" << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

//function bubble sort to sort the array
void bubbleSort(int a[])
{
    int i, j;
    int temp;

    for (i = 1; i < 10000; i++) {//change made
        for (j = 0; j < 10000 - i; j++) {//change made
            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 = 10000 - 1;//change made
    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[10000];//change made
    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 < 10000; i++) {//change made
        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 << "\nTime taken by searching: " << timeElapsed << " milliseconds" << endl;
}

We can see it now takes considerably more time than when it had 1000 elements

Add a comment
Know the answer?
Add Answer to:
My following program has an array which holds 1000 random integers between 1-1000. Now I need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Question 1) Suppose a program has the following code: const int X = 2, Y =...

    Question 1) Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3},                                  {4, 5, 6}}; for(int i=0; i<X; i++) {      sum=0;      for(int j=0; j<Y; j++)         sum+=values[i][j];    cout<<sum<<endl; } What is this program getting the sum of? Group of answer choices D-) The columns of the 2D array C-) The rows of the 2D array A-) All of the elements of the 2D...

  • REWRITE the code below and declare a class that holds an array of 10. Create a...

    REWRITE the code below and declare a class that holds an array of 10. Create a constructor that initializes the array with random integers 1 and 100. Write a member function to show the entire array and another to sort the array. Rewrite with instruction above: //Exchange Sort Function for Descending Order void ExchangeSort(vector<int> &num) {      int i, j;      int temp;   // holding variable      int numLength = num.size();      for (i=0; i< (numLength -1); i++)       {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT