Question

The quicksort is an example of a divide and conquer algorithm in that it divides the...

The quicksort is an example of a divide and conquer algorithm in that it divides the sorting problem into smaller problems by dividing the list of items to be sorted into smaller subsets. The pivot is the mechanism that is used to segment the list. Describe how the quicksort works including a discussion of the pivot, how it is selected, and why the pivot is important to the quicksort.

Homework Answers

Answer #1

Hello Student,

Working of Quicksort Algorithm:

In quicksort algorithm we first consider an element of the given unsorted array as a pivot element whether it be the first, last or the middle element to place elements smaller than the pivot to left and greater to right side. Also, we point first index element as low index and last index element as high index (depending on pivot’s location). Then simply moving the low index to right if the pointed element is lesser compared to pivot and moving the high index to left if pointed element is greater compared to pivot. But if low index points to greater element and high index points to smaller element than pivot then swap the low and high index pointed elements. At last if low index becomes equal to high index then swap the element with pivot.

In quicksort, the array is divided into lower side and greater side of pivot i.e. 2 parts and the same above method is applied to both part and at last joined all together with the pivot to give the whole sorted array.

Example-

L= low index, H= high index, P= pivot.

Here we selected last 48(last element) as pivot, first 48(first element) as low index and 26(2nd last element) as high index.

48

37

64

96

75

12

26

48

L

H

P

Swapped low and high element as low was > and high was < than pivot.

26

37

64

96

75

12

48

48

L

H

P

Low index moved to right until greater element than pivot was pointed.

26

37

64

96

75

12

48

48

L

H

P

Swapped low and high element as low was > and high was < than pivot.

26

37

48

96

75

12

64

48

L

H

P

High index moved to left until lesser element than pivot was pointed.

26

37

48

96

75

12

64

48

L

H

P

Swapped low and high element as low was > and high was < than pivot.

26

37

12

96

75

48

64

48

L

H

P

Low index moved to right until greater element than pivot was pointed.

26

37

12

96

75

48

64

48

L

H

P

Swapped low and high element as low was > and high was < than pivot.

26

37

12

48

75

96

64

48

L

H

P

High index moved to left until lesser element than pivot was pointed.

26

37

12

48

75

96

64

48

L

H

P

Here we get Low = High, thus swapped it with pivot.

26

37

12

48

75

96

64

48

L=H

P

Divided the array in left side and right side according to pivot.

quick sorting the Left side.

26

37

12

L

H

P

  

High index moved to left until lesser element than pivot was pointed.

Here we get Low = High, thus swapped it with pivot.

26

37

12

L=H

P

Again,

12

37

26

L

H

P

  

Low index moved to right until greater element than pivot was pointed. Here we get Low = High, thus swapped it with pivot.

12

37

26

L=H

P

So, left side of pivot is sorted.

12

26

37

     

Quick sorting the Right side.

75

96

64

48

L

H

P

Swapped low and high element as low was > and high was < than pivot.

64

96

75

48

L

H

P

High index moved to left until lesser element than pivot was pointed. Here we get Low = High, thus swapped it with pivot.

64

96

75

48

L=H

P

Again,

48

96

75

64

L

H

P

Low index moved to right until greater element than pivot was pointed.

48

96

75

64

L

H

P

High index moved to left until lesser element than pivot was pointed. Here we get Low = High, thus swapped it with pivot.

48

96

75

64

L=H

P

So, right side of pivot is sorted.

48

64

75

96

Thus, After re-joining the left side, pivot, right side we get the sorted array.

12

26

37

48

48

64

75

96

---------------------------------------------------------------------------------------------------------------------------------------------------------

THANK YOU!

LIKE THE ANSWER IF IT HELPED YOU

Know the answer?
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
Similar Questions
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
SOC 3332 APPLIED SOCIAL PSYCH        GHA #1 Week 1     Name: ____________________________________    Date: ___________________     Each answer for...
SOC 3332 APPLIED SOCIAL PSYCH        GHA #1 Week 1     Name: ____________________________________    Date: ___________________     Each answer for Questions #1-4 will be evaluated using the following Grading Rubric http://www.rcampus.com/rubricshowc.cfm?sp=yes&code=E33X44 The grading criteria are: Content; Organization; and Writing Mechanics. Each is criteria is worth four points for a total of 12 points per question. Maximum score is 48 points. #1: Pick ONE of the compare/contrast questions and answer in 1-2 detailed paragraphs with supporting answers and citations. CLO1: Analyze: Students will draw connections...
Introduction Purpose Your goal is to create a design for a software interface. You will experience...
Introduction Purpose Your goal is to create a design for a software interface. You will experience the scope of the design process from brainstorming ideas and gathering information about users’ needs to storyboarding, prototyping, and finally, testing and refining your product. As you work on the software interface, you will demonstrate your ability to apply fundamental Human-Computer Interaction principles to interface analysis, design, and implementation. You will be responsible for delivering project components to your professor at several points during...
Pandora is the Internet’s most successful subscription radio service. As of June 2013, it had over...
Pandora is the Internet’s most successful subscription radio service. As of June 2013, it had over 200 million registered users (140 million of which access the service via a mobile device) and over 70 million active listeners. Pandora now accounts for more than 70% of all Internet radio listening hours and a 7% share of total U.S. radio listening (both traditional and Internet). At Pandora, users select a genre of music based on a favorite musician, and a computer algorithm...
Analysis: This section should include the issue register as a bare minimum, but may include also...
Analysis: This section should include the issue register as a bare minimum, but may include also why-why diagrams, a Pareto chart, a waste table and/or value-added analysis table. Flow analysis or simulation of this case study might be possible but might require making a lot of assumptions given the provided data. The first part of the project: Introduction    Walmart has continued to retain the top position on the Fortune 500 list for a consecutive fifth year. The brand has...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...