sortingAlgorithms

sortingAlgorithms - CS202 Fundamentals of Computer Science...

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

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

View Full Document

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

View Full Document

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

View Full Document

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.

Unformatted text preview: 2/21/11 CS202 - Fundamentals of Computer Science II 1 Sorting • Sorting is a process that organizes a collection of data into either ascending or descending order. • An internal sort requires that the collection of data fit entirely in the computer’s main memory. • 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 internal sorting algorithms. • Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. • Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. • Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. • A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons. 2/21/11 CS202 - Fundamentals of Computer Science II 2 Sorting Algorithms • There are many sorting algorithms, such as: – Insertion Sort – Selection Sort – Bubble Sort – Merge Sort – Quick Sort • First three sorting algorithms are not so efficient, but last two are efficient sorting algorithms. • We have already discussed Insertion Sort and Merge Sort algorithms. 2/21/11 CS202 - Fundamentals of Computer Science II 3 Selection Sort • The list is divided into two sublists, sorted and unsorted , which are divided by an imaginary wall. • We find the biggest element from the unsorted sublist and swap it with the element at the end of the unsorted data. • After each selection and swapping, the imaginary wall between the two sublists move one element back, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. • A list of n elements requires n-1 passes to completely rearrange the data . 2/21/11 CS202 - Fundamentals of Computer Science II 4 Selection Sort (cont.) Unsorted Sorted 2/21/11 CS202 - Fundamentals of Computer Science II 5 Selection Sort (cont.) typedef type-of-array-item DataType; void selectionSort(DataType theArray[], int n) { for (int last = n-1; last >= 1; --last) { int largest = indexOfLargest(theArray, last+1); swap(theArray[largest], theArray[last]); } } 2/21/11 CS202 - Fundamentals of Computer Science II 6 Selection Sort (cont.) int indexOfLargest(const DataType theArray[], int size) { int indexSoFar = 0; for(int currentIndex=1; currentIndex<size;++currentIndex) { if (theArray[currentIndex] > theArray[indexSoFar]) indexSoFar = currentIndex; } return indexSoFar; } -------------------------------------------------------- void swap(DataType &x, DataType &y) { DataType temp = x; x = y; y = temp; } 2/21/11 CS202 - Fundamentals of Computer Science II 7 Selection Sort -- Analysis • In general, we compare keys and move items (or exchange items) in a...
View Full Document

{[ snackBarMessage ]}

Page1 / 34

sortingAlgorithms - CS202 Fundamentals of Computer Science...

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

View Full Document
Ask a homework question - tutors are online