# day10 - COP 3503 Computer Science II CLASS NOTES DAY#10...

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

COP 3503 – Computer Science II CLASS NOTES - DAY #10 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 10 - 1 SORTING

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

View Full Document
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. Elementary Sorting Algorithms Selection Sort Selection sort is an attempt to localize the exchanges of array elements by finding a misplaced element first and putting it in its final place. The element with the lowest value is selected and exchanged with the element in the first position. Then for the remaining elements the smallest value is found and interchanged with the second position in the array, and so on. Example – Selection Sort Initial array 7 5 2 3 1 4 6 First swap produces (underlined cells indicate sorted portion of the array) 1 5 2 3 7 4 6 Second swap produces 1 2 5 3 7 4 6 Third swap produces 1 2 3 5 7 4 6 Fourth swap produces 1 2 3 4 7 5 6 Fifth swap produces 1 2 3 4 5 7 6 Sixth swap produces – completes sort 1 2 3 4 5 6 7 Day 10 - 2
Pseudocode and Java Implementations of Selection Sort Pseudocode version selectionsort(data[ ]) for (i = 0; i < data.length-1; i++) select the smallest element among data[i] , …, data[data.length-1]; swap it with data[i]; Generic Java version public void selectionsort( Object[ ] data) { int i, j, least;

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 06/12/2011.

### Page1 / 15

day10 - COP 3503 Computer Science II CLASS NOTES DAY#10...

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

View Full Document
Ask a homework question - tutors are online