Question

In: Computer Science

Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what...

Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what it does, how it does. Include code and show output.

Solutions

Expert Solution

#As language not specified, using Python, comment if anything else required

#randomisedPartition using partition process of QuickSort()
def randomisedPartition(array, l, r):
   x = array[r]
   i = l
   for j in range(l, r):
       if array[j] <= x:
           array[i], array[j] = array[j], array[i]
           i += 1
   array[i], array[r] = array[r], array[i]
   return i

def randomizedSelect(array, l, r, k):
   #Randomised partition for searching
   index = randomisedPartition(array, l, r)
  
   # if index is the required position then return
   if (index - l == k - 1):
   return array[index]

   # If position is greater then recursion for left subarray
   if (index - l > k - 1):
   return randomizedSelect(array, l, index - 1, k)
   # Else recursion for right subarray
   return randomizedSelect(array, index + 1, r, k - index + l - 1)

array = [ 7, 44, 76, 2, 11, 9, 3, 23, 34, 32, 15, 18 ]
size = len(array)
k = 3
print(k,"rd smallest element is ",randomizedSelect(array, 0, size - 1, k))
k = 9
print(k,"th smallest element is ",randomizedSelect(array, 0, size - 1, k))

Comment down for queries


Related Solutions

Write a program, using any language you want, and any sorting algorithm discussed in this unit...
Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following : 243, 1, 4, 6, 234, 33, 674, 32, 3333 Note: Can you write it in quick sort
Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8,...
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8, 7, 9,16, 14, 3, 2, 4, 1]
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT