Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***convert this c++ program into PROLOG*** / get the array after partition. int partition(int arr[], int l, int r, int pivotIndex){ int i = l-1;

***convert this c++ program into PROLOG*** / get the array after partition. int partition(int arr[], int l, int r, int pivotIndex){ int i = l-1; for(int j = l ; j <= r ; j ++ ) { if( arr[j] < pivotIndex ) { i += 1; swap(arr[i],arr[j]); } } return i; } // finding the median of medians of medians of array arr. int median_of_median(int arr[], int l, int right ) { int size = r-l+1; if( size <= 0) return INT_MIN; if( size <= 5 ) { // sort the array and get the median. sort(arr+l,arr+(l+size)); return arr[l + size/2] // get the median } int numberOfMedians = size/5; vector temp; for(int i = 0 ; i < numberOdmedians ; i ++) { temp.push_back(median_of_median(arr,left + 5*i, left + 5*(i+1)-1)); } return median_of_median(arr,0,arr.size()-1) } // find the kth element of the array. int kthLargextElement( int arr[], int left, int right, int k ) { int index = median_of_median(arr,left,right); int pivotIndex = partition(arr,left,right,arr[index]); // find the kth pivot index so that we can easily find the answer. while( k != pivotIndex){ if( k > pivotIndex+1) { pivotIndex = kthLargextElement(arr, pivotIndex + 1, right, k); } else { pivotIndex = kthLargextElement(a, left, pivotIndex, k) } } return arr[pivotIndex+1]; // this kth largest of the element. }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions