Question

In: Computer Science

Steps and formula (if applied) are mandatory for all the questions. 3.   a) Sort 99, 54, 64,...

Steps and formula (if applied) are mandatory for all the questions.

3.   a) Sort 99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write the algorithm.

                                                                 

b) Sort 69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38 using Shell sort. Write the algorithm.

Solutions

Expert Solution

a) Sort 99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write the algorithm.

Quick Sort: In data structures, Quick Sort is a divide and conquer algorithm. In this sort, one element has to be picked i.e: whether it is the fist element, last element or random as a pivot, and that pivot partitions the given array.

Algorithm:
Choose one element as a pivot.
Partition the array such that
All pivot element should be in the correct position in the soring list.
All left side elements of the pivot are less than equal to the pivot.
All the right side elements of the pivot are greater than the pivot.
Sort the subarray to the left side of the pivot element and subarray to the right side of pivot recursively.


Given list of elements in an array:
99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89

1) Chose the first element as ivot.

99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89

2) Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89

Here there is no element greater than the pivot. So swap with pivot.

89, 54, 64, 23, 12, 09, 45, 32, 19, 65, 99

3) At this point, left side array is less than the pivot. So again sort the left side subarray 89, 54, 64, 23, 12, 09, 45, 32, 19, 65 using 89 as the pivot

89, 54, 64, 23, 12, 09, 45, 32, 19, 65, 99

4) Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

89, 54, 64, 23, 12, 09, 45, 32, 19, 65, 99

Here there is no element greater than the pivot. So swap with pivot.

65, 64, 23, 12, 09, 45, 32, 19, 89, 99

5) At this point, the left side array is less than the pivot, right side elements are greater than the pivot(89). So again sort the left side subarray 65, 64, 23, 12, 09, 45, 32, 19 using 65 as the pivot

65, 64, 23, 12, 09, 45, 32, 19, 89, 99

6) Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

65, 64, 23, 12, 09, 45, 32, 19, 89, 99

Here there is no element greater than the pivot. So swap with pivot.

19, 64, 23, 12, 09, 45, 32, 65, 89, 99

7) At this point, the left side array is less than the pivot, right side elements are greater than the pivot(65). So again sort the left side subarray 19, 64, 23, 12, 09, 45, 32 using 19 as the pivot

19, 64, 23, 12, 09, 45, 32, 65, 89, 99

8) Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

19, 64, 23, 12, 09, 45, 32, 65, 89, 99

Swap them:

19, 09, 23, 12, 64, 45, 32, 65, 89, 99

9)  Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

19, 09, 23, 12, 64, 45, 32, 65, 89, 99

10) Search from the left side that is greater than the pivot and search from the right side that is less than the pivot.

19, 09, 23, 12, 64, 45, 32, 65, 89, 99

Swap them:

19, 09,12, 23, 64, 45, 32, 65, 89, 99

11) Here you can see two sets of array which are less than ivot and grater than pivot"

19,09,12 23,64,32,65,89,99

12) Apply the quicksort algorithm on two sets:

19,09,12 23,64,32,65,89,99

12,09,19 23,64,32,65,89,99

09,12,19 23, 32,64,65,89,99

b) Sort 69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38 using Shell sort. Write the algorithm.

The Shell Sort: In data structures, Shell sort is nothing but a dimenshing increment sort. It is same as insertion sort but the difference is by breaking list into number of smller sublists. We seperate these sublists usig the key by assuming to the shell sort.

b) Sort

69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38

Let us assume h as 7 such that each list contains 7 elements and compare two sublists as like we do in insertion sort.

69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38

Compare {69,32} {74,19} {64,65} {23,88} {12,33} {09,60} {45,38} and swap accordingly.

32,19,64,23,12,09,38,69,74,65,88,33,60,45


Now, Let us assume h as 2 such that each list contains 2 elements and compare two sublists as like we do in insertion sort.

32,19,64,23,12,09,38,69,74,65,88,33,60,45

32,19, 64,23, 12,09, 38,69, 74,65 88,33, 60,45

Sort accordinlgly:

19,32 23,64 09,12 38,69 74,65 33,88, 45,60

19,23 32,64 09,12 38,69 65,74 33,45 60,88

09,12 19,23 32,64 38,69 65,74 33,45 60,88

As we can see, left side subset is sorted. Let us apply shell sort on remaining elements.

09,12,19,23 32,64 33,45 38,69 60,88 65,74


Now, Let us assume h as 1 such that each list contains 1 elements and compare two sublists as like we do in insertion sort.

09,12,19,23 32, 64 33, 45 38, 69 60, 88 65, 74

09,12,19,23 32, 33, 64, 38, 45, 60, 69, 65, 88, 74

09,12,19,23 32, 33, 38, 45, 60, 64, 69, 65, 74, 88

09,12,19,23 32, 33, 38, 45, 60, 64, 65, 69, 74, 88


Related Solutions

Steps and formula (if applied) are mandatory for all the questions. 1.   a) Sort the keys 66,...
Steps and formula (if applied) are mandatory for all the questions. 1.   a) Sort the keys 66, 99, 23, 12, 98, 09, 45, 29, 31, 20 in ascending order using the Heap sort. Also write the algorithm.                                                                                                                                                           b) Sort the keys 20, 31, 29, 45, 09, 98, 12, 23, 99, 66 in descending order using the Heap sort. Also write the algorithm. 2.   Construct a Huffman coding tree using the given Letters and frequencies below. Also write the algorithm. F:...
Provide all steps b) Sort the keys 20, 31, 29, 45, 09, 98, 12, 23, 99,...
Provide all steps b) Sort the keys 20, 31, 29, 45, 09, 98, 12, 23, 99, 66 in descending order using the Heap sort. Also write the algorithm. a) Sort 99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write the algorithm.                                                                    b) Sort 69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38 using Shell sort. Write the algorithm.
Answer all questions 1. Explain the “Law of Equi-marginal utility” as applied in consumer theory (3...
Answer all questions 1. Explain the “Law of Equi-marginal utility” as applied in consumer theory 2. Compare the equilibrium of a firm under perfect competition and monopoly 3. Suppose that the total cost function of a firm operating in the short run is given by TC = Q2+ 24Q+6 Where TC=Total Cost Q= Quantities produced Required: i) The ATC function ii) The marginal cost function iii) The average variable cost function iv) Calculate the average fixed cost where Q= 8...
Answer all questions 1. Explain the “Law of Equi-marginal utility” as applied in consumer theory (3...
Answer all questions 1. Explain the “Law of Equi-marginal utility” as applied in consumer theory 2. Compare the equilibrium of a firm under perfect competition and monopoly 3. Suppose that the total cost function of a firm operating in the short run is given by TC = Q2+ 24Q+6 Where TC=Total Cost Q= Quantities produced Required: i) The ATC function ii) The marginal cost function iii) The average variable cost function iv) Calculate the average fixed cost where Q= 8...
Please show all formula and steps for full credit ------------------------------------------------------------------ Probability |   Rate of Return |              ...
Please show all formula and steps for full credit ------------------------------------------------------------------ Probability |   Rate of Return |                                       You have $10,000 and want to invest $2,000                                                                                                        in Stock A, $7,000 in Stock B and $1,000 in                                 |       A           |     B               |        C       |        Stock C. ------------------------------------------------------------------ P1= 0.5      | R1A=10% |   R1B=14% | R1C=7% | P2=0.5      | R2A=15% |   R2B=22% | R2C=33%| Calculate Expected Return of A, Calculate Variance of A, Calculate Standard Deviation of A, Calculate...
Business Mathematics answers the questions show the steps based on a formula. Use formulas to find...
Business Mathematics answers the questions show the steps based on a formula. Use formulas to find the present value for each of the following: Future Value Rate Number of Years Compounded Present Value 1 $1 10% 5 Annually ? 2 $1 12% 8 Semi-annually ? 3 $1 6% 10 Quarterly ? 4 $1 12% 2 Monthly ? Ahmed borrowed AED30,000 for office furniture. The loan was for six months at an annual interest rate of 8%. What are Ahmed’s interest...
PLEASE SHOW ALL THE FORMULA IN AN EXCEL FORMAT AND ANSWER ALL QUESTIONS All problems must...
PLEASE SHOW ALL THE FORMULA IN AN EXCEL FORMAT AND ANSWER ALL QUESTIONS All problems must be solved using Excel formulas to receive credit. Question Set 1. You are in charge of quality control for computer monitors at Dell. You have data on twenty-five batches of monitors, tracking five types of defects: brightness, color, contrast, dead pixels, and stuck pixels. These data are given in the table below. 1. For each defect type, find the average number of defects per...
3. Select all of the following statements that are true of tRNAs A)64 different tRNAs are...
3. Select all of the following statements that are true of tRNAs A)64 different tRNAs are used in each organism B)for a tRNA to function correctly, it's anticodon must bind correctly to both mRNA and to an aminoacyl tRNA synthetase. C)most organisms do not use all of the available tRNAs; this differs between species D)the nucleotides of a tRNA do not form hydrogen bonds
Can the Greek Heroic Code be applied to any or all of these 3 Roman figures...
Can the Greek Heroic Code be applied to any or all of these 3 Roman figures (Aeneas and Romulus and Remus)? Explain. If not, then describe at least 2 elements of what might be called the "Roman Heroic Code".
PLEASE ANSWER ALL QUESTIONS PLEASE. 1A.) What is the forward price (formula) for an asset that...
PLEASE ANSWER ALL QUESTIONS PLEASE. 1A.) What is the forward price (formula) for an asset that provides no income using the parameters of S0 , risk-free rate r (annual rate with continuous compounding), and time to maturity of T? Using this formula, if the spot market price of gold is given as 1715 per ounce, the risk-free rate is 1% per annum with continuous compounding, and two years to maturity, what is the fair price for a two-year forward contract?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT