Question

In: Computer Science

You have been given the following set of keys 12, 45, 1, 9, 67, 230, 78,...

You have been given the following set of keys

12, 45, 1, 9, 67, 230, 78, 64, 450, 436, 123, 6, 12, 90

  1. Use insertion to sort the elements in ascending and descending order ( show the steps)

    [5 Marks]

  1. Using heaps, show how the keys can be sorted in ascending and descending order ( show the steps)

     [5 Marks]

  1. Using Big O, derive the space and time complexity of insertion sort and heap sort algorithms above

Solutions

Expert Solution

Ans-Insertion sort -

insertion sort is a simple sorting algorithm. this algorithm used insert each element onto its proper place in the sorted array and less efficient than other sort algorithm.

complexity-o(n^2)

Algorithm-

Insertion-sort(A)

{

for j=2 to A.length

key=A[j]

//insert A[j] into sorted sequence A[1.....j-1]

i=j-1

while(i>0 and A[j]>key)

A[i+1]=A[i]

i=i-1

A[i+1]=key

}

descending order-12,45,1,9,67,230,78,64,450,436,123,6,12,90

1,12,45,9,67,230,78,64,450,436,123,6,12,90 (inserted 1)

1,9,12,45,67,230,78,64,450,436,123,6,12,90 ( inserted 9)

1,9,12,45,67,78,230,64,450,436,123,6,12,90 ( inserted 67)

1,9,12,45,64,67,78,230,450,436,123,6,12,90 (inserted 78)

1,9,12,45,64,67,78,230,436,450,123,6,12,90 (" 123)

1,9,12,45,64,67,78,123,230,436,450,6,12,90(" 230)

1,6,9,12,45,64,67,78,123,230,436,450,12,90( "430)

1,6,9,12,12,45,,64,67,78,123,230,436,450,90

1,6,9,12,12,45,64,67,78,90,123,230,436,450(sorted array)

Ascending order-1,6,9,12,12,64,67,78,90,123,436,450

Heap sort- is a comparison based sorting based on binary heap structure it is similar to selection sort.

Algorithm-

1. build a max heap from the input data

2.At this point the largest item is stored at root of heap.

3.Replace it with the last item of the heap followed by reducing the size of heap by 1 finally ,heapify the root of the tree.

4. repeat step 2 while size of heap is greater than 1.

Insertion sort space complexity=o(1)

Heap sort space complexity=o(1)

complexity-o(log n)

build heap complexity=o(nlog n)  


Related Solutions

Given the data set below 25 52 67 23 78 89 57 90 32 77 45...
Given the data set below 25 52 67 23 78 89 57 90 32 77 45 48 62 54 94 69 46 79 40 33 21 57 84 54 23 34 68 63 61 76 87 78 39 50 70 60 32 65 73 45 28 82 66 79 71 80 46 66 24 90 A)What is the probability of an impossible even? B) What is the probability of a certain event? C) find the approximate mean using the Frequency...
Assume the following list of keys: 78, 40, 16, 82, 64, 67, 57, 40, 37, 47,...
Assume the following list of keys: 78, 40, 16, 82, 64, 67, 57, 40, 37, 47, 72, 14, 17, 27, 55 This list is to be sorted using the quick sort algorithm as discussed in this chapter. Use pivot as the median of the first, last, and middle elements of the list. What is the pivot? Give the resulting list after one call to the partition function.
Find the lower fence for the following data set: 45, 34, 27, 78, 42, 64, 78...
Find the lower fence for the following data set: 45, 34, 27, 78, 42, 64, 78 Find the upper fence for the following data set: 3, 19, 47, 18, 23, 34, 45, 27, 10, 7
The actual values for 12 periods (shown in order) are: (1) 45 (2) 52 (3) 48 (4) 59 (5) 55 (6) 54 (7) 64 (8) 59 (9) 72 (10) 66 (11) 67 (12) 78
Question 1 contains the actual values for 12 periods (listed in order, 1-12). In Excel, create forecasts for periods 6-13 using each of the following methods: 5 period simple moving average; 4 period weighted moving average (0.63, 0.26, 0.08, 0.03); exponential smoothing (alpha = 0.23 and the forecast for period 5 = 53); linear regression with the equation based on all 12 periods; and quadratic regression with the equation based on all 12 periods.  Round all numerical answers to two...
As a researcher, you have been given a set of standards to use in analysis of...
As a researcher, you have been given a set of standards to use in analysis of cellobiase. You have the standard numbers and absorbance, graph this standard curve Standard Number Absorbance Amt of p-nitrophenol Produced S1 0 0 S2 0.221 12.5 S3 0.454 25 S4 0.901 50 S5 1.783 100
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black...
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black trees that can be constructed with those keys. Draw the tree for each possible key-insertion order, showing the transformations involved at each step.
5. Given the data set A = {9, 5, 16, 4, 32, 8, 12, 9, 11,...
5. Given the data set A = {9, 5, 16, 4, 32, 8, 12, 9, 11, 15, 5, 9, 18, 10}, which is the data of an entire population of subjects: a. Calculate the arithmetic mean (5 pts) b. Find the median (5 pts) c. Find the mode (5 pts) d. Calculate the range (5 pts) e. Calculate the interquartile range (5 pts) f. Calculate the mean deviation (5 pts) g. Calculate the variance (5 pts) h. Calculate the standard...
1. Given the following information, you have been requested byyour supervisor to submit the cost...
1. Given the following information, you have been requested by your supervisor to submit the cost of ending inventory under LIFO periodic. At year-end 850 units remained in inventory. January 1 inventory: 2,500 at $2.95 April 9: 4,000 at $4.00June 15: 1,000 at $6.50August 10: 500 at $7.00December 9: 200 at $8.50 2. Given the following information, you have been requested by your supervisor to submit the cost of ending inventory under Weighted-Average periodic. At year-end 850 units remained in...
You have been given the following information for Spector Inc.: Retained earnings as at January 1,...
You have been given the following information for Spector Inc.: Retained earnings as at January 1, 20X7, were $50,000. The statement of comprehensive income for the year ended December 31, 20X7, reported comprehensive income of $12,000 comprising net income of $5,000 and other comprehensive income of $7,000. Spector paid out $30,000 in cash dividends during the 20X7 fiscal year. Its dividend payable account increased by $3,000. What is the balance in Spector Inc.’s retained earnings account as at December 31,...
You have been given the job of evaluating the following merger candidate. You have collected the...
You have been given the job of evaluating the following merger candidate. You have collected the following cash flow for the acquisition candidate for the proposed merger (in millions): Year                                                                1                              2                               3                               4                               5__ Cash flows now for canidate 90                            85                               205                            165                            180 Additional cash flows with merger 60                            90                               100                            225                            250 Total cash flows with synergy 150                            175                            305                            390                            430 Risk free rate of return                                                                                                                 3.0% Beta for this project (the company after merging)                                                        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT