Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms Lakshmish Ramaswamy. Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays.

Similar presentations


Presentation on theme: "Algorithms Lakshmish Ramaswamy. Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays."— Presentation transcript:

1 Algorithms Lakshmish Ramaswamy

2 Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays –Compare the current pointer elements of two arrays –Place the one that is lowest and increment its pointer

3 Merge Algorithm public static int[] Merge(int[] A1, int[] A2){ int[] A3 = new int[A1.length+ A2.length]; int i, j; while(i < A1.length && j < A2.length){ if(i => A1.length){ A3[i+j] = A2[j]; j++; } elseif (j => A2.length){ A3[i+j] = A1[i]; i++; } elseif(A1[i] < A2[j]){ A3[i+j] = A1[i]; i++; } else{ A3[i+j] = A2[j]; j++; } } return(A3); }

4 Merge Sort Recursive algorithm Partition array at the center Call merge sort on each part of the array Call merge to combine the two parts

5 Quick Sort Select a pivot element Create two arrays –Array1 contains all elements < pivot –Array2 contains all elements > pivot Call quicksort on each array parts Concatenate Array1, Pivot and Array2

6 Trace [9, 3, 8, 12, 1, 5, 22, 18]

7 Analysis More complex than others Best case - array is split in half at each call –log(N) levels of tree –N-1 comparisons per level –O(N logN) Worst case - one subarray is empty (or contains only 1 element) –N levels and N-1 comparisons per level –O(N 2 ) algorithm Average case is O(N logN)

8 How to Select Pivot? Median of the array results in best case –But, calculating median is as hard as sorting itself If the array is random then any element can be selected as pivot –May be even the array[0] or array[n-1] If array is sorted, array[0] and array[n-1] result in worst case Median of array[0], array[n-1] and array[n/2]

9 Data Structures Lakshmish Ramaswamy

10 Example Text editor software Functionalities –Read and store new text (from terminal) –Delete existing text (either at end or in between) –Change existing text

11 Option 1 Store text in an array Appending at end is easy if array length is sufficiently long Deleting text at end is easy But what about adding text or deleting text somewhere in the middle –Needs moving parts of array –Inefficient

12 Data Structures Representation of data and set of operations Different data structures permit different operations with different costs –Insertion, deletion are common operations Goal – Component reuse Implement once use multiple times Example – Filing cabinet, Shelf, Drawer

13 Generic Protocol


Download ppt "Algorithms Lakshmish Ramaswamy. Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays."

Similar presentations


Ads by Google