This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 121/43Sorting terminologyComparison and sortingComparing primitive typesComparing ObjectsThe Comparable and Comparator interfacesCSE 12Sorting and Searching122/41Sorting and orderingTo sort elements of a set... a set of integers, or a set of Strings, or a set of Objects... a total orderingmust apply to the elements of the setThe elements of a set are said to be totally ordered by a relation if the following axioms hold for all elements x, y, and zfrom the collection:(transitivity:)if x yand y z, then x z(antisymmetry:)if x yand y x, then xequals y(totality:)either x yor y x (or both)123/41Kinds of sorting orderA sorting algorithm takes as input a sequence of elements, and arranges the elements of the sequence in order, according to a total ordering relationincreasing order(also called sorted or ascending order):The elements in the sorted sequence are unique (no duplicates) and are ordered such that e1< e2< . < en. For example: 2 < 5 < 16nondecreasing order: The elements in the sorted sequence may contain duplicates and are sorted such that e1 e2 . en. For example: 2 5 5 21124/41More kinds of sorting orderdecreasing order(also called reverse sorted or descending order): The elements in the sorted sequence are unique (no duplicates) and are ordered such that e1> e2> . > en. For example: 21 > 16 > 5 > 2nonincreasing order: The elements in the sorted sequence may contain duplicates and are sorted such that e1 e2 . en. For example: 21 5 5 2125/41More Terminologystable sort: Duplicates from the unsorted sequence appear in the same relative order in the sorted sequence. That is, if ei== ejand i < j, then eiwill appear before ejin the sorted sequence. In the following example, the subscripts to 5show their relative order in the unsorted sequence.Unsorted collection:512 6 524Sorted, stable:2 4 51526Sorted, not stable: 2 4 52516inplace sort: The extraspace required to store the data during the sort is independent of the size of the input. 126/41Sorting algorithmsThere are many sorting algorithms sorting is an important operation in many applicationsSome algorithms we will look at: selection sort, insertion sort, quicksort, merge sort, heapsortFor each, we will want to consider its time and space costs127/41Sorting algorithm time complexityMost sorting algorithms are based on two fundamental operations:comparing two elementsswapping two elementsAlgorithms differ in how they choose which elements in the sequence to compare, and which elements to swapOther operations are performed as well, but these fundamental operations take most of the timeTherefore, to analyze sorting algorithm time costs, we often just count the number of comparisonsand swapsit takes to sort a sequence of size n128/41Sorting algorithm space complexityTo sort a sequence of length n, you obviously need O(n) space to store the sequence...
View Full
Document
 Fall '07
 Aben

Click to edit the document details