Java Unit 8 - Data Structures and Algorithm Design

Embed Size (px)

Citation preview

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    1/10

    Data Structures and Algorithm Design

    Organizing Java Methods that Sort an Array

    A class of static methods that performs different sorts ( generic sorting method)

    -with < ? super T > (any super class of T), we are able to compare elements in an inheritance hierarchy

    Bubble Sort (Sinking Sort) On each pass, successive neighboring pairs are compared It is called Bubble Sort: smaller values bubble their way to top and large values sink to bottom After 1 st pass, last element becomes largest in array. After 2 nd pass, 2 nd element is 2 nd largest

    -checks if it is already in the correct order (thengoes through outer loops only ONCE)

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    2/10

    Selection Sort

    Approach: Look at books, Select shortest book, Swap w/ first book, Look at remaining, Select Shortest,Swap with second book.

    Both implementations result in the similar performance of O(n 2)

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    3/10

    Insertion Sort

    Approach: Remove the next unsorted book, Slide the sorted books to the right one by one until you findthe right spot for the removed book, Insert the book into its new position

    ***Inserts the next unsorted element to its proper location within the sorted portion of an array

    Worst time efficiency: O(n 2)

    Best time efficiency: O(n)

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    4/10

    Shell Sort

    A variation of the insertion show that is better than O(n 2) Approach: Sorting subarrays of equally spaced indices Instead of moving to an adjacent location, an element moves several locations away

    o Results in an almost sorted array

    If we use EVEN spacing o Worst Case: O(n 2) If n is a power of 2o Average Case: O(n 1.5 ) If we use ODD spacing o Worst Case: O(n 1.5 )

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    5/10

    Merge-Sort Algorithm

    Merge-Sort on an input array a with n elements consists of 3 steps:o Divide : if a has 2+ elements, partition a into 2 sequences a1 and a2 of about n/2

    elements each (do nothing is 0 or 1 elements)o Recur : recursively sort a1 and a2o Conquer : merge a1 and a2 into a unique sorted array

    Merge-Sort Tree

    An execution of merge-sort is depicted by abinary tree

    o Each node represents a recursive call ofmerge-sort and stores

    Unsorted sequence beforeexecution and its partition

    Sorted sequence at the end ofthe execution

    o Root is the initial callo Leaves are calls on subsequences of size

    0 or 1

    Conquer (Merge) Step:

    Takes O(n+m) time for two sorted arrays containing n and m elements Takes at most (n-1) comparisons to compare the elements in two subarrays and n moves in

    total so the total time is O(2n-1)

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    6/10

    Analysis of Merge-Sort

    Overall work done at the nodes of depth i is O(n) The height h of the merge-sort tree is O(log n)

    o At each recursive call, we divide the sequence in half Thus, total running time of merge-sort is: O(n log n)

    Recurrence Equation Analysis

    The conquer (merge) step of merge-sort takes at most bn steps, for some constant b We will let T(n) denote the running time of merge-sort:

    Iterative Substitution

    In the iterative substitution , or plug-and-chug , technique, we iteratively apply the recurrence

    equation to itself and see if we can find a patternNote that, the base T(1)=b can occur when:

    2 i=n , therefore, i = log n.

    So, T(n) = bn + bn log n (second part: ibn -> bn log n)

    Thus, T(n) is O(nlog n).

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    7/10

    Quick-Sort

    Sorts a sequence S based on the divide-and-conquer techniqueo Divide: if S has 2+ elements, select a random element x (called pivot )

    and partition (takes O(n) time) S into 3 sequences L stores elements less than x E stores elements equal x G stores elements greater than x

    o Recur: recursively sort sequence L and Go Conquer: put back the elements into S, by linking L, E and G

    Quick-Sort Tree

    An execution of quick-sort is depicted by a binary tree:o Each node represents a recursive call of quick-sort and stores

    Unsorted Sequence before execution and pivot Sorted Sequence at the end of the execution

    o Root is the initial callo Leaves are calls on subsequences of size 0 and 1

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    8/10

    Worst-Case Running Time

    We get the worst running time T(n) if the sequence of n element is in the correct ordero The worst case time is O(n 2)

    Best and Average time complexity

    In the best case , the pivot divides the array each time in two parts of about the same size andthus, will have the same Big O as Merge Sort of O(n log n)

    Average time is also O(n log n) The height of this quick sort tree will be log n

    Creating partition in Quick-Sort without creating two new subsequences

    We choose the pivot and swap it with the last element of the array We use two Indices: Left-Most Index l and Right-Most Index r In the partition step, the two indexes scans the sequence until they cross A swap is performed when the l th element is larger than the pivot and r th element is smaller

    than the pivot A final swap with the pivot completes one partition step

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    9/10

    Pivot Selection

    Ideally, the pivot should be the median value in the arrayo Recommendation: Use median of first, last and middle locations after sorting the three

    Array with first, middle, and last elements sorted

    Array after positioning the pivot and just beforepartitioning

    Merge Sort vs. Quick Sort

    Merge sort is more efficient than quick sort in the worst case but two are same in average case Merge sort requires temp array, so Quick sort is more space efficient

  • 8/12/2019 Java Unit 8 - Data Structures and Algorithm Design

    10/10

    Radix Sort

    Does not compare objects Treats array elements as if they were strings of the same length Groups elements by a specified digit or character ; known as the key

    o Elements placed into which match the key

    Radix Sort The Analysis

    Each key is looked at once for each digit oralphabet of the data items

    If the longest key has M digits and there are Nkeys , the radix sort has order O(M x N)

    Since the size of the keys are not significant, thisalgorithm is of linear complexity O(N)

    Comparing the Algorithms