Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

Q. Write down an algorithm to sort a given list by making use of Quick sort method. Describe the behaviour of Quick sort when input given to us is already sorted.                                  

Ans.

Algorithm for Quick Sort is written below

QUICK(A, N, BEG, END, LOC)

Here A is an array with N element. Parameter BEG

and END comprises the   boundary value of the sub

list of A to which this method applies. LOC keeps track of the position of the first element A[BEG] of the sublist during the particular procedure. The local varrible LEFT     and  RIGHT will contain the boundary value of the list elements that have not been scanned.

1. [Initialize]   Set LEFT:=BEG, RIGHT;=END and LOC:=BEG.

2. [Scan from left to right]

(a) Repeat while A[LOC] <=A[RIGHT] and LOC!=RIGHT; RIGHT:=RIGHT-1; [End of loop]

(b)If LOC= RIGHT, then Return;

(c)If A[LOC ] > A[RIGHT],then: [Interchange    A[LOC] and A[RIGHT]] TEMP:=  A[LOC]  ,A[LOC] =  A[RIGHT] , A[RIGHT] :=TEMP;

(i) Set LOC =RIGHT

(ii)      Go to step 3

3.[Scan from left to right]

repeat while A[LEFT] <=A[LOC] and  LEFT!= LOC; LEFT := LEFT +1; [End of loop]

(a) If LOC  =LEFT, then Return;

(b) If  A[LEFT]  > A[LOC] ,then

(i) [Interchange A[LEFT]  and A[LOC]] TEMP:=A[LOC],A[LOC]:=A[LEFT] . A[LEFT]:= TEMP

(ii) Set LOC :=LEFT (iii) Go to Step 2; [End of if structure]

(Quicksort) This algorithm sorts an array A with N elements.

1. [Intialize.] TOP := NULL

2. [Push boundary values of A onto stacks when A has 2 or more elements.]

If  N>1, then: TOP+1,LOWER [1]:=1, UPPER [1]: =N

3. Repeat steps 4 to 7 while TOP != NULL.

4. [Pop sublist from stacks.]

Set BEG: =LOWER[TOP], END:=UPPER[TOP], TOP:=TOP-1.

5. Call QUICK (A, N, BEG, END, LOC). [ Push left

sublist onto stacks when it has 2 or more elements.]

If BEG < LOC -1, then:

TOP:= TOP+1, LOWER[TOP] := BEG, UPPER[TOP]= LOC -1.

[End of If structure.]

6. [Push right sublist onto stacks when it has 2

or more elements.]

If  LOC +1< END , then:

TOP := TOP+1, LOWER[TOP] := LOC +1, UPPER[TOP] := END.

[End of If structure .] [End of Step 3 loop.]

7. Exit.

The behaviour of quick sort when the list is sorted is of order O(n2) as this is the worst case for quicksort

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9504396

Have any Question?


Related Questions in Computer Engineering

Consider the car-caravan analogy from section 4 in chapter

Consider the car-caravan analogy from Section 4 in Chapter 1.  In this problem, assume a propagation speed of 120 km/hr and that each toll booth takes 8 seconds to service a car. a) Suppose the caravan of 10 cars begins ...

Question research the internet to obtain information on

Question: Research the Internet to obtain information on Windows Group Policies and the Group Policy Editor. • Review the critical considerations to prepare a procedure guide. • Organize all the steps necessary for imple ...

There is a lot of information about how relevant this type

There is a lot of information about how relevant this type of thick clients is still. What are the advantages/disadvantages of these type of connections?

Excel discussionconsider this using the mouse to point and

Excel discussion Consider this.... Using the mouse to point and click is one way to work on a computer. Often, the same work can be accomplished using just the keyboard, using shortcut keyboard combinations. For example, ...

Question reversing the elements of an array involves

Question : Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. G ...

Question explain the need for designing procedures for

Question: Explain the need for designing procedures for simple tasks such as creating or modifying access controls. Create a procedure guide that provides clear instructions that anyone with a basic technical knowledge b ...

In thenbspworkspaceproject-lognbspdirectory create file

In the ~/workspace/project-log directory, create file named  changelog.txt  with the following content and format: Changelog Version: 1.0 Redirect the output of the ls command to a file named  file-list.txt  in the ~/wor ...

Stock portfolio gui projectgoalyou are to write a gui

Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. For example, you might track the profit or loss on the trades. You might allow for ...

Suppose you insert 125 integer keys into a hash table with

Suppose you insert 125 integer keys into a hash table with an initial capacity of 25 and load factor threshold of 2. THe hash code is the key itself,and the function key mod table_capacity is used to map a key into a tab ...

Question suppose we run dfs on a undirected graph g that

Question : Suppose we run DFS on a (undirected) graph G that has edge weights and is connected (there is a path connecting any two vertices of G). (a) Prove that the DFS tree is a spanning tree of G. (b) Recall that the ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As