CS251-lecture3-Sort

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
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 Right Arrow Icon
Ask a homework question - tutors are online