lec13 - 12-1/43Sorting terminologyComparison and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 12-1/43Sorting terminologyComparison and sortingComparing primitive typesComparing ObjectsThe Comparable and Comparator interfacesCSE 12Sorting and Searching12-2/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)12-3/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 2112-4/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 212-5/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 52516in-place sort: The extraspace required to store the data during the sort is independent of the size of the input. 12-6/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 costs12-7/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 n12-8/41Sorting algorithm space complexityTo sort a sequence of length n, you obviously need O(n) space to store the sequence...
View Full Document

Page1 / 43

lec13 - 12-1/43Sorting terminologyComparison and...

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