lec17

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

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

17 Sorting and ordering Fundamental sorting algorithms Time and space costs of sorting CSE 12 Sorting Algorithms

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

View Full Document
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)
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

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

View Full Document
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
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

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

View Full Document
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
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 )

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

View Full Document
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
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
Ask a homework question - tutors are online