Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose we are given an array of numbers A [0], A [1], . . . , A [ n - 1]. You m

ID: 3846647 • Letter: S

Question

Suppose we are given an array of numbers A[0], A[1], . . . , A[n - 1]. You may assume that n is a multiple of 6. We wish to find a number x that simultaneously satisfies two properties: First, x should be in the middle two-thirds of the array (in other words, x = A[i ] for some n/6 <= i < 5n/6). Second, after sorting the array, x should still be in the middle two-thirds.

a. Prove that such an x exists. (Hint: how many values are not in the middle two-thirds of the original array? How many are not in the middle two-thirds of the sorted array?)
b. Describe a linear time algorithm for finding such an x. (Note, if your solution calls one of the algorithms described in the lectures, you should just refer to it by name, e.g. “quicksort”, rather than explaining how it works.)

Explanation / Answer

The below code is the answer for A.

It uses bubble sort algorithm to solve.

#include <stdio.h>

int* bubblesort(int* array,int l){
int c,d,n,swap;
n=l;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
return array;
}

int main()
{
int i,j, x=5, n=10;
int a[10]={10,9,8,7,6,5,4,3,2,1};
int *b;
scanf("%d",&x);
b=bubblesort(a,10);
for(i=n/6;i<5*n/6;i++){
if(a[i]==x){
for(j=n/6;j<5*n/6;j++){
if(b[j]==x){
printf("Found");
return 0;
}
}
}
}
printf("Not Found");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote