In our median-finding algorithm (Section 2.4), a basic primitive is the split operation, which takes as input an array S and a value V and then divides S into three sets: the elements less than V , the elements equal to V , and the elements greater than V . Show how to implement this split operation in place, that is, without allocating new memory.

Short Answer

Expert verified

The split will be done in place and with time bound:οn

Step by step solution

01

Algorithm

Algorithms to performing "split" operations without any need for additional memory:

function splita1,...,n,v

hira=1fori=1ton:ifai<v:swapaiandahirahira=hira+1fori=hiraton:ifaiv:swapaiandahirahira=hira+1

02

Explanation of Algorithm

• “split ” is the function which accepts the array “s1,...,n” and a value “” as the input parameters.

• Initially, the variable “ count” is assigned with the value.

• The function has 2 “for ” loops. In the first “ for” loop,

o Process all the elements in the array and brings the elements that are smaller than the value 2 “ ” to front of the array by swapping.

o Thus, the array is split into sub-array by moving all the smaller elements in the array to front.

• It first checks if the value in the k“th” element of the array is less than the value of “ val”.

• If it is true, it swaps the k“th” element of the array “ s” with the position of the array element which has the value of count.

• Then, the value of the “count” is incremented.

• In the second “for” loop,

o Find the position of the value “ val” and move it next to the sub-array by swapping.

• It first checks if the value in the k“th” element of the array is equal to the value of “val ”.

• If it is true, it swaps the k“th” element of the array “ s” with the position of the array element which has the value of count.

• Then, the value of the “ count” is incremented.

o Here, both the “ for” loops require constant time.

Therefore, it takes the running time of οn.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free