Download presentation
Presentation is loading. Please wait.
Published byChristiana Horn Modified over 9 years ago
1
1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR
2
The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input sequence such that a 1 ’ ≤ a 2 ’ ≤ · · · ≤ a n ’
3
Why Study Sorting Algorithms? There are a variety of situations that we can encounter –Do we have randomly ordered keys? –Are all keys distinct? –How large is the set of keys to be ordered? –Need guaranteed performance? Various algorithms are better suited to some of these situations
4
Sorting – Definitions Sorting: determine a permutation = (p 1, …, p n ) of n records that puts the keys in non-decreasing order K p 1 < … < K p n. Permutation: a one-to-one function from {1, …, n} onto itself. There are n! distinct permutations of n items. Rank: Given a collection of n keys, the rank of a key is the number of keys that precede it. That is, rank(K j ) = |{K i | K i < K j }|. If the keys are distinct, then the rank of a key gives its position in the output file.
5
Sorting Terminology Internal (the file is stored in main memory and can be randomly accessed) vs. External (the file is stored in secondary memory & can be accessed sequentially only) Comparison-based sort: uses only the relation among keys, not any special property of the representation of the keys themselves. Stable sort: records with equal keys retain their original relative order; i.e., i < j & Kp i = Kp j p i < p j (next slide) Array-based (consecutive keys are stored in consecutive memory locations) vs. List-based sort (may be stored in nonconsecutive locations in a linked manner) In-place sort: needs only a constant amount of extra space in addition to that needed to store keys.
6
Stability A STABLE sort preserves relative order of records with equal keys Sort file on first key: Sort file on second key: Records with key value 3 are not in order on first key!! 6
7
Sorting Categories Sorting by Insertion insertion sort Sorting by Exchange bubble sort, quicksort Sorting by Selection selection sort, heapsort Sorting by Merging merge sort Sorting by Distribution radix sort
8
Elementary Sorting Methods Easier to understand the basic mechanisms of sorting. Good for small files. Good for well-structured files that are relatively easy to sort, such as those almost sorted. Can be used to improve efficiency of more powerful methods.
9
Selection Sort Selection-Sort(A, n) 1. for i = n downto 2 do 2. max i 3. for j = i – 1 downto 1 do 4. if A[max] < A[j] then 5. max j 6. t A[max] 7. A[max] A[i] 8. A[i] t Selection-Sort(A, n) 1. for i = n downto 2 do 2. max i 3. for j = i – 1 downto 1 do 4. if A[max] < A[j] then 5. max j 6. t A[max] 7. A[max] A[i] 8. A[i] t
10
Insertion Sort InsertionSort(A, n) 1. for j = 2 to n do 2. key A[j] 3. i j – 1 4. while i > 0 and key < A[i] 5. A[i+1] A[i] 6. i i – 1 7. A[i+1] key InsertionSort(A, n) 1. for j = 2 to n do 2. key A[j] 3. i j – 1 4. while i > 0 and key < A[i] 5. A[i+1] A[i] 6. i i – 1 7. A[i+1] key
11
Worst-case Analysis The maximum number of comparisons while inserting A[i] is (i-1). So, the number of comparisons is C wc (n) i = 2 to n (i -1) = j = 1 to n-1 j = n(n-1)/2 = (n 2 ) For which input does insertion sort perform n(n-1)/2 comparisons?
12
Bubble Sort (another elementary sorting algorithm) Idea: –Repeatedly pass through the array –Swaps adjacent elements that are out of order Easier to implement, but slower than Insertion sort 123n i 1329648 j
13
Bubble Sort Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] 1329648 i = 1j i
14
Example 1329648 i = 1j 3129648 j 3219648 j 3291648 j 3296148 j 3296418 j 3296481 j 3296481 i = 2j 3964821 i = 3j 9648321 i = 4j 9684321 i = 5j 9864321 i = 6j 9864321 i = 7 j 14
15
Bubble Sort Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] 1329648 i = 1j i
16
Bubble-Sort Running Time T(n) = (n 2 ) Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] T(n) =c 1 (n+1) +c2c2 c3c3 c4c4 = (n) + (c 2 + c 3 + c 4 ) Comparisons: n 2 /2 Exchanges: n 2 /2 16
17
Merge Sort Alg.: MERGE-SORT (A, p, r) if p < r Check for base case then q ← (p + r)/2 Divide MERGE-SORT (A, p, q) Conquer MERGE-SORT (A, q + 1, r) Conquer MERGE (A, p, q, r) Combine Initial call: MERGE-SORT (A, 1, n) 12345678 6231742 5 p r q
18
Sorting Insertion sort –Design approach: –Sorts in place: –Best case: –Worst case: – n 2 comparisons, n 2 exchanges Bubble Sort –Design approach: –Sorts in place: –Running time: – n 2 comparisons, n 2 exchanges Yes (n) (n 2 ) incremental Yes (n 2 ) incremental
19
Sorting Selection sort –Design approach: –Sorts in place: –Running time: – n 2 comparisons, n exchanges Merge Sort –Design approach: –Sorts in place: –Running time: Yes (n 2 ) incremental No divide and conquer (n lgn)
20
Sorting Files with Huge Records and Small Keys Insertion sort or bubble sort? –NO, too many exchanges Selection sort? –YES, it takes linear time for exchanges Merge sort? –Probably not: needs extra space Example application: Reorganize your MP-3 files
21
Sorting Huge, Randomly - Ordered Files Selection sort? –NO, always takes quadratic time Bubble sort? –NO, always takes quadratic time Insertion sort? –NO, quadratic time for randomly-ordered keys Merge sort? –YES, it is designed for this problem Application: Process transaction record for a phone company
22
Sorting Files That are Almost in Order Selection sort? –NO, always takes quadratic time Bubble sort? –NO, always takes quadratic time Insertion sort? –YES, takes linear time for most definitions of “almost in order” Merge sort? –Probably not: takes nlgn Applications: Re-sort a huge database after a few changes Double-check that someone else sorted a file
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.