day11 - COP 3503 Computer Science II CLASS NOTES - DAY #11...

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

View Full Document Right Arrow Icon
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 <10 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 Day 11 - 1 CHAPTER 8
Background image of page 1

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

View Full DocumentRight Arrow Icon
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] = a[j-1]; a[j] = temp; } Array Position Inversions Processed 0 1 2 3 4 5 initial state 8 5 9 6 2 3 after a[0. .1] is sorted (8,5) 5 8 9 6 2 3 after a[0. .2] is sorted none 5 8 9 2 6 3 after a[0. .3] is sorted (9,2) (8,2) (5,2) 2 5 8 9 6 3 after a[0. .4] is sorted (9,6) (8,6) 2 5 6 8 9 3 after a[0. .5] is sorted (9,3) (8,3) (6,3)
Background image of page 2
Image of page 3
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 / 10

day11 - COP 3503 Computer Science II CLASS NOTES - DAY #11...

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

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