Analysis of Insertion Sort and Selection Sort Algorithms, Assignments of Data Structures and Algorithms

Insights into the performance analysis of insertion sort and selection sort algorithms. It discusses the use of binary search in insertion sort and its impact on the running time, the elimination of unnecessary swaps in selection sort, and a comparison of the growth rates of insertion sort, shellsort, and quicksort functions. Students studying data structures and algorithms will find this document useful for understanding the time complexity of these sorting algorithms.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-wtg-1
koofers-user-wtg-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Key to Homework Assignment 10
8.4 When implementing Insertion Sort, a binary search could be used to locate the position within
the first i1 elements of the array into which element ishould be inserted. Why would using
such a binary search not speed up the asymptotic running time for Insertion Sort?
Although the position to insert could be found in Θ(log(i)),shifting the elements to make
room for the insert will still require Θ(i) the same as the original algorithm. So the overall
runtime will (up to big-Θ) be unchanged.
8.5 Figure 8.5 shows the best-case number of swaps for Selection Sort as Θ(n).This is because
the algorithm does not check to see if the ith record is already in the ith position; that is, it
may perform unnecessary swaps.
(a) Modify the algorithm so that it does not make unnecessary swaps.
The original selection sort algorithm is
int minSubscript;
for (int i = 0; i < n-1; i++) {
minSubscript = i;
for (int j = i+1; j < n; j++)
if (list[j] < list[minSubscript])
minSubscript = j;
swap(list[i], list[minSubscript]);
}
We can eliminate unnecessary swaps by testing whether iis the same as minSubscript:
int minSubscript;
for (int i = 0; i < n-1; i++) {
minSubscript = i;
for (int j = i+1; j < n; j++)
if (list[j] < list[minSubscript])
minSubscript = j;
if (i != minSubscript)
swap(list[i], list[minSubscript]);
}
(b) What is your prediction regarding whether this modification actually improves running
time?
1
pf3

Partial preview of the text

Download Analysis of Insertion Sort and Selection Sort Algorithms and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Key to Homework Assignment 10

8.4 When implementing Insertion Sort, a binary search could be used to locate the position within the first i − 1 elements of the array into which element i should be inserted. Why would using such a binary search not speed up the asymptotic running time for Insertion Sort? Although the position to insert could be found in Θ(log(i)), shifting the elements to make room for the insert will still require Θ(i) — the same as the original algorithm. So the overall runtime will (up to big-Θ) be unchanged.

8.5 Figure 8.5 shows the best-case number of swaps for Selection Sort as Θ(n). This is because the algorithm does not check to see if the ith record is already in the ith position; that is, it may perform unnecessary swaps.

(a) Modify the algorithm so that it does not make unnecessary swaps. The original selection sort algorithm is int minSubscript; for (int i = 0; i < n-1; i++) { minSubscript = i; for (int j = i+1; j < n; j++) if (list[j] < list[minSubscript]) minSubscript = j; swap(list[i], list[minSubscript]); }

We can eliminate unnecessary swaps by testing whether i is the same as minSubscript: int minSubscript; for (int i = 0; i < n-1; i++) { minSubscript = i; for (int j = i+1; j < n; j++) if (list[j] < list[minSubscript]) minSubscript = j; if (i != minSubscript) swap(list[i], list[minSubscript]); }

(b) What is your prediction regarding whether this modification actually improves running time?

Data Structures and Algorithms, Key to Homework 10 2

Asymptotically, there will be no difference. In the asymptotic analysis, the principle contribution to the run time is the inner for loop, which is still Θ(n − i). In practice lists for which few swaps are required might see a slight improvement in overall runtime, while lists for which many swaps are required might see an even slighter degradation in run time.

8.8 Assume L is an array, L.length returns the number of records in the array, and qsort(L, i, j) sorts the records of L from i to j using the Quicksort algorithm (that we discussed in class). What is the average-case time complexity for each of the following code fragments?

(a) for (i = 0; i < L.length; i++) qsort(L, 0, i)

The implementation of quicksort we discussed in class chooses the pivot to be the larger of the first two distinct elements in the list. Since L[0:i-1] is sorted, if we assume the elements of the list are all distinct, this will result in choosing the pivot to be the second or third smallest element in L[previous_pivot:i]. (Whether it’s the second or third depends on the value of L[i].) So each second recursive call will be on a list of length previous_length-1 or previous_length-2. In either case the height of the tree of recursive calls will be Θ(i). So each call in the for loop, qsort(L, 0, i), will be Θ(i^2 ), and the total run time will be n∑− 1

i=

i^2 , which is Θ(n^3 ).

(Here n = L.length.) Note that as long as all of the elements of L are distinct, this analysis applies regardless of the original order. So our runtime is best case, average case, and worst case. (b) for (i = 0; i < L.length; i++) qsort(L, i, L.length-1)

In this case we sort the entire list in the first call to qsort, and all of the subsequent calls will call qsort on a sorted list. So the total run time will be

n log(n) +

n∑− 1

i=

(n − i)^2 , which is Θ(n^3 ).

Once again, if we assume that all of the elements of the list are distinct, this is best case, average case, and worst case.

8.10 Graph f 1 (n) = n log(n), f 2 (n) = n^1.^5 , and f 3 (n) = n^2 in the range 1 ≤ n ≤ 1000 to visually compare their growth rates. Typically, the constant factor in the running-time expression for an implementation of Insertion Sort will be less than the constant factors for Shellsort or Quicksort. How many times greater can the constant factor be for Shellsort to be faster than Insertion Sort when n = 1000.? How many times greater can the constant factor be for Quicksort to be faster than Insertion sort when n = 1000. All three functions are increasing and concave up on the interval [1, 1000]. Furthermore f 1 (n) < f 2 (n) ≤ f 3 (n) on the interval.