This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 10/21/2010 1 1 Sorting ringbutton3 Sorting is a process that organizes a collection of data into either ascending or descending order. ringbutton3 An internal sort requires that the collection of data fit entirely in the computer’s main memory. ringbutton3 We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk. ○ We will analyze only some of the internal sorting algorithms. 2 10/21/2010 2 Sorting ringbutton3 Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. ringbutton3 Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. ringbutton3 Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. ringbutton3 A comparisonbased sorting algorithm makes ordering decisions only on the basis of comparisons. 3 Selection Sort ringbutton3 The list is divided into two sublists, sorted and unsorted , which are divided by an imaginary wall. ringbutton3 We find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. ringbutton3 After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. ringbutton3 Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. ringbutton3 A list of n elements requires n1 passes to completely rearrange the data. 4 10/21/2010 3 5 23 78 45 8 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 Sorted Unsorted Selection Sort (cont.) template <class Item> void selectionSort(Item a[], int n) { for (int i = 0; i < n1; i++) { int min = i; for (int j = i+1; j < n; j++) if (a[j] < a[min]) min = j; swap(a[i], a[min]); } } template <class Object> void swap(Object &lhs, Object &rhs) { Object tmp = lhs; lhs = rhs; rhs = tmp; } 6 10/21/2010 4 Selection Sort  Analysis circle5 In general, we compare keys and move items (or exchange items) in a sorting algorithm (which uses key comparisons). barb4right So, to analyze a sorting algorithm we should count the number of key comparisons and the number of moves. circle5 Ignoring other operations does not affect our final result. circle5 In selectionSort function, the outer for loop executes n1 times....
View
Full
Document
This note was uploaded on 11/12/2010 for the course CENG 223 taught by Professor Yok during the Spring '10 term at Middle East Technical University.
 Spring '10
 Yok

Click to edit the document details