CS251-lecture3-Sort

# CS251-lecture3-Sort - The sorting problem Input a list of n...

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

The sorting problem Input: a list of n elements Output: the same list of n elements rearranges in increasing (non decreasing) or decreasing (non increasing) order Why do we care so much about sorting? sorting is used by many applications Sorting is initial step of many algorithms many techniques can be illustrated by studying sorting

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

View Full Document
Selection sort How does it work: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. How does it sort the list in a non increasing order? Selection sort is: The simplest sorting techniques. a good algorithm to sort a small number of elements an incremental algorithm – induction method Selection sort is Inefficient for large lists. Incremental algorithms process the input elements one- by-one and maintain the solution for the elements processed so far.
Selection Sort Algorithm Input: An array A [1. . n ] of n elements. Output: A [1 ..n ] sorted in nondecreasing order. 1. for i 1 to n - 1 2. k i 3. for j i + 1 to n {Find the i th smallest element.} 4. if A [ j ] < A [ k ] then k j 5. end for 6. if k i then interchange A [ i ] and A [ k ] 7. end for

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

View Full Document
Analysis of Algorithms Algorithm analysis: quantify the performance of the algorithm, i.e., the amount of time and space taken in terms of n . T
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/04/2012 for the course COMPUTER 101 taught by Professor Ahmed during the Spring '11 term at alamo.edu.

### Page1 / 16

CS251-lecture3-Sort - The sorting problem Input a list of n...

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

View Full Document
Ask a homework question - tutors are online