lec17 - CSE 12 Sorting Algorithms Sorting and ordering...

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

View Full Document Right Arrow Icon
17 Sorting and ordering Fundamental sorting algorithms Time and space costs of sorting CSE 12 Sorting Algorithms
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sorting and ordering To sort elements of a set. .. a set of integers, or a set of Strings, or a set of Objects. .. a total ordering must apply to the elements of the set The elements of a set are said to be totally ordered by a relation if the following axioms hold for all elements x , y , and z from the set: (transitivity:) if x y and y z , then x z (antisymmetry:) if x y and y x , then x equals y (totality:) either x y or y x (or both)
Background image of page 2
Kinds of sorting order A sorting algorithm takes as input a sequence of elements, and arranges the elements of the sequence in order, according to a total ordering relation increasing order (also called sorted or ascending order) : The elements in the sorted sequence are unique (no duplicates) and are ordered such that e 1 < e 2 < …. < e n . For example: 2 < 5 < 16 nondecreasing order : The elements in the sorted sequence may contain duplicates and are sorted such that e 1 ≤ e 2 ≤ …. ≤ e n . For example: 2 ≤ 5 ≤ 5 ≤ 21
Background image of page 3

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

View Full DocumentRight Arrow Icon
Kinds of sorting order decreasing order (also called reverse sorted or descending order) : The elements in the sorted sequence are unique (no duplicates) and are ordered such that e 1 > e 2 > …. > e n . For example: 21 > 16 > 5 > 2 nonincreasing order : The elements in the sorted sequence may contain duplicates and are sorted such that e 1 ≥ e 2 ≥ …. ≥ e n . For example: 21 ≥ 5 ≥ 5 ≥ 2
Background image of page 4
Sorting algorithms There are many sorting algorithms: sorting is an important operation in many applications Some algorithms we will look at: selection sort, insertion sort, quicksort, merge sort, heapsort… For each, we will want to understand how it works, and consider its time and space costs
Background image of page 5

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

View Full DocumentRight Arrow Icon
Sorting algorithm time complexity Most sorting algorithms are based on two fundamental operations on elements of the sequence being sorted: comparing two elements swapping two elements Algorithms differ in how they choose which elements in the sequence to compare, and which elements to swap Other operations are performed as well, but these fundamental operations take most of the time Therefore, to analyze sorting algorithm time costs, it usually suffices to count the number of comparisons and swaps it takes to sort a sequence of size n
Background image of page 6
Sorting algorithm space complexity To sort a sequence of length n , you obviously need O( n ) space to store the sequence All the algorithms we will consider use either a constant O(1) amount of additional space (so these are in-place sorts ), or O( n ) additional space So, the overall space cost of all these algorithms is O( n )
Background image of page 7

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

View Full DocumentRight Arrow Icon
Selection sort Pseudocode : selectionSort( comparableType [] array ) 1. while the size of the unsorted part is greater than 1 2. find the position of the largest element in the unsorted part
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 29

lec17 - CSE 12 Sorting Algorithms Sorting and ordering...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online