Question

Hi, I am having trouble with the following question: Given an unsorted array with integers, find...

Hi, I am having trouble with the following question:

Given an unsorted array with integers, find the median of it using the Quick Select method we’ve discussed in the class (Hint: We use the quick select to find the kth smallest element in an unsorted array in O(n) time complexity). Note: A median is the middle number of the array after it is sorted. And in this problem, we return the N/2-th number after sorted if there are even numbers in the array. Do not sort the array to find the median!

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

Algorithm

Algorithm kthSmallest(arr[],l, r, k)

// This algorithm returns k'th smallest element in arr[l..r] usingQuickSort based method. ASSUMPTION: ALL ELEMENTS IN ARR[] ARE DISTINCT

{

    // If k is smaller than number of elements in array

    if (k > 0 && k <= r - l + 1)

    {

        // Partition the array around last element and get position of pivot element in sorted array

        pos = partition(arr, l, r);

        // If position is same as k

        if (pos-l = = k-1)

            return arr[pos];

        if (pos-l > k-1) // If position is more, recur for left subarray

            return kthSmallest(arr, l, pos-1, k);

        // Else recur for right subarray

        return kthSmallest(arr, pos+1, r, k-pos+l-1);

    }

    // If k is more than number of elements in array

    return INT_MAX;

}

Algorithm swap( a, b)

{

    temp = a;

    a = b;

    b = temp;

}

Algorithm partition(arr[],l, r)

// Partition process of QuickSort(), considers the last element as pivot and moves all smaller element to left of it and greater elements to right

{

    x = arr[r], i = l;

    for (j = l; j <= r - 1; j++)

    {

        if (arr[j] <= x)

        {

            swap(arr[i], arr[j]);

            i++;

        }

    }

    swap(arr[i], arr[r]);

    return i;

}

Algorithm findMedian()

{

    Input array of size n

//Median is middle element when n is odd and average of middle two elements when n is even.

if (n%2==0)                           

    {

        k=n/2;

         Output median=kthSmallest(arr, 0, n-1, k);

    }

    else

    {

      k=(n-1)/2;

     a=kthSmallest(arr, 0, n-1, k);

      b=kthSmallest(arr, 0, n-1, (k+1));

       Output median = (a+b)/2.

    }   

}

Complexity of this algorithm is O(n).

Add a comment
Know the answer?
Add Answer to:
Hi, I am having trouble with the following question: Given an unsorted array with integers, find...
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
  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • 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...

  • 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,...

  • Please answer in Java for an immediate upvote :) Given the following array of 8 elements,...

    Please answer in Java for an immediate upvote :) Given the following array of 8 elements, trace the merge sort algorithm. Assume the array is to be sorted in ascending order.                        81          16          4        6             34          11          23        67 ANSWER: (Hint 6 – lines): Why don’t we select the median element of all the n-entries in the array to be our pivot point? ANSWER:

  • I am working on the divide/conquer algorithm. I am having a trouble with a print for...

    I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • Hi, I am having trouble answering the following physics question. The electron beam inside a television...

    Hi, I am having trouble answering the following physics question. The electron beam inside a television picture tube is 0.500mm in diameter and carries a current of 53.0 ?A. This electron beam impinges on the inside of the picture tube screen. a) How many electrons strike the screen each second? b) What is the current density in the electron beam? Express your answer with the units A/m^2. c) The electrons move with a velocity of 4.50

  • 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...

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