Download presentation
Presentation is loading. Please wait.
Published byAlexander Joseph Modified over 9 years ago
1
Sorting preparation for searching
2
Overview levels of performance categories of algorithms Java class Arrays
3
Performance ‘human’ sorting algorithms proven best performance for sorting random data special conditions O(n 2 ) O(n log n) O(n)
4
Categories of algorithms interchange - move items from unsorted to sorted subset (selection, insertion, heapsort) divide and conquer – sort subsets and combine (shellsort, mergesort, quicksort, radix sort) distribution counting
5
Sorting in Java binary search Arrays class static int binarySearch(Object[] a, Object key) Searches the specified array for the specified object using the binary search algorithmbinarySearchObject static int binarySearch(T[] a,binarySearch T key, Comparator c)Comparator Searches the specified array for the specified object using the binary search algorithm. before JAVA 1.5: static int binarySearch(Object[] a, Object key, Comparator c)binarySearchObject Comparator
6
Sorting – modified mergesort* static void sort(Object[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.sortObject public static void sort(T[] a, Comparator c) Sorts the specified array of objects according to the order induced by the specified comparator.Comparator static void sort(Object[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.sortObject public static void sort(T[] a, int fromIndex, int toIndex, Comparator c ) Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.Comparator *primitive type arrays are sorted by quicksort
7
Sorting Quadratic performance (thanks to Lorrie Fava-Lindon)
8
Selection sort elements are selected in order and placed in their final sorted positions i th pass selects the i th largest element in the array, and places it in the ( n- i) th position of the array an “interchange sort”; i.e., based on swapping
9
Selection sort public static void selectionsort( int[] data, int first, int n) { int i, j, temp; int big; // index of largest value // in data[first…first + i] for (i = n-1; i>0; i--) { big = first; for (j=first+1; j<=first+i; j++) if (data[big] < data[j]) big=j; temp = data[first+i]; data[first+i] = data[big]; data[big] = temp; }
10
Analysis of selection sort best, average, and worst case time Θ (n 2 ) comparisons, Θ (n) swaps Θ (n 2 ) time—quadratic.
11
Advantages of Selection sort can be done “in-place”—no need for a second array minimizes number of swaps
12
Insertion sort begin with “sorted” list of one element sequentially insert new elements into sorted list size of sorted list increases, size of unsorted list decreases
13
Insertion sort public static void insertionsort( int[] data, int first, int n) { int i, j; int entry; for (i=1; i<n; i++) { entry = data[first + i]; for (j= first+i; (j>first)&&(data[j-1]>entry) ; j--) data[j] = data[j-1]; data[j]=entry; }
14
Analysis of Insertion sort worst case: elements initially in reverse of sorted order. Θ (n 2 ) comparisons, swaps average case: same analysis as worst case best case: elements initially in sorted order no swaps Θ (n) comparisons
15
Advantages of Insertion sort can be done “in-place” if data is in “nearly sorted” order, runs in Θ (n) time
16
Shellsort improved insertion sort – better than O(n 2 ) (really a divide-and-conquer strategy)
17
Shellsort insertion sort: -most moves of data are a single step shellsort: -long moves in first loop, then shorter moves in later loops -uses same basic logic as insertion
18
Insertion pseudocode a: array of integers, length n for (i = 1; i < n; i++) temp = a[i] j = i while ( a[j-1] > temp ) a[j] = a[j-1] j- - a[j] = temp 14162127333942433032115626194210 30 temp 33394243
19
Shellsort many small ‘parallel’ insertion sorts reduce number of parallel sorts, round by round last round is pure insertion sort (but data is ‘almost’ sorted)
20
Shellsort pseudocode // calcualte h – first, biggest “step size” h = 1 while ( h <= n ) h = h*3 + 1 // repeat, reducing h by approx 2/3 each time while ( h > 1 ) h = h / 3 for (i = h; i < n; i++) temp = a[i] j = i while ( j >= h && a[j-h] > temp ) a[j] = a[j-h] j = j-h a[j] = temp
21
Shellsort example ASORTINGEXAMPLE AEORTINGEXAMPLS AEAGEINMPLORTXS AAEEGILMNOPRSTX 13 h 4 h 1 h
22
Shellsort performance formal analysis unsolved (as far as I know) estimates: O(n 3/2 ), possibly better depends on sequence of h values – should be relatively prime
23
Sorting Heap sort (thanks to Lorrie Fava-Lindon)
24
Heap (recall) Node of heap contains: (element, priority) Storage rules: 1.Element contained by a node has a priority ≥ priorities of node’s children. 2.Tree is a complete binary tree. (All leaves at same level, leftmost positions.)
25
Array representation of complete binary tree (recall) a[0] stores data from root node a[1] data from left child of root a[2] data from right child of root Left child of a[i] located at a[2i+1] Right child of a[i] located at a[2i+2] Parent of a[i] located at a[(i-1)/2] *(for sorting, root is usually not stored at a[1])
26
Phases of Heapsort 0) Interpret array as binary tree 1) Convert the tree into a heap 2) Extract elements from heap, placing them into sorted position in the array
27
Overview of Heapsort - 1 Two stage process First, heapify the array: “rearrange the values in the array so that the corresponding complete binary tree is a heap.” Largest element now at the root position—the first location in the array.
28
Overview of Heapsort - 2 Two stage process Second, repeat Swap elements in first and last locations of heap. Now, largest element in last position—its correct position in sorted order. Element in root out of place. Reheapify downward. Heap shrinks by one, sorted sequence increases by 1. Next largest element now at root position.
29
Analysis of Heapsort time to build initial heap: Θ (n log n) time to remove the elements from heap, and place in sorted array: Θ (n log n) overall time: Θ (n log n) average, and worst cases
30
Advantages of Heapsort in-place (doesn’t require temporary array) asymptotic analysis same as Mergesort, average case of Quicksort on average takes twice as long as Quicksort
31
Constructing heap in Θ (n) time a binary tree with only one node satisfies the properties of a heap interpret array as binary tree, and consider leaves as heaps; i.e., second half of array from midpoint of array downto root, insert each element into heap formed by subtree
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.