{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec13 - 12-1/43•Sorting terminology•Comparison 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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 12-1/43•Sorting terminology•Comparison and sorting•Comparing primitive types•Comparing Objects•The Comparable and Comparator interfacesCSE 12Sorting and Searching12-2/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)12-3/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 ≤ 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 > 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 ≥ 212-5/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•in-place sort: The extraspace required to store the data during the sort is independent of the size of the input. 12-6/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 costs12-7/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 n12-8/41Sorting algorithm space complexity•To sort a sequence of length n, you obviously need O(n) space to store the sequence...
View Full Document

{[ snackBarMessage ]}

Page1 / 43

lec13 - 12-1/43•Sorting terminology•Comparison and...

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

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