Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Slides on Sorting - Object-Oriented Programming and Data Structures | CS 2110, Study notes of Computer Science

Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Summer 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-1wh-1
koofers-user-1wh-1 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Slides on Sorting - Object-Oriented Programming and Data Structures | CS 2110 and more Study notes Computer Science in PDF only on Docsity! Sorting Lecture 12 CS211 – Summer 2008 2 InsertionSort Many people sort cards this way Invariant: everything to left of i is already sorted Works especially well when input is nearly sorted Worst-case is O(n2) Consider reverse-sorted input Best-case is O(n) Consider sorted input Expected case is O(n2) Expected number of inversions is n(n–1)/4 //sort a[], an array of int for (int i = 1; i < a.length; i++) { int temp = a[i]; int k; for (k = i; 0 < k && temp < a[k–1]; k––) a[k] = a[k–1]; a[k] = temp; } 3 SelectionSort To sort an array of size n: Examine a[0] to a[n–1]; find the smallest one and swap it with a[0] Examine a[1] to a[n–1]; find the smallest one and swap it with a[1] In general, in step i, examine a[i] to a[n–1]; find the smallest one and swap it with a[i] This is the other common way for people to sort cards Runtime Worst-case O(n2) Best-case O(n2) Expected-case O(n2) 4 Divide & Conquer? It often pays to Break the problem into smaller subproblems, Solve the subproblems separately, and then Assemble a final solution This technique is called divide-and-conquer Caveat: It won’t help unless the partitioning and assembly processes are inexpensive Can we apply this approach to sorting? 5 MergeSort Quintessential divide-and-conquer algorithm Divide array into equal parts, sort each part, then merge Questions: Q1: How do we divide array into two equal parts? A1: Find middle index: a.length/2 Q2: How do we sort the parts? A2: call MergeSort recursively! Q3: How do we merge the sorted subarrays? A3: We have to write some (easy) code 6 Merging Sorted Arrays A and B Create an array C of size = size of A + size of B Keep three indices: i into A j into B k into C Initialize all three indices to 0 (start of each array) Compare element A[i] with B[j], and move the smaller element into C[k] Increment i or j, whichever one we took, and k When either A or B becomes empty, copy remaining elements from the other array (B or A, respectively) into C 7 1 3 4 4 6 7 Merging Sorted Arrays C = merged array B A 1 3 4 6 8 4 7 7 8 9k i j 8 MergeSort Analysis Outline (detailed code on the website) Split array into two halves Recursively sort each half Merge the two halves Merge = combine two sorted arrays to make a single sorted array Rule: always choose the smallest item Time: O(n) where n is the combined size of the two arrays Runtime recurrence Let T(n) be the time to sort an array of size n T(n) = 2T(n/2) + O(n) T(1) = 1 Can show by induction that T(n) is O(n log n) Alternately, can see that T(n) is O(n log n) by looking at tree of recursive calls 9 MergeSort Notes Asymptotic complexity: O(n log n) Much faster than O(n2) Disadvantage Need extra storage for temporary arrays In practice, this can be a disadvantage, even though MergeSort is asymptotically optimal for sorting Can do MergeSort in place, but this is very tricky (and it slows down the algorithm significantly) Are there good sorting algorithms that do not use so much extra storage? Yes: QuickSort 10 QuickSort Intuitive idea Given an array A to sort, choose a pivot value p Partition A into two subarrays, AX and AY AX contains only elements ≤ p AY contains only elements ≥ p Sort subarrays AX and AY separately Concatenate (not merge!) sorted AX and AY to get sorted A Concatenation is easier than merging – O(1) 11 20 31 24 19 45 56 4 65 5 72 14 99 pivot partition 5 19 14 4 31 72 56 65 45 24 99 204 5 14 19 24 31 45 56 65 72 99 QuickSort QuickSort 4 5 14 19 20 24 31 45 56 65 72 99 concatenate 12 QuickSort Questions Key problems How should we choose a pivot? How do we partition an array in place? Partitioning in place Can be done in O(n) time (next slide) Choosing a pivot Ideal pivot is the median, since this splits array in half Computing the median of an unsorted array is O(n), but algorithm is quite complicated Popular heuristics: Use first value in array (usually not a good choice) Use middle value in array Use median of first, last, and middle values in array Choose a random element
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved