Algorithms Lakshmish Ramaswamy
Binary Search Algorithm Problem – In an sorted array of numbers, search whether a given number is present Can the fact that the array is sorted be used to reduce comparisons?
Illustration 2 5 7 11 14 19 25 31 7 25 Depending upon the relationship part of the array can be safely eliminated from future comparisons
Logic of Binary Search Compare given number to center of the array If found terminate Eliminate one half of the array Continue until no more comparisons left
Binary Search Algorithm public static int (int[] Arr, int given){ int high = Arr.length-1; int low = 0; int mid; while(low <= high){ mid = (low+high)/2; if(Arr[mid] < given) low = mid+1; else if(Arr[mid] > given) high = mid-1; else return (mid); } return(-1);
Trace [2, 4, 6, 9, 13, 16, 20, 24, 26, 29, 30, 32, 36, 38, 41, 50] given = 36 given = 4 given =3
Analysis At most two comparisons at each iteration Constant time per comparison Repeated halving log2N comparisons O(log N) algorithm
Sorting Algorithms Problem – Given an array, rearrange the elements such that they are in increasing (or decreasing order) Fundamental operation with wide range of applications Several algorithms Selection sort Insertion sort Bubble sort Merge sort Quick sort Radix sort
Selection Sort Logic – In ith iteration select the ith smallest element and place it ith location How to select the ith minimum element Repeated use of minimum element algorithm
Selection Sort Algorithm public static void SelectionSort(int[] Arr){ for(int i = 0; i < Arr.length-1; i++){ minElement = Arr[i]; minIndex = i; for(j = (i+1); j < Arr.length; j++) if(Arr[j] < minElement){ minElement = Arr[j]; minIndex = j; } swap(Arr[i], Arr[minIndex]);
Trace [ 9, 3, 8, 12, 1, 5, 22, 18, 14, 2]
Analysis Constant time for comparison (N-1) comparisons when i = 0 1 comparison when i = (N-2) Total comparisons = 1+2+…+(N-2) + (N-1) Total comparisons = (N-1)N/2 O(N2) algorithm
Insertion Sort Conceptual Logic Create a duplicate array Insert elements from original array into duplicate array (ith element in iteration i) Maintain sortedness of duplicate array at all times May need to move existing elements to accommodate the incoming element
Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2] [9] [3, 9] [3, 8, 9] [3, 8, 9, 12] [1, 3, 8, 9, 12] … [1, 2, 3, 5, 8, 9, 12, 14, 18, 22]