Question

7) Here is the code for the partition function (used by Quick Sort). Explain the purpose of each line of code. int partition

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

//7)
int partition(int* vals, int low, int high)
{
   int lowpos =low;//assums leftmost element as pivot
   low++;//now excluding pivot
  
   while(low<=high)//loop runs upto low is less than or equal to high
   {
       while(low<=high && vals[low]<=vals[lowpos])//loop runs upto low is less than or equal to high and value at index low is less than or equal to pivot
       low++;//then increasing low
       while(high>=low &&vals[high]>vals[lowpos])//loop runs upto high is greater than or equal to low and value at index high is greater than pivot
       high--;//then decreasing high
       if(low<high)//if low is less than high
       swap(&vals[low],&vals[high]);//then swapping values at indexes low and high
          
   }
   //finally updating pivot position
   swap(&vals[lowpos],&vals[high]);//swapping values at indexes lowpos and high
   return high;//returning pivot index//or parition index
}

Add a comment
Know the answer?
Add Answer to:
7) Here is the code for the partition function (used by Quick Sort). Explain the purpose...
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
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