Sort 1 - 1 Sorting - Introduction z Sorting is simply the...

Info iconThis preview shows pages 1–10. 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

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: 1 Sorting - Introduction z Sorting is simply the ordering of your data in a consistent manner. (e.g., cards, telephone#s, student names) z Each element is usually part of a collection of data called a record . z Each record contains a key , which is the value to be sorted, and the remainder of the record consists of satellite data . z Assumptions made here: – Integers – Use internal memory 2 Sorting - Introduction z There are several easy algorithms to sort in O ( n 2 ), such as insertion sort. z There is an algorithm, Shellsort , that is very simple to code, runs in o ( n 2 ), and is efficient in practice. z There are slightly more complicated O ( n log n ) sorting algorithms. z Any general-purpose sorting algorithm requires Ω ( n log n ) comparisons. 3 Introduction z Internal vs. External Sorting Methods z Different Sorting Methods – Bubble Sort – Insertion Sort – Selection Sort – Quick Sort – Merge Sort – Shell Sort – Radix Sort 4 Introduction z Types of Sorting – Single-pass – Multiple-pass z Operations in Sorting – Permutation – Inversion (Swap) – Comparison 5 Permutation z A permutation of a finite set S is an ordered sequence of all the elements of S , with each element appearing exactly once. z For example, if S = { a , b , c }, there are 6 permutations of S : abc , acb , bac , bca , cab , cba . z There are n ! permutations of a set of n elements. 6 K-Permutation z A k-permutation of S is an ordered sequence of k elements of S , with no element appearing more than once in the sequence. z The twelve 2-permutations of the set { a , b , c , d } are ab, ac, ad, ba, bc, bd, ca, cb, cd, da, db, dc . 7 Inversion z An inversion in an array of numbers is any ordered pair ( i , j ) having the property that i < j but a [ i ] > a [ j ]. z For example, the input list 34, 8, 64, 51, 32, 21 has nine inversions, namely (34,8), (34,32), (34,21), (64,51), (64,32), (64,21), (51,32), (51,21) and (32,21). z Notice that this is exactly the # of swaps that needed to be performed by insertion sort . 8 Preliminaries z Internal Sort--Each algorithm will be passed an array containing the elements and an integer containing the # of elements. z Validity--We will assume that N , the # of elements passed to our sorting routines, has already been checked and is legal. z Ordering--We require the existence of the “<“ and “>” operators, which can be used to place a consistent ordering on the input. 9 Bubble Sort z It is done by scanning the list from one- end to the other, and whenever a pair of adjacent keys is found to be out of order, then those entries are swapped....
View Full Document

Page1 / 37

Sort 1 - 1 Sorting - Introduction z Sorting is simply the...

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

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