Question

In: Computer Science

You can use any programming languages Using a random generator, compute 1000 integers between 1 and...

You can use any programming languages

Using a random generator, compute 1000 integers between 1 and 1000. There will be duplicates in the array. Sort the array using bubble sort and merge sort. The two sorted arrays should agree. Then pick at random one element from your sorted array and use a binary search to find its position in the array.

Solutions

Expert Solution

C program :-

#include <stdio.h>
#include <stdlib.h>

int b_search(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
  
if (arr[mid] == x)
return mid;
  
if (arr[mid] > x)
return b_search(arr, l, mid - 1, x);

return b_search(arr, mid + 1, r, x);
}
  
return -1;
}

void merge_func(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
  
int L[n1], R[n2];
  
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
  
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
  
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
  
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
  
void m_sort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
  
m_sort(arr, l, m);
m_sort(arr, m+1, r);
  
merge_func(arr, l, m, r);
}
}

int main(void)
{
int arr[1000];
int b_arr[1000];
int m_arr[1000];
int temp;
int ele_ind;
int ele;
  
for(int i = 0; i<1000; i++) {
arr[i] = (rand() % 1000) + 1;
b_arr[i] = arr[i];
m_arr[i] = arr[i];
}

/* binary search */   
for (int i = 0 ; i < 1000 - 1; i++)
{
for (int j = 0 ; j < 1000 - i - 1; j++)
{
if (b_arr[j] > b_arr[j+1])
{
temp = b_arr[j];
b_arr[j] = b_arr[j+1];
b_arr[j+1] = temp;
}
}
}
  
m_sort(m_arr, 0, 1000 - 1);
  
ele_ind = (rand() % 1000) + 1;
ele = arr[ele_ind];
  
printf("The index of the element %d in sorted array is %d",ele,b_search(b_arr, 0, 999, ele));

return 0;
}


Related Solutions

In many programming languages you can generate a random number between 1 and a limiting value...
In many programming languages you can generate a random number between 1 and a limiting value named LIMIT by using a statement similar to randomNumber = random(LIMIT). Create the logic for a guessing game in which the application generates a random number and the player tries to guess it. Display a message indicating whether the player’s guess was correct, too high, or too low. (After you finish Chapter 4, you will be able to modify the application so that the...
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested...
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested for loops over the elements of the matrix to find the number of prime elements , as well as the sum of the prime elements in the matrix. You can use MATLAB's built-in function isprime to check if an element is a prime number. You should have these variable: cnt (counts the number of prime elements) sm (sum of prime elements) In the command...
What are the differences between the programming languages of VHDL and Verilog? Why use one over...
What are the differences between the programming languages of VHDL and Verilog? Why use one over the other?
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive)....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive). Note: the tests call your function 1000 times to verify that no results are <0 or >1000. Rarely, this can score an incorrect function as correct.
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a minimum of 2, and a maximum of 18. Obtain the mean and the histogram of these numbers, and discuss whether they appear uniformly distributed with the desired mean.
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20 and a variance of 4. Obtain the mean, variance, and histogram of these numbers, and discuss whether they appear normally distributed with the desired mean and variance.
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Collect the Data: Use a random number generator to generate 50 values between 0 and 1...
Collect the Data: Use a random number generator to generate 50 values between 0 and 1 (inclusive). Theoretical Distribution In words, X = The theoretical distribution of X is X ~ U(0, 1). In theory, based upon the distribution X ~ U(0, 1), find μ = __________ σ = __________ 1st quartile = __________ 3rd quartile = __________ median = __________ Construct a box plot of the data. Be sure to use a ruler to scale accurately and draw straight...
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
A random number generator returns random floats between 0 and 1, according to a uniform distribution....
A random number generator returns random floats between 0 and 1, according to a uniform distribution. say you let the random number generator draw 100 numbers, the sample mean will be... A.impossible to determine, because the data aren't drawn from a normal distribution. B.normally distributed, with mean 0.5 and standard deviation 0.2887. C.uniformely distributed, between 0 and 1. D.normally distributed, with mean 0.5 and standard deviation 0.02887.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT