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/43•Sorting terminology•Comparison and sorting•Comparing primitive types•Comparing Objects•The Comparable and Comparator interfacesCSE 12Sorting and Searching122/41Sorting and ordering•To 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 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 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 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 e1< e2< …. < en. For example: 2 < 5 < 16•nondecreasing 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 > 2•nonincreasing 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 Terminology•stable 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 52516•inplace sort: The extraspace required to store the data during the sort is independent of the size of the input. 126/41Sorting 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 consider its time and space costs127/41Sorting algorithm time complexity•Most sorting algorithms are based on two fundamental operations:comparing two elementsswapping 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, we often just count the number of comparisonsand swapsit takes to sort a sequence of size n128/41Sorting algorithm space complexity•To sort a sequence of length n, you obviously need O(n) space to store the sequence...
View
Full Document
 Fall '07
 Aben
 Sort, ej, insertion point

Click to edit the document details