Question

I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

I need help

In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method:

/**
 * Returns the number that would be at index {@code index} in a sorted version
 * of the array.
 * 
 * @param numbers array with pairwise distinct numbers.
 * @param index   the index of the number we're looking for in a sorted version
 *                of {@code numbers}.
 * @return the number at the given index.
 * @throws IllegalArgumentException if {@code index} is not valid for the array.
 */
public static int select(int[] numbers, int index) {
        return -1;
}

Form 5-groups as indicated in the script. For inputs with a maximum of 5 elements, you can end the recursion of the algorithm by sorting the input and finding the desired element in this way. Do not use predefined sorting algorithms in this task, but implement everything yourself! The algorithm can be implemented by working each recursive call on a new array. However, creating the new arrays can take a long time. Instead, the algorithm can also be built completely in-place. But this is not a requirement to complete the task successfully.

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

Algorithm:

Here we have elements in unsorted array .

Lets size of array is N. Its indexing starting from 0 to N-1.

If median is odd. median will be at integral part of N/2 position.

If median is even , median will be average of value at N/2 and (N+1)/2.

So we will calculate the value for some index i that will be pointed to sorted array.

FindElement(array,i)

{

    value=array(i)

    start from index1 =0 and index1 =N-1;

   while(index1<index2)

   {

         do{

                     index1++;

}while(array(index1)<value);

   do{

                     index2--;

}while(array(index2)>=value);

          if(index1<index2)

         {

               SwapElements(array1(index),array1(index2);

         }

    }

SwapElements(array(index2),array(i))

Return array(i)

}

CalculateMedian{

num1,num2

Input N

Input all array elemnts in unsorted manner in array

if N is Odd . Median = FindElement(array,N/2)

if N is Even . Median =(float) (FindElement(array , N/2) + FindElement(array , (N+1)/2) )/2;

print median;

}

FindMedian is Based on calculating element in sorted manner.

Add a comment
Know the answer?
Add Answer to:
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...
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
  • Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

    Need help with my Java Hw: Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • I have this Java program I need Help plz package ads.set2.select; public class Selector { public...

    I have this Java program I need Help plz package ads.set2.select; public class Selector { public static void main(String[] args) {   int index = 3;   int[] numbers = { 23, 4, 33, 11, 99,24 };   int results = select(numbers, index);   System.out.println("Element at index: " + index + " in sorted array is " + results);   // error case   select(numbers, 9); } /** * Returns the number that would be at index {@code index} in a sorted version * of the...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Please Help me! Recursive approximate median Consider an array of integers. You wish to find an...

    Please Help me! Recursive approximate median Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

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
ADVERTISEMENT