recategorized by
862 views
2 votes
2 votes

Let $A\left [ i \right ] : i=0, 1, 2, \dots , n-1$ be an array of $n$ distinct integers. We wish to sort $A$ in ascending order. We are given that each element in the array is at a position that is at most $k$ away from its position in the sorted array, that is, we are given that $A[i]$ will move to a position in $\left \{ i-k,i-k+1,\dots,i,\dots ,i+k-1,i+k \right \}$ after the array is sorted in ascending order. Suppose insertion sort is used to sort this array: that is, in the $i$ th iteration, $A[i]$ is compared with the elements in positions $A[i-1], A[i-2], \dots$ until one that is smaller is found and $A[i]$ is inserted after that element. Note that elements can be moved back when later insertions are made before them. Let $t(n)$ be the worst-case number of comparisons made by insertion sort for such inputs. Then,

  1. $t\left ( n \right ) = \Theta \left ( n^{2} \right )$
  2. $t\left ( n \right ) = \Theta \left ( n\: \log_{2}\:n \right )$
  3. $t\left ( n \right ) = \Theta \left ( nk\:\log\:k \right )$
  4. $t\left ( n \right ) = \Theta \left ( n\:\log_{2}\:k \right )$
  5. $t\left ( n \right ) = \Theta \left ( nk \right )$
recategorized by

1 Answer

1 votes
1 votes

Let me recall that in insertion sort we have two parts in the array. First one is where element are sorted and second one is in which elements are not sorted. In each step but we do we pick an element (starting element) from the unsorted array and put it into the right place in the sorted array part by comparisons. To insert an element into its exact position elements from the sorted part might need to be shift.

Coming to the question we are promised that each element is at most k distance from its actual position. Now when an element (Say x) will be picked from the unsorted part. To insert x into its correct position we only need to compare it with O(k) many elements and also need to move/shift at most O(k) elements (as given in the question statement that element is k distance away from its actual position). Now to insert n elements intio their right position it will take O(nk) time.

Answer:

Related questions

763
views
1 answers
2 votes
soujanyareddy13 asked Mar 25, 2021
763 views
Let $G$ be a connected bipartite simple graph (i.e., no parallel edges) with distinct edge weights. Which of the following statements on $\text{MST}$ (minimum ... the third lightest edge.No $\text{MST}$ in $G$ contains the heaviest edge.
490
views
0 answers
2 votes
soujanyareddy13 asked Mar 25, 2021
490 views
Let $P$ be a convex polygon with sides $5, 4, 4, 3$. For example, the following:Consider the shape in the plane that consists of all points within distance $1$ from some point ... \ell < 21$21\leq \ell< 22$22\leq \ell< 23$23\leq \ell< 24$
961
views
2 answers
2 votes
soujanyareddy13 asked Mar 25, 2021
961 views
Consider the following statements about propositional formulas.$\left ( p\wedge q \right )\rightarrow r$ ... $\text{(ii)}$ is always false.
836
views
1 answers
1 votes
soujanyareddy13 asked Mar 25, 2021
836 views
Let $L$ be a singly-linked list $X$ and $Y$ be additional pointer variables such that $X$ points to the first element of $L$ and $Y$ points to the ... an element before the first element of $L$.Interchange the first two elements of $L$.