Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: Program Design Including Data Structures1 Searching and Sorting  Learn the various search algorithms  Sequential and binary search.

Similar presentations


Presentation on theme: "Java Programming: Program Design Including Data Structures1 Searching and Sorting  Learn the various search algorithms  Sequential and binary search."— Presentation transcript:

1 Java Programming: Program Design Including Data Structures1 Searching and Sorting  Learn the various search algorithms  Sequential and binary search algorithms  Learn about asymptotic and big-O notation  Learn the various sorting algorithms

2 Java Programming: Program Design Including Data Structures2 Searching and Sorting Algorithms Interface  All algorithms described are generic  Searching and sorting require comparisons of data  They should work on the type of data with appropriate methods to compare data items  All algorithms described are for array-based lists except merge sort  Class SearchSortAlgorithms will implement methods in this interface

3 Java Programming: Program Design Including Data Structures3 Search Algorithms  Each item in a data set has a special member that uniquely identifies the item in the data set  Called the key of the item  Keys are used in operations such as: searching, sorting, inserting, and deleting  Analysis of algorithms involves counting the number of key comparisons  The number of times the key of the search item is compared with the keys in the list

4 Java Programming: Program Design Including Data Structures4 Sequential Search public int seqSearch(T[] list, int length, T searchItem) { int loc; boolean found = false; for (loc = 0; loc < length; loc++) { if (list[loc].equals(searchItem)) { found = true; break; } if (found) return loc; else return -1; } //end seqSearch

5 Java Programming: Program Design Including Data Structures5 Sequential Search public int seqSearch(T[] list, int length, T searchItem) { int loc = 0; boolean found = false; while (loc < length && !found) { if (list[loc].equals(searchItem)) found = true; else loc++; } if (!found) loc = -1; return -1; } //end seqSearch

6 Java Programming: Program Design Including Data Structures6 Sequential Search Analysis  The statements in the for loop are repeated several times  For each iteration of the loop, the search item is compared with an element in the list  When analyzing a search algorithm, you count the number of key comparisons  Suppose that L is a list of length n  The number of key comparisons depends on where in the list the search item is located

7 Java Programming: Program Design Including Data Structures7 Sequential Search Analysis (continued)  Best case  The item is the first element of the list  You make only one key comparison  Worst case  The item is the last element of the list  You make n key comparisons  Average case-  On average, a successful sequential search searches half the list

8 algorith m efficien cy 8 Algorithm Efficiency  Space vs time  space efficiency - the amount of memory or storage a program requires  time efficiency - how long a program takes to execute  complexity theory – separate field of computer science  space complexity  time complexity  Correctness is paramount  clarity

9 algorth m efficien cy 9 Performance analysis  applies to both space and time efficiency  performance measured in terms of some value  usually the amount of data processed - N  cost function  numeric function that gives the performance of an algorithm in terms of one or more variables  approximation

10 Java Programming: Program Design Including Data Structures10 Binary Search  Method binarySearch public int binarySearch(T[] list, int length, T searchItem) { int first = 0; int last = length - 1; int mid = -1; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; Comparable compElem = (Comparable ) list[mid];

11 Java Programming: Program Design Including Data Structures11 Binary Search (continued)  Method binarySearch if (compElem.compareTo(searchItem) == 0) found = true; else if (compElem.compareTo(searchItem) > 0) last = mid - 1; else first = mid + 1; } if (found) return mid; else return -1; }//end binarySearch

12 Java Programming: Program Design Including Data Structures12 Binary Search (continued) Figure 18-1 Sorted list for a binary search Table 18-1 Values of first, last, and middle and the Number of Comparisons for Search Item 89

13 algorith m efficien cy 13  Dominance  given cost functions f and g, g dominates f if  c*g(x) >= f(x) (c is positive)  Asymptotic dominance  given cost functions f and g, g dominates f if  c*g(x) >= f(x) for all x >= x 0 (c, x 0 are positive)  or g asymptotically dominates f if g dominates f for all large values of x  an asymptotically dominant function overestimates the actual cost for all but small amounts of data => the upper bound of the running time for an algorithm

14 algorith m efficien cy 14 Estimating Functions Desirable estimating function has three characteristics 1. It asymptotically dominates the Actual Time function. 2. It is simple to express and understand. 3. It is as close to an estimate as possible.

15 algorith m efficien cy 15 A look at logarithms  Definitions log x b = a iff x ^a = b so log 2 8 = 3 means 2^3 = 8 log nnn log nn2n2 41664256 6643844096 8256204865,536

16 algorith m efficien cy 16 Example  ActualTime(vSize) = vSize 2 + 5*vSize + 100  EstimateofActualTime(vSize) = vSize 2 1. estimate asymptotically dominates ActualTime  c * vSize 2 >= vSize 2 + 5*vSize + 100 2. Simpler to express 3. Closest estimate

17 algorith m efficien cy 17 Order of a function  Given two nonnegative functions f and g, the order of f is g if and only if g asymptotically dominates f  the order of f is g  f is of order g  f = O(g)// Big-O Notation

18 algorith m efficien cy 18 Big-O Notation  ActualTime(vSize) = O(EstimateofActualTime(vSize))  vSize 2 + 5*vSize + 100 = O(vSize 2 )  vSize 2 + 5*vSize + 100 is of order vSize 2  ActualTime(vSize) = O(vSize 2 )  the running time is of order vSize 2  relative speed

19 algorith m efficien cy 19 Time Complexity and Algorithm Analysis  Efficiency of different algorithms  Sequential search O(n)  Binary search O(log n)  Insertion sort O(n 2 )  Quick sort O(n log n)

20 algorith m efficien cy 20 Big-O Arithmetic rules  Let f and g be functions and k a constant 1. O(k * f) = O(f) 2. O(f*g) = O(f) * O(g) and O(f/g) = O(f)/O(g) 3. O(f) >= O(g) iff f dominates g 4. O(f+g) = Max[O(f),O(g)]

21 algorith m efficien cy 21 O(k * f) = O(f)  Constant multipliers do not affect the big-O measure  O(2*N) = O(N)  O(1.5*N) = O(N)  O(2371*N) = O(N)

22 algorith m efficien cy 22 O(f *g) = O(f) * O(g) O(f/g) = O(f)/O(g) O((17*N)*N) = O(17*N)*O(N) = O(N)*O(N) = O(N 2 )

23 algorith m efficien cy 23 O(f) >= O(g) iff f dominates g O(f+g) = Max[O(f),O(g)] O(N 5 + N 2 + N) = Max[O(N 5 ),O(N 2 ),O(N)] = O(N 5 )

24 algorith m efficien cy 24 Dominance rules Let X and Y denote variables and let a,b,n, and m denote constants  X X dominates X!  X! dominates a X  a X dominates b X if a > b  a X dominates x N if a > 1  X n dominates X m if n > m  X dominates log a xif a > 1  log a x dominates log b xif b > a > 1  log a x dominates 1if a > 1  Any term with a singe variable X neither dominates nor is dominated by a term with the single independent variable Y

25 Java Programming: Program Design Including Data Structures25 Asymptotic Notation: Big-O Notation (continued) Figure 18-9 Growth Rate of Various Functions

26 algorith m efficien cy 26 Examples  f(N) = 3*N 4 + 17 * N 3 + 13 * N + 175  order: Max(O(3*N 4 ), O(17*N 3 ),O(13*N),)(175) = Max(O(N 4 ), O(N 3 ),O(N),)(175) = O(N 4 ) f(N) =O(N 4 ) 53 * empCount 2 O(empCount 2 ) 65*ordersFilled 3 + 26*ordersFilled O(ordersFilled 3 ) hdCnt 6 + 3 * hdCnt 5 + 5 * hdCnt 2 + 7O(hdCnt 6 ) 75 + 993*numsToSearchO(numsToSearch) 76431 2*emps 2 + 3*emps + 4 * mngrs+6O(emps 2 + mngrs)

27 algorith m efficien cy 27 Categories of running time  O(1) - constant time, constant algorithms  execution time never varies with the amount of data  very efficient  O(N a ) - polynomial time  O(N) - linear time  O(N 2 ) - quadratic time  O(N 3 ) - cubic time  O(log a N) = O(log N) - logarithmic time  faster then O(N)  O(a N ) - exponential algorithms  slow, impractical

28 algorith m efficien cy 28 Control Structures and Run Time Performance  Single assignment statementO(1)  simple expressionO(1)  The sequencemaximum of O(S1) and O(S2)  if maximum of O(S1),O(S2), and elseO(cond)  for (i = 1;i <= N; i++)O(N * S1)

29 algorith m efficien cy 29 Repetition is the primary determinant of efficiency  Algorithm without loop or recursionO(1)  for (i = a;i <= b; i++)O(1)  for (i = a;i <= N; i++)O(N) //loop body requiring constant time  for (i = a;i <= N; i++)O(N 2 ) for (j = b;j <= N; j++) //loop body requiring constant time  for (i = a;i <= N; i++)O(N*M) //loop body requiring time O(M)

30 Java Programming: Program Design Including Data Structures30 Asymptotic Notation: Big-O Notation  Consider the following algorithm System.out.print(“Enter the first number: “); //Line 1 num1 = console.nextInt(); //Line 2 System.out.println(); //Line 3 System.out.print(“Enter the second number: “); //Line 4 num2 = console.nextInt(); //Line 5 System.out.println(); //Line 6 if (num1 >= num2) //Line 7 max = num1; //Line 8 else //Line 9 max = num2; //Line 10 System.out.println(“The maximum number is: “ + max); //Line 11

31 Java Programming: Program Design Including Data Structures31 Asymptotic Notation: Big-O Notation (continued)  In this algorithm, the number of operations executed is fixed  Now, consider the following algorithm

32 Java Programming: Program Design Including Data Structures32 Asymptotic Notation: Big-O Notation (continued) System.out.println(“Enter positive integers “ + “ending with -1”); //Line 1 count = 0; //Line 2 sum = 0; //Line 3 num = console.nextInt(); //Line 4 while (num != -1) //Line 5 { sum = sum + num; //Line 6 count++; //Line 7 num = console.nextInt(); //Line 8 } System.out.println(“The sum of the numbers is: “ + sum); //Line 9 if (count != 0) //Line 10 average = sum / count; //Line 11 else //Line 12 average = 0; //Line 13 System.out.println(“The average is: “ + average); //Line 14

33 Java Programming: Program Design Including Data Structures33 Asymptotic Notation: Big-O Notation (continued)  The previous algorithm executes 5n + 11 or 5n + 10 operations  Where n is the number of iterations performed by the loop  In these expressions, 5n becomes the dominating term  For large values of n  The terms 11 and 10 become negligible

34 Java Programming: Program Design Including Data Structures34 Asymptotic Notation: Big-O Notation (continued)  Suppose that an algorithm performs f(n) basic operations to accomplish a task  Where n is the size of the problem  f(n) gives you the efficiency of the algorithm  Different algorithms may have different efficiency functions  You can create a comparison table

35 Java Programming: Program Design Including Data Structures35 Asymptotic Notation: Big-O Notation (continued) Table 18-4 Growth Rate of Various Functions

36 Java Programming: Program Design Including Data Structures36 Asymptotic Notation: Big-O Notation (continued)  If an algorithm complexity function is similar to f(n), you can say that the function is of O(n 2 )  Called Big-O of n 2  f(n) = O(g(n)), if there exist positive constants c and n 0 such that: f(n) ≤ cg(n) for all n ≥ n 0

37 Java Programming: Program Design Including Data Structures37 Asymptotic Notation: Big-O Notation (continued) Table 18-7 Some Big-O Functions That Appear in Algorithm Analysis

38 Java Programming: Program Design Including Data Structures38 Asymptotic Notation: Big-O Notation (continued) Table 18-8 Number of Comparisons for a List of Length n

39 Java Programming: Program Design Including Data Structures39 Lower Bound on Comparison- Based Search Algorithms  Sequential and binary search algorithms search the list by comparing elements  These algorithms are called comparison-based search algorithms  Sequential search is of the order n  Binary search is of the order log 2 n  You cannot design a comparison-based search algorithm of an order less than log 2 n

40 algorith m efficien cy 40 Big-O Analysis  For complicated algorithms, big-O analysis may be difficult or impossible  typical case?  cannot capture small differences in algorithms  not applicable for small sets of data

41 Java Programming: Program Design Including Data Structures41 Sorting Algorithms  There are several sorting algorithms in the literature  You can analyze their implementations and efficiency

42 Java Programming: Program Design Including Data Structures42 Sorting a List: Bubble Sort  Method bubbleSort public void bubbleSort(T list[], int length) { for (int iteration = 1; iteration < length; iteration++) { for (int index = 0; index < length - iteration; index++) { Comparable compElem = (Comparable ) list[index]; if (compElem.compareTo(list[index + 1]) > 0) { T temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } }//end bubble sort

43 Java Programming: Program Design Including Data Structures43 Sorting a List: Bubble Sort (continued) Figure 18-11 Elements of list during the first iteration Figure 18-12 Elements of list during the second iteration

44 Java Programming: Program Design Including Data Structures44 Analysis: Bubble Sort  A sorting algorithm makes key comparisons and also moves the data  You look for both operations to analyze a sorting algorithm  The outer loop executes n – 1 times  For each iteration, the inner loop executes a certain number of times  There is one comparison per each iteration of the outer loop

45 Java Programming: Program Design Including Data Structures45  Total number of comparisons (general case)  Average number of assignments  Total number of comparisons (book’s method) Analysis: Bubble Sort (continued)

46 Java Programming: Program Design Including Data Structures46 Selection Sort: Array-Based Lists  Sorts a list by  Selecting the smallest element in the (unsorted portion of the list)  Moving this smallest element to the top of the list

47 Java Programming: Program Design Including Data Structures47 Selection Sort: Array-Based Lists (continued)  Method minLocation private int minLocation(T[] list, int first, int last) { int minIndex = first; for (int loc = first + 1; loc <= last; loc++) { Comparable compElem = (Comparable ) list[loc]; if (compElem.compareTo(list[minIndex]) < 0) minIndex = loc; } return minIndex; }//end minLocation

48 Java Programming: Program Design Including Data Structures48 Selection Sort: Array-Based Lists (continued)  Methods swap and selectionSort private void swap(T[] list, int first, int second) { T temp; temp = list[first]; list[first] = list[second]; list[second] = temp; }//end swap public void selectionSort(T[] list, int length) { for (int index = 0; index < length - 1; index++) { int minIndex = minLocation(list, index, length - 1); swap(list, index, minIndex); } }//end selectionSort

49 Java Programming: Program Design Including Data Structures49  Number of item assignments: 3(n – 1) = O(n)  Number of key comparisons:  Selection sort does not depend on the initial arrangement of the data Analysis: Selection Sort

50 Java Programming: Program Design Including Data Structures50 Insertion Sort: Array-Based Lists  Sorts a list by  Moving each element to its proper place in the sorted portion of the list  Tries to improve the performance of the selection sort  Reduces the number of key comparisons

51 Java Programming: Program Design Including Data Structures51 Insertion Sort: Array-Based Lists (continued)  Method insertionSort public void insertionSort(T[] list, int length) { for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder ++) { Comparable compElem = (Comparable ) list[firstOutOfOrder]; if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0) { Comparable temp = (Comparable ) list[firstOutOfOrder];

52 Java Programming: Program Design Including Data Structures52 Insertion Sort: Array-Based Lists (continued) int location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while (location > 0 && temp.compareTo(list[location - 1]) < 0); list[location] = (T) temp; } }//end insertionSort

53 Java Programming: Program Design Including Data Structures53  Average number of item assignments and key comparisons: Analysis: Insertion Sort

54 Java Programming: Program Design Including Data Structures54 Analysis: Insertion Sort (continued) Table 18-9 Average Case Behavior of the Bubble Sort, Selection Sort, and Insertion Sort Algorithms for a List of Length n

55 Java Programming: Program Design Including Data Structures55 Quick Sort: Array-Based Lists  General algorithm if (the list size is greater than 1) { a. Partition the list into two sublists, say lowerSublist and upperSublist. b. Quick sort lowerSublist. c. Quick sort upperSublist. d. Combine the sorted lowerSublist and sorted upperSublist. }

56 Java Programming: Program Design Including Data Structures56 Quick Sort: Array-Based Lists (continued) Figure 18-37 list before the partition Figure 18-38 list after the partition

57 Java Programming: Program Design Including Data Structures57 Quick Sort: Array-Based Lists (continued)  Method partition private int partition(T[] list, int first, int last) { T pivot; int smallIndex; swap(list, first, (first + last) / 2); pivot = list[first]; smallIndex = first; for (int index = first + 1; index <= last; index++) { Comparable compElem = (Comparable ) list[index]; if (compElem.compareTo(pivot) < 0) { smallIndex++; swap(list, smallIndex, index); } swap(list, first, smallIndex); return smallIndex; }//end partition

58 Java Programming: Program Design Including Data Structures58 Quick Sort: Array-Based Lists (continued)  Method swap and recQuickSort private void swap(T[] list, int first, int second) { T temp; temp = list[first]; list[first] = list[second]; list[second] = temp; }//end swap private void recQuickSort(T[] list, int first, int last) { if (first < last) { int pivotLocation = partition(list, first, last); recQuickSort(list, first, pivotLocation - 1); recQuickSort(list, pivotLocation + 1, last); } }//end recQuickSort

59 Java Programming: Program Design Including Data Structures59 Quick Sort: Array-Based Lists (continued)  Method quickSort public void quickSort(T[] list, int length) { recQuickSort(list, 0, length - 1); }//end quickSort

60 Java Programming: Program Design Including Data Structures60 Analysis: Quick Sort Table 18-10 Analysis of the Quick Sort Algorithm for a List of Length n

61 Java Programming: Program Design Including Data Structures61 Merge Sort: Linked List-Based Lists  General algorithm if the list is of size greater than 1 { a. Divide the list into two sublists. b. Merge sort the first sublist. c. Merge sort the second sublist. d. Merge the first sublist and the second sublist. }

62 Java Programming: Program Design Including Data Structures62 Merge Sort: Linked List-Based Lists (continued) Figure 18-48 Merge sort algorithm

63 Java Programming: Program Design Including Data Structures63 Divide  General steps  Find the middle node  Since you don’t know the size of the list, you have to traverse it until you reach the middle node  Divide the list into two sublists of nearly equal size

64 Java Programming: Program Design Including Data Structures64 Merge  General steps  Compare the elements of the sorted sublists  Adjust the references of the nodes with the smaller info

65 Java Programming: Program Design Including Data Structures65 Merge (continued)  Method recMergeSort private LinkedListNode recMergeSort(LinkedListNode head) { LinkedListNode otherHead; if (head != null) //if the list is not empty if (head.link != null) //if the list has more //than one node { otherHead = divideList(head); head = recMergeSort(head); otherHead = recMergeSort(otherHead); head = mergeList(head, otherHead); } return head; }//end recMergeSort

66 Java Programming: Program Design Including Data Structures66 Merge (continued)  Method mergeSort public void mergeSort() { first = recMergeSort(first); if (first == null) last = null; else { last = first; while (last.link != null) last = last.link; } } //end mergeSort

67 Java Programming: Program Design Including Data Structures67 Analysis: Merge Sort  The maximum number of comparisons is O(nlog 2 n)  This applies for both the worst and the average case

68 Java Programming: Program Design Including Data Structures68 Heap Sort: Array-Based Lists  A heap is a list in which each element contains a key  The key in the element at position k in the list is at least as large as the key in the element at position 2k + 1, and 2k + 2  Given a heap, you can construct a complete binary tree  After you convert the array into a heap, the sorting phase begins

69 Java Programming: Program Design Including Data Structures69 Heap Sort: Array-Based Lists (continued) Figure 18-58 Complete binary tree corresponding to the list in Figure 18-57 Figure 18-57 A list that is a heap

70 Java Programming: Program Design Including Data Structures70 Build Heap  Method heapify private void heapify(T[] list, int low, int high) { int largeIndex; Comparable temp = (Comparable ) list[low]; //copy the root node of the subtree largeIndex = 2 * low + 1; //index of the left child while (largeIndex <= high) { if (largeIndex < high) { Comparable compElem = (Comparable ) list[largeIndex]; if (compElem.compareTo(list[largeIndex + 1]) < 0) largeIndex = largeIndex + 1; //index of the largest child }

71 Java Programming: Program Design Including Data Structures71 Build Heap (continued) if (temp.compareTo(list[largeIndex]) > 0) //subtree //is already in a heap break; else { list[low] = list[largeIndex]; //move the larger //child to the root low = largeIndex; //go to the subtree to //restore the heap largeIndex = 2 * low + 1; } }//end while list[low] = (T) temp; //insert temp into the tree, //that is, list }//end heapify

72 Java Programming: Program Design Including Data Structures72 Build Heap (continue)  Method buildHeap private void buildHeap(T[] list, int length) { for (int index = length / 2 - 1; index >= 0; index--) heapify(list, index, length - 1); }//end buildHeap

73 Java Programming: Program Design Including Data Structures73 Build Heap (continue)  Method heapSort public void heapSort(T[] list, int length) { buildHeap(list, length); for (int lastOutOfOrder = length - 1; lastOutOfOrder >= 0; lastOutOfOrder--) { T temp = list[lastOutOfOrder]; list[lastOutOfOrder] = list[0]; list[0] = temp; heapify(list, 0, lastOutOfOrder - 1); }//end for }//end heapSort

74 Java Programming: Program Design Including Data Structures74 Analysis: Heap Sort  Number of key comparisons in the worst case 2nlog 2 n + O(n) = O(nlog 2 n)  Number of item assignments in the worst case nlog 2 n + O(n) = O(nlog 2 n)  Number of key comparisons, average case 1.39nlog 2 n + O(n) = O(nlog 2 n)  Number of item assignments, average case 1.39nlog 2 n + O(n) = O(nlog 2 n)


Download ppt "Java Programming: Program Design Including Data Structures1 Searching and Sorting  Learn the various search algorithms  Sequential and binary search."

Similar presentations


Ads by Google