Question

In: Computer Science

Suppose you have a group of N numbers and would like to determine the kth largest....

Suppose you have a group of N numbers and would like to determine the kth largest. This is known as the selection problem. There are quite a few “obvious” solutions.

One way to solve this problem would be to read the N numbers into an array, sort the array in decreasing order by some simple algorithm such as bubble sort, and then return the element in position k.

A somewhat better algorithm might be to read the 1st k elements into an array and sort them (in decreasing order). Next, each remaining element is read one by one. As a new element arrives, it is ignored if it is smaller than the kth element in the array. Otherwise, it is placed in its correct spot in the array, bumping one element out of the array. When the algorithm ends, the element in the kth position is returned as the answer.

Write a program to solve the selection problem. Let k=N/2.

Draw a table showing the running time of your program for various values of N. ( use Excel to create a chart of exported runtime data )

IT'S C++ CODING LAB

Solutions

Expert Solution

#include <iostream>

using namespace std;

void swap(int *xp, int *yp);

void bubbleSort(int arr[], int n);

void insertElement(int nums[], int size, int pos, int value);


int main()

{

int size;

cout << endl << "How many numbers do you want to enter? ";

cin >> size;

int original[size];

cout << "Enter the numbers: " << endl;

for(int i = 0; i < size; i++)

{

cin >> original[i];

}

cout << "\nThe original array is: ";

for(int i = 0; i < size; i++)

{

if(i == size - 1)

cout << original[i] << endl;

else

cout << original[i] << ", ";

}

cout << endl;

// user input for k

int k;

cout << "Enter the value of k: ";

cin >> k;

int kthArr[k];

int rem[size - k];

// copy all the elements upto k from original array to the kthArr

for(int i = 0; i < k; i++)

{

kthArr[i] = original[i];

}

// copy the remaining elements from original array to rem array

for(int i = 0; i < size - k; i++)

{

rem[i] = original[i + k];

}

// bubble sort the kthArray in decreasing order

bubbleSort(kthArr, k);

int kArrLen = sizeof(kthArr) / sizeof(kthArr[0]);

int remArrLen = sizeof(rem) / sizeof(rem[0]);

cout << endl;

// perform the operation

int counter = 0;

while(counter < remArrLen)

{

// get a new element from the rem array

int testElement = rem[counter];

// if the chosen element from the rem array is greater than the last element of the kthArr array

if(testElement > kthArr[kArrLen - 1])

{

// loop over the whole kthArr

for(int i = 0; i < kArrLen; i++)

{

// if the chosen element is greater than the ith element of the kthArr array, insert the chosen element at the ith position in the kthArr and bumb out the last element from the kthArr

if(testElement > kthArr[i])

{

insertElement(kthArr, kArrLen, i, testElement);

break;

}

}

}

counter++;

}

cout << "The largest element for (k = " << k << ") is: " << kthArr[k - 1] << endl;

return 0;

}

// this method swaps 2 integers

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

// the bubble sort method

void bubbleSort(int arr[], int n)

{

int i, j;

for (i = 0; i < n-1; i++)

{

for (j = 0; j < n-i-1; j++)

{

if (arr[j] < arr[j+1])

swap(&arr[j], &arr[j+1]);

}

}

}

// this method inserts the value at the given position of the specified array of a given size

void insertElement(int nums[], int size, int pos, int value)

{

for(int i = size; i > pos; i--)

  {

    nums[i] = nums[i - 1];

  }

nums[pos] = value;

}

******************************************************************* SCREENSHOT ********************************************************


Related Solutions

You would like to determine who in a group of 100 students carries antibodies for a...
You would like to determine who in a group of 100 students carries antibodies for a certain virus. You can perform blood tests on each student individually, which would require 100 tests. Instead, you can partition the students into 10 groups of 10. Combine the blood samples of the 10 students in each group, and analyze the combined sample. If none of the 10 students in that group carries the antibodies, the test will show negative, while if one or...
Senario: You are watching a group of children run relay races. You would like to determine...
Senario: You are watching a group of children run relay races. You would like to determine the speed of that the children run. You select 30 children and time them in their relay races. e) What is the parameter for the study? f. What is the statistic for the study? g. Is the study an experiment or an observational study? h. For the study, should you use a proportion or an average?
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
You have n numbers and you want to find a number x (out of the n)...
You have n numbers and you want to find a number x (out of the n) such that x is larger than the median. You can create an algorithim that takes time O(nlogn): sort the n numbers and then report any number that is larger than the element in position n2 of the sorted array. You can also create an algo in O(n) time, by finding the median in linear time and then doing a linear scan to find a...
Suppose that a consumer advocacy group would like to conduct a survey to find the proportion...
Suppose that a consumer advocacy group would like to conduct a survey to find the proportion p of smartphone users that were happy with their smartphone. The advocacy group took a random sample of 1,000 smartphone users and found that 400 were happy with their smartphone. A 90% confidence interval for p is closest to: a. (0.396, 0.404) b. (0.375, 0.425) c. (0.387, 0.413) d. (0.390, 0.410)
Suppose that a consumer advocacy group would like to conduct a survey to find the proportion...
Suppose that a consumer advocacy group would like to conduct a survey to find the proportion p of smartphone users that were happy with their smartphone. The advocacy group took a random sample of 1,000 smartphone users and found that 400 were happy with their smartphone. A 90% confidence interval for p is closest to:
Suppose a consumer advocacy group would like to conduct a survey to find the proportion of...
Suppose a consumer advocacy group would like to conduct a survey to find the proportion of consumers who bought the newest generation of an MP3 player were happy with their purchase. Their survey asked consumers if they were happy or unhappy with their purchase. The responses indicated 28 out of 70 customers reported being unhappy with their purchase. Compute a 90% confidence interval for the population proportion of consumers who are happy with their purchase. (.2492, .5508) (.4852, .7148) (.5037,...
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
A researcher would like to determine if meditation training will affect anxiety-related distress among a group...
A researcher would like to determine if meditation training will affect anxiety-related distress among a group of participants. For a week prior to training, each participant records the number of times they feel anxious. Participants then receive meditation training and for the week following training the number of times they feel anxious is again measured. The data are as follows: Before      After 8             5 7             2 5             4 5             2 8             3 4             2 6             4 6             3 a. Compute...
Suppose you have nanometer-sized features in your sample, and would like to resolve the shape as...
Suppose you have nanometer-sized features in your sample, and would like to resolve the shape as good as you can. Which setting(s) of a Köhler microscope will give you the highest chance of resolving these features and their shape?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT