Вы находитесь на странице: 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 1st pass, last element becomes largest in array. After 2nd pass, 2nd element is 2nd largest

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

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(n2)

Insertion Sort Approach: Remove the next unsorted book, Slide the sorted books to the right one by one until you find the 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(n2) Best time efficiency: O(n)

Shell Sort A variation of the insertion show that is better than O(n2) 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(n2) If n is a power of 2 o Average Case: O(n1.5) If we use ODD spacing o Worst Case: O(n1.5)

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 a2 o Conquer: merge a1 and a2 into a unique sorted array

Merge-Sort Tree An execution of merge-sort is depicted by a binary tree o Each node represents a recursive call of merge-sort and stores Unsorted sequence before execution and its partition Sorted sequence at the end of the execution o Root is the initial call o 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)

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 pattern Note that, the base T(1)=b can occur when: 2i=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).

Quick-Sort Sorts a sequence S based on the divide-and-conquer technique o 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 G o 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 call o Leaves are calls on subsequences of size 0 and 1

Worst-Case Running Time We get the worst running time T(n) if the sequence of n element is in the correct order o The worst case time is O(n2)

Best and Average time complexity In the best case, the pivot divides the array each time in two parts of about the same size and thus, 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 lth element is larger than the pivot and rth element is smaller than the pivot A final swap with the pivot completes one partition step

Pivot Selection Ideally, the pivot should be the median value in the array o 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 before partitioning

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

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 or alphabet of the data items If the longest key has M digits and there are N keys, the radix sort has order O(M x N) Since the size of the keys are not significant, this algorithm is of linear complexity O(N)

Comparing the Algorithms

Вам также может понравиться