day12extra - COP 3503 Computer Science II CLASS NOTES - DAY...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #12 Supplement Efficient Sorting Algorithms The O(N 2 ) limit for sorting based upon inversion removal for adjacent elements is too costly for large sorts and must be broken down to improve efficiency and decrease run-times. Several different techniques have been developed which involve the basic divide and conquer strategy – some more subtly than others. The diminishing increment sort developed by Donald Shell is one such sort as are the merge sort and quick sort techniques. Shell Sort The basic approach to Shell’s sort is given by the following pseudocode algorithm. If h is too small, then the subarray data (i) could be too large and the sort will remain inefficient. On the other hand, if h is too large, then too many small subarrays are created, and although they are sorted, the overall order of the original array will remain largely unchanged. If only one partition of the original array is performed – the gain in execution time will be slight. To solve this problem, several different subdivisions must be used, and for every subdivision, the same procedure will be applied separately. This is shown in the pseudocode algorithm shown below. This is the basic idea behind the Shell sort. The division of the array into several subarrays is done in such a fashion that elements spaced further apart are compared first, then the elements closer to each other are compared, and so on, until adjacent elements are compared on the last pass. The original array is subdivided into Day 12 Supplement - 1 divide data into h subarrays; for (i = 1; i h; i++) sort subarray data (i) ; sort array data; determine numbers h t . . . h 1 of ways of dividiny array data into subarrays; for (h = h t ; t > 1; t--, h = h t ) divide data into h subarrays for (i = 1; i ≤ h; i++) sort subarray data (i) ; sort array data;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
subarrays by picking every h t th element as part of one subarray. Therefore, there are h t subarrays, and for every h = 1, …, h t , For example, if h t = 3, the array data will be subdivided into three subarrays data 1 , data 2 , and data 3 so that: data 31 [0] = data[0], data 31 [1] = data[3], data 31 [2] = data[6], data 31 [i] = data[3*i], … data 32 [0] = data[1], data 32 [1] = data[4], data 32 [2] = data[7], data 32 [i] = data[3*1+1], … data 33 [0] = data[2], data 33 [1] = data[5], data 33 [2] = data[8], data 33 [i] = data[3*1+2], … If h t = 3, the process of extracting subarrays and sorting them is called a 3-sort . If h t = 3, the process is called a 5-sort , and so on. Example – Shell Sort (same example – different view) Original 81 94 11 96 12 35 17 95 28 58 41 75 15 5 subarrays 35 81 41 before 17 94 75 sorting 11 95 28 96 12 58 5 subarrays 35 41 81 after 17 75 94 sorting 11 95 28 96 12 58 After 5-sort 35 17 11 28 12 41 75 15 96 58 81 94 95 Day 12 Supplement - 2 )] ( [ ] [ 1 h i h data i data t h h t - + × =
Background image of page 2
Start 3-sort 35 17 11 28 12 41 75 15 96 58 81 94 95 3 subarrays 28 35 75
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2009 for the course COP 3503c taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 12

day12extra - COP 3503 Computer Science II CLASS NOTES - DAY...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online