Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching and Sorting Algorithms Based on D. S

Similar presentations


Presentation on theme: "Searching and Sorting Algorithms Based on D. S"— Presentation transcript:

1 Searching and Sorting Algorithms Based on D. S
Searching and Sorting Algorithms Based on D.S. Malik, Java Programming: Program Design Including Data Structures

2 Sequential Search //method seqSearch
public int seqSearch(T[] list, int length, T item){ for(int i = 0; i < length; i++) if(list[i].equals(item)) return i; return -1; //NOT FOUND! }

3 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 length of the list is n The number of key comparisons depends on where in the list the search item is located

4 Analysis: Sequential Search
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 You need to determine the average case

5 Analysis: Sequential Search
To determine the average case Consider all possible cases Find the number of comparisons for each case Add them and divide by the number of cases Average case On average, a successful sequential search searches half the list

6 Binary Search //method binSearch
public int binSearch(T[] list, int length, T item) { int first = 0; int last = length - 1; int middle = -1; while (first <= last) { middle = (first + last) / 2; Comparable<T> listElem = (Comparable<T>) list[middle]; if(listElem.compareTo(item) == 0) return middle; else if(listElem.compareTo(item) > 0) last = middle - 1; first = middle + 1; } return -1; //NOT FOUND!

7 Binary Search (continued)
Sorted list for a binary search Values of first, last, and middle and the number of comparisons for search item 89

8 Analysis: Binary Search
Suppose that length of the sorted list is n and n is a power of 2 (n = 2m  m = log2n) After each iteration of the for loop, about half the elements are left to search The maximum number of iteration of the for loop is about m + 1 Each iteration makes two key comparisons Maximum number of comparisons: 2(m + 1)

9 Big-O Notation Number of comparisons for a list of length n
NOTE: You cannot design a comparison-based search algorithm of an order less than log2n

10 Sorting: Bubble Sort //method bubbleSort
public void bubbleSort(T[] list, int length) { for(int pass = 0; pass < length - 1; pass++) { for(int i = 0; i < length - 1; i++) { Comparable<T> listElem = (Comparable<T>) list[i]; if(listElem.compareTo(list[i + 1]) > 0) { //swap T temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; }

11 Sorting a List: Bubble Sort (continued)
Figure Elements of list during the first iteration Elements of list during the second iteration

12 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

13 Analysis: Bubble Sort Total number of comparisons (general case)
Average number of assignments Total number of comparisons (book’s method)

14 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

15 Selection Sort: Array-Based Lists
//(Helper) method swap private void swap(T[] list, int i, int j){ T temp; temp = list[i]; list[i] = list[j]; list[j] = temp; } //(Helper) method minLocation private int minLocation(T[] list, int first, int last){ int minIndex = first; for(int loc = first + 1; loc <= last; loc++) { Comparable<T> compElem = (Comparable<T>) list[loc]; if(compElem.compareTo(list[minIndex]) < 0) minIndex = loc; return minIndex;

16 Selection Sort: Array-Based Lists
//method selectionSort 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); }

17 Analysis: Selection Sort
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

18 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 selection sort and reduces the number of key comparisons. Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 24 36 10 6 12

19 Insertion Sort: Array-Based Lists
public void insertionSort(T[] list, int length) { for(int i = 1; i < length; i++) { Comparable<T> listElem = (Comparable<T>) list[i]; if(listElem.compareTo(list[i - 1]) < 0) { Comparable<T> temp = (Comparable<T>) list[i]; int loc = i; do { list[loc] = list[loc - 1]; loc--; } while (loc> 0 && temp.compareTo(list[loc - 1]) < 0); list[loc] = (T) temp; }

20 Analysis: Insertion Sort
Average number of item assignments and key comparisons:

21 Quick Sort: Array-Based Lists
General algorithm (pseudocode) 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.

22 Quick Sort: Array-Based Lists
list before the partition list after the partition

23 Quick Sort: Array-Based Lists
//(Helper) 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<T> compElem = (Comparable<T>) list[index]; if(compElem.compareTo(pivot) < 0) { smallIndex++; swap(list, smallIndex, index); } swap(list, first, smallIndex); return smallIndex;

24 Quick Sort: Array-Based Lists
//helper for quickSort: swap private void swap(T[] list, int i, int j) { T temp; temp = list[i]; list[i] = list[j]; list[j] = temp; } //helper for quickSort: recursive method private void recursiveQuickSort(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); //quickSort public void quickSort(T[] list, int length){ recursiveQuickSort(list, 0, length - 1);

25 Analysis: Quick Sort Analysis of the quick sort algorithm for a list of length n

26 Sorting Algorithms: Average Case Number of Comparisons
Simple Sorts Selection Sort Bubble Sort Insertion Sort More Complex Sorts Quick Sort Merge Sort * Heap Sort * O(N2) O(N*log N)


Download ppt "Searching and Sorting Algorithms Based on D. S"

Similar presentations


Ads by Google