DOC PREVIEW
UMD CMSC 132 - Sorting Algorithms

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Sorting Algorithms Fawzi Emad Chau Wen Tseng Department of Computer Science University of Maryland College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge sort O n2 O n log n Linear sort Counting sort Bucket bin sort Radix sort O n Sorting Goal Arrange elements in predetermined order Based on key for each element Derived from ability to compare two keys by size Properties Stable relative order of equal keys unchanged In place uses only constant additional space External can efficiently sort large of keys Sorting Comparison sort Only uses pairwise key comparisons Proven lower bound of O n log n Linear sort Uses additional properties of keys Bubble Sort Approach 1 Iteratively sweep through shrinking portions of list 2 Swap element x with its right neighbor if x is larger Performance O n2 average worst case Bubble Sort Example Sweep 1 Sweep 2 Sweep 3 Sweep 4 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 2 7 5 8 4 2 5 4 7 8 2 7 5 4 8 Bubble Sort Code void bubbleSort int a int outer inner for outer a length 1 outer 0 outer for inner 0 inner outer inner if a inner a inner 1 Swap with Sweep right neighbor int temp a inner through if larger a inner a inner 1 array a inner 1 temp Selection Sort Approach 1 Iteratively sweep through shrinking portions of list 2 Select smallest element found in each sweep 3 Swap smallest element with front of current list Performance O n2 average worst case Example 7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8 Selection Sort Code void selectionSort int a int outer inner min for outer 0 outer a length 1 outer min outer for inner outer 1 inner a length inner Find smallest Sweep if a inner a min element min inner through array Swap with smallest int temp a outer element found a outer a min a min temp Tree Sort Approach 1 Insert elements in binary search tree 2 List elements using inorder traversal Performance Binary search tree O n log n average case O n2 worst case Balanced binary search tree O n log n average worst case Example Binary search tree 7 2 8 5 4 7 2 8 5 4 Heap Sort Approach 1 Insert elements in heap Example Heap 2 Remove smallest 2 element in heap repeat 3 List elements in order of removal from heap Performance O n log n average worst case 4 7 8 5 7 2 8 5 4 Quick Sort Approach 1 Select pivot value near median of list 2 Partition elements into 2 lists using pivot value 3 Recursively sort both resulting lists 4 Concatenate resulting lists For efficiency pivot needs to partition list evenly Performance O n log n average case O n2 worst case Quick Sort Algorithm 1 If list below size K Sort w other algorithm 2 Else pick pivot x and x partition S into L elements x E elements x G elements x 3 Quicksort L G 4 Concatenate L E G If not sorting in place x L E x G Quick Sort Code void quickSort int a int x int y int pivotIndex if y x 0 pivotIndex partionList a x Lower y end of quickSort a x pivotIndex 1 array quickSort a pivotIndex 1 y region to be sorted Upper end of array region to be sorted int partionList int a int x int y partitions list and returns index of pivot Quick Sort Example 7 2 8 5 4 2 5 4 2 4 7 2 4 5 7 8 8 2 4 5 5 4 5 Partition Sort 2 4 4 5 5 Result 7 8 Quick Sort Code int partitionList int a int x int y int pivot a x Use first int left x element int right y as pivot while left right while a left pivot left right left Partition elements while a right pivot in array relative to right value of pivot if left right swap a left right Place pivot in middle swap a x right of partitioned array return right return index of pivot Merge Sort Approach 1 Partition list of elements into 2 lists 2 Recursively sort both lists 3 Given 2 sorted lists merge into 1 sorted list a Examine head of both lists b Move smaller to end of new list Performance O n log n average worst case Merge Example 2 4 5 2 7 4 5 8 4 5 8 2 4 7 8 2 4 5 7 2 7 7 5 8 8 2 4 5 7 8 Merge Sort Example 2 4 5 7 8 7 2 8 5 4 8 5 4 7 2 7 2 8 5 4 5 Split 4 5 8 2 7 7 4 2 8 4 5 5 Merge 4 Merge Sort Code void mergeSort int a int x int y int mid x y 2 Upper Lower end of if y x return end of array mergeSort a x mid array region mergeSort a mid 1 y region to be merge a x y mid to be sorted sorted void merge int a int x int y int mid merges 2 adjacent sorted lists in array Upper Merge Sort Code end of 1st array void merge int a int x int y int mid region int size y x Upper Lower int left x end of end of 2nd array int right mid 1 st 1 array region int tmp int j region for j 0 j size j if left mid tmp j a right else if right y a left a right tmp j a left Copy smaller of two else tmp j a right elements at head of 2 array regions to tmp for j 0 j size j buffer then move on a x j tmp j Copy merged array back Counting Sort Approach 1 Sorts keys with values over range 0 k 2 Count number of occurrences of each key 3 Calculate of keys each key 4 Place keys in sorted location using keys counted If there are x keys key y Put y in xth position Decrement x in case more instances of key y Properties O n k average worst case Counting Sort Example Original list Count 7 2 8 5 4 0 1 2 3 4 0 0 1 0 1 1 0 1 1 0 1 2 3 4 5 6 7 8 Calculate keys value 0 0 1 1 2 3 3 4 5 0 1 2 3 4 5 6 7 8 Counting Sort Example Assign locations 0 0 1 1 2 3 3 4 5 0 1 2 3 4 7 …


View Full Document

UMD CMSC 132 - Sorting Algorithms

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Loading Unlocking...
Login

Join to view Sorting Algorithms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sorting Algorithms and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?