Sorting Algorithms: Insertion Sort and Shellsort, Study notes of Computer Science

Class notes on sorting algorithms, specifically focusing on insertion sort and shellsort. The algorithms' functionality, worst and average case time complexities, and the importance of removing inversions to achieve an efficient sort. The notes also discuss the advantages of shellsort over insertion sort and provide an analysis of its running time.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-onc
koofers-user-onc ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP 3503 โ€“ Computer Science II โ€“ CLASS NOTES - DAY #11
SORTING
Sorting is one of the fundamental applications of computer systems. Almost
everything done on a computer involves sorting of some kind. We will be
concerned here only with internal sorting which is sorting that can be completely
carried out in main memory (thus the number of elements is relatively small <106
probably). Sorting operations that cannot be completely carried out in main
memory are called external sorts.
In general sorting makes finding things easier. Recall our algorithms for
searching, linear searching was costly but binary searching was much more
efficient because the search space was sorted. Think what finding a phone number
in the Orlando phone book would be like if it were unsorted!
Sorting also has advantages that are less apparent but just as useful as applications
in searching. Consider the problem of determining if an array of values contains
any duplicate entries (an algorithm for solving this problem is shown below).
Algorithm for determining the presence of duplicate elements in an array
assume a[ ], SIZE
for ( i = 0; i < SIZE; i++)
for (j = i+1; j < SIZE; j++)
if (a[i] == a[j]) return true;
return false;
Clearly the worst case time for this algorithm is O(N2) where N = SIZE. Now
consider the following algorithm for the same problem.
Algorithm for determining the presence of duplicate elements in an array
sort_array();
for (i = 0; i < SIZE; i++)
if (a[i] == a[i+1]) return true;
return false;
This algorithm has a worst case time of: max[O(N), O(sort_array())] since the
loop is clearly an O(N) loop one pass through the sorted array will tell you if any
adjacent elements are the same โ€“ so if we can sort the array in any time less than
Day 11 - 1
CHAPTER 8
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Sorting Algorithms: Insertion Sort and Shellsort and more Study notes Computer Science in PDF only on Docsity!

COP 3503 โ€“ Computer Science II โ€“ CLASS NOTES - DAY #

SORTING

Sorting is one of the fundamental applications of computer systems. Almost everything done on a computer involves sorting of some kind. We will be concerned here only with internal sorting which is sorting that can be completely carried out in main memory (thus the number of elements is relatively small < 6 probably). Sorting operations that cannot be completely carried out in main memory are called external sorts. In general sorting makes finding things easier. Recall our algorithms for searching, linear searching was costly but binary searching was much more efficient because the search space was sorted. Think what finding a phone number in the Orlando phone book would be like if it were unsorted! Sorting also has advantages that are less apparent but just as useful as applications in searching. Consider the problem of determining if an array of values contains any duplicate entries (an algorithm for solving this problem is shown below). Algorithm for determining the presence of duplicate elements in an array assume a[ ], SIZE for ( i = 0; i < SIZE; i++) for (j = i+1; j < SIZE; j++) if (a[i] == a[j]) return true; return false; Clearly the worst case time for this algorithm is O(N 2 ) where N = SIZE. Now consider the following algorithm for the same problem. Algorithm for determining the presence of duplicate elements in an array sort_array(); for (i = 0; i < SIZE; i++) if (a[i] == a[i+1]) return true; return false; This algorithm has a worst case time of: max[O(N), O(sort_array())] since the loop is clearly an O(N) loop one pass through the sorted array will tell you if any adjacent elements are the same โ€“ so if we can sort the array in any time less than

CHAPTER 8

quadratic time โ€“ this algorithm will beat the previous version. The only difference between the two algorithms is that the second one assumes the array is sorted. Insertion Sort Insertion sort is one of the simplest sorts known. In the worst case its running time will be O(N 2 ) โ€“ which will occur when the array to be sorted is already sorted but in the reverse order (i.e., want ascending order and array is in descending order or vice versa). The insertion sort algorithm is shown below: Insertion Sort Algorithm assume a[ ], SIZE int temp; for (p = 1; p < SIZE; p++) {temp = a[p]; for (j = p; j > 0 && temp < a[j-1]; j--) a[j] = a[j-1]; a[j] = temp; } Array Position Inversions Processed

initial state 8 5 9 6 2 3 after a[0..1] is sorted (8,5)^5 8 9 6 2 after a[0..2] is sorted none^5 8 9 2 6 after a[0..3] is sorted (9,2) (8,2) (5,2)^2 5 8 9 6 after a[0..4] is sorted (9,6) (8,6)^2 5 6 8 9 after a[0..5] is sorted (9,3) (8,3) (6,3) (5,3)

Insertion sort example โ€“ shaded area is the sorted portion of the array after the step As mention before the worst case time for this algorithm is O(N 2 ) but the best case time will be O(N) which occurs when the input array is already sorted in which case the inner for loop condition will fail at every test and thus it never loops

Shellsort The Shellsort represents an improvement on the insertion sort. It was developed in 1959 by Donald Shell and while it is not the fastest of all sorts it is a subquadratic sort that is simple to code and has a good average performance. The basic idea behind the Shellsort was to avoid moving large amounts of data during the sort by first comparing elements that are relatively far away and then by comparing elements that are relatively much closer โ€“ gradually collapsing into the basic insertion sort where the incremental distance between compared elements is one. The Shellsort operates with an increment sequence , h 1 , h 2 , h 3 , โ€ฆ, hk. Any increment sequence will work, as long as h 1 = 1, however, some increment sequences are better than others, we will see why. After a sorting phase, using some increment hk, the data set will have the property: ๏€ขi, a[i] ๏‚ฃ a[i + hk]. Thus all elements that are spaced hk apart are sorted. At this point the data set is said to be hk โ€“ sorted. Original 81 94 11 96 12 35 17 95 28 58 41 75 15 Start 5-sort 35 81 17 94 11 95 28 96 12 58 41 81 75 94 15 95 After 5-sort 35 17 11 28 12 41 75 15 96 58 81 94 95 Start 3-sort 35 17 11 28 12 41 75 15 96 58 81 94 95 28 35 12 17 11 41 35 75

After 3-sort 28 12 11 35 15 41 58 17 94 75 81 96 95 Start 1-sort 28 12 11 35 15 41 58 17 94 75 81 96 95 12 28 11 12 28 11 12 28 35 11 12 15 28 35 11 12 15 28 35 41 11 12 15 28 35 41 58 11 12 15 17 28 35 41 58 11 12 15 17 28 35 41 58 94 11 12 15 17 28 35 41 58 75 94 11 12 15 17 28 35 41 58 75 81 94 11 12 15 17 28 35 41 58 75 81 94 96 After 1-sort 11 12 15 17 28 35 41 58 75 81 94 95 96 Original 81 94 11 96 12 35 17 95 28 58 41 75 15

Example: N = 8 (2^3 ), gap divisor = 2. Original 8 2 6 3 7 1 5 4 start 3-sort 3 8 2 7 1 6 5 8 4 7 intermediate 3 2 1 5 4 6 8 7 2 4 1 6 5 8 4 7 intermediate 3 2 1 5 4 6 8 7 1 6 5 8 4 7 intermediate 3 2 1 5 4 6 8 7 5 8 4 7 intermediate 3 2 1 5 4 6 8 7 4 7 after 3-sort 3 2 1 5 4 6 8 7 start 2-sort 1 3 2 5 3 4 5 6 4 8 6 7 intermediate 1 2 3 5 4 6 8 7 2 5

intermediate 1 2 3 5 4 6 8 7 3 4 5 6 4 8 6 8 intermediate 1 2 3 5 4 6 8 7 5 6 4 8 6 7 intermediate 1 2 3 5 4 6 8 7 4 8 6 7 after 2-sort 1 2 3 5 4 6 8 7 start 1-sort 1 2 2 3 3 5 4 5 5 6 6 8 7 8 intermediate 1 2 3 4 5 6 7 8 2 3 3 4 4 5 5 6 6 7

Running time (msec) of Insertion and Shellsort with various increments