Consider the following version of insertion sort:
for i ← 1 to n − 1 do
j ← i − 1
while j ≥ 0 and A[j] > A[j + 1] do
swap(A[j], A[j + 1])
j ← j − 1
What is its time efficiency? How does it compare to the version given in the text?
Apply insertion sort to sort the list E, X, A, M, P, L, E in alphabetical order.
Consider the following algorithm to check connectivity of a graph defined by its adjacency matrix.
Algorithm Connected(A[0...n - 1, 0...n - 1])
// Input: Adjacency matrix A[0...n - 1, 0...n - 1] of an undirected graph G
// Output: 1 (true) if G is connected and 0 (false) if it is not
if n = 1 return 1 // one-vertex graph is connected by definition
else
if not Connected(A[0...n - 2, 0...n - 2]) return 0
else for j ← 0 to n - 2 do
if A[n - 1, j] return 1
return 0
Does this algorithm work correctly for every undirected graph with n > 0 vertices? If you answer "yes," indicate the algorithm’s efficiency class in the worst case; if you answer "no," explain why.
a. What sentinel should be put before the first element of an array being
sorted in order to avoid checking the in-bound condition j ≥ 0 on each
iteration of the inner loop of insertion sort?
b. Will the version with the sentinel be in the same efficiency class as
the original version?
a. Prove that the topological sorting problem has a solution for a digraph
if and only if it is a dag.
b. For a digraph with n vertices, what is the largest number of distinct
solutions the topological sorting problem can have?
a. What is the time efficiency of the DFS-based algorithm for topological
sorting?
b. How can one modify the DFS-based algorithm to avoid reversing the
vertex ordering generated by DFS?
to generate a solution
a solution
- Given below is the Randomized Quick Sort Algorithm, where p and r represent lower and upper bounds of array A respectively. RANDOMIZED-PARTITION (A, p, r) 1 i = RANDOM(p,r) 2 exchange A[r] with A[i] 3 return PARTITION (A, p, r) RANDOMIZED-QUICKSORT if parrow_forwardPlese Explain Q 5. Thank you.arrow_forwardUsing Java, 1. Implement external sort: for sort phase use normal sort, for merge phase use two way merge to merge n sorted files (merge2way(n)), for array sort use heapsort. Also write merge(f1, f2, f3) to merge two sorted files f1 and f2 into f3.. Write mergenway(n) method and print execution time of both merges for initial input file over 10MB data. A sample input is as follow:Note:Fist input is max array size for sort 10 84 82 52 80 96 85 75 75 82 87 92 89 57 94 93 92 63 99 87 72 73 56 74 50 84 62 72 55 86 75 74 100 83 60 53 68 89 67 66 65 72 94 73 54 98 96 85 75 75 82 87 92 89arrow_forwardIn this problem, consider a non-standard sorting algorithm called the Slow Sort. Given anarray A[1 : n] of n integers, the algorithm is as follows: Slow-Sort(A[1 : n]):1. If n < 100, run merge sort (or selection sort or insertion sort) on A.2. Otherwise, run Slow-Sort(A[1 : n −1]), Slow-Sort(A[2 : n]), and Slow-Sort(A[1 : n −1]) again. Question: Prove the correctness of Slow-Sort.arrow_forwardA given implementation of bubble-sort takes on average 1 second to sort an array of 1000 elements. How many seconds do you expect sorting an array of 8000 elements will take? A given implementation of quicksort takes on average 0.1 seconds to sort an array of 1000 elements. How many seconds do you expect sorting an array of 8000 elements will take?arrow_forwardIn Python: Let n be a (large) positive integer of your choice. We want to know which of the two Quicksort algorithms methods (he Lomuto method and the Hoare method) is superior when sorting arrays containing n elements. To answer this question, you will run Quicksort on 10000 random permutations of [1,2,3, . . . , n], on each of your two Quicksort algorithms.arrow_forwardarrow_back_iosarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education