/
Module 8  –  Searching & Module 8  –  Searching &

Module 8 – Searching & - PowerPoint Presentation

karlyn-bohler
karlyn-bohler . @karlyn-bohler
Follow
347 views
Uploaded On 2019-12-14

Module 8 – Searching & - PPT Presentation

Module 8 Searching amp Sorting Algorithms 71119 CSE 1321 Module 8 1 Ps Topics Linear Search Binary Search Bubble Sort Insertion Sort Selection Sort 71119 CSE 1321 Module 8 2 Linear Search ID: 770310

module sort 1321 int sort module int 1321 position list cse elements unsorted mid chop return array index algorithm

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Module 8 – Searching &" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Module 8 – Searching & Sorting Algorithms 7/11/19 CSE 1321 Module 8 1 Ps

Topics Linear Search Binary Search Bubble Sort Insertion Sort Selection Sort 7/11/19CSE 1321 Module 8 2

Linear SearchProcess of examining all values in a list (or array) until a target value is found or the search reaches the end of the list and no target is found. Number of visits for a linear search of a list (array) of size n elements - The average search visits n/2 elements - The maximum visits is nLinear search is to the order of n (i.e., O(n)) algorithm. The “Big-Omega” notation means “worst-case” scenario 7/11/19CSE 1321 Module 8 3

Linear Search AlgorithmMETHOD LinearSearch (parameter: list G, target) BEGIN FOR each element temp in list G IF (temp == target ) RETURN true ENDIF ENDFOR RETURN false END LinearSearch 4/26/2018 CSE 1321 Module 8 5 Ps

Java Linear Search Algorithm public boolean linearSearch ( int [] array, int target){ for (int i = 0; i < array.length; i++) { // If we find a match, return true if (array[i] == target) return true ; } return false ; } 4/26/2018 CSE 1321 Module 8 6

C# Linear Search Algorithm public bool linearSearch ( int [] array, int target){ for (int i = 0; i < array.Length; i++) { // If we find a match, return true if (array[i] == target) return true ; } return false ; } 4/26/2018 CSE 1321 Module 8 6

C++ Linear Search Algorithm bool linearSearch ( int array[], int size, int target){ for (int i = 0; i < array.Length; i++) { // If we find a match, return true if (array[i] == target) return true ; } return false ; } 4/26/2018 CSE 1321 Module 8 6

Binary Search Works on sorted list (array) and starts in middle element of array. One of three things happens:We found the target (hurrah!) If target is < the element, repeat process on left half (subarray)Else, repeat process on right subarray Usually do this using recursion AMAZING PERFORMANCE log 2 (n) (i.e., O( log(n) ) algorithm.7/11/19 CSE 1321 Module 8 8

7/11/19 CSE 1321 Module 8 9 If you’re rusty on logs… How many times can we chop this in half ? 32 elements

7/11/19 CSE 1321 Module 8 10 If you’re rusty on logs… How many times can we chop this in half ? 32 elements16 elements 16 elements 1 chop

7/11/19 CSE 1321 Module 8 11 If you’re rusty on logs… How many times can we chop this in half ? 32 elements16 elements 16 elements 1 chop 8 elements 8 elements 1 chop Total: 2 chops

7/11/19 CSE 1321 Module 8 12 If you’re rusty on logs… How many times can we chop this in half ? 32 elements16 elements 16 elements 1 chop 8 elements 8 elements 1 chop 4 elements 4 elements 1 chop Total: 3 chops

7/11/19 CSE 1321 Module 8 13 If you’re rusty on logs… How many times can we chop this in half ? 32 elements16 elements 16 elements 1 chop 8 elements 8 elements 1 chop 4 elements 4 elements 1 chop 2 elements 2 elements 1 chop Total: 4 chops

7/11/19 CSE 1321 Module 8 14 If you’re rusty on logs… How many times can we chop this in half ? 32 elements16 elements 16 elements 1 chop 8 elements 8 elements 1 chop 4 elements 4 elements 1 chop 2 elements 2 elements 1 chop 1 element 1 element 1 chop Total: 5 chops

Why 5 Chops? log2 (32) = 5 The “2” is because we chop it in half If we chop it in thirds, we have log3 Searching 4 billion sorted numbers: log2(4 billion) = ~32We can search 4 billion records in 32 steps!Look at everything we don’t search! 7/11/19CSE 1321 Module 8 15

4/26/2018CSE 1321 Module 8 10 Binary Search Algorithm METHOD BinarySearch (parameters: array G , target) BEGIN low ← 0, mid ← 0, high ← the number of elements in G WHILE (true) mid ← (low + high) / 2 IF (target == G[mid] ) RETURN true ELSE IF (target < G[mid] ) high ← mid ELSE low ← mid ENDIF ENDIF IF ( mid+1 >= high ) RETURN false ENDIF ENDWHILE END BinarySearch Ps

4/26/2018CSE 1321 Module 8 11 Java Binary Search Algorithm public static boolean BinarySearch ( int [] searchArray , int find) { boolean found = false ; int low = 0, mid = 0, high = searchArray.length ; while (!found) { mid = (low + high) / 2; if (find == searchArray [mid]) return true ; else if (find < searchArray [mid]) high = mid; else low = mid; if (low >= high - 1) return false ; } return found ; }

4/26/2018CSE 1321 Module 8 12 C# Binary Search Algorithm public static bool BinarySearch ( int [] searchArray , int find) { bool found = false ; int low = 0, mid = 0, high = searchArray.Length; while (!found) { mid = (low + high) / 2; if (find == searchArray [mid]) return true ; else if (find < searchArray [mid]) high = mid; else low = mid; if (low >= high - 1) return false ; } return found ; }

4/26/2018CSE 1321 Module 8 12 C++ Binary Search Algorithm static bool BinarySearch ( int searchArray [], int size, int find) { bool found = false ; int low = 0, mid = 0, high = size; while (!found) { mid = (low + high) / 2; if (find == searchArray [mid]) return true ; else if (find < searchArray [mid]) high = mid; else low = mid; if (low >= high - 1) return false ; } return found ; }

Sorting Sorting is the process of re-arranging elements to be in a specific order We typically sort in non-decreasing or non-increasing order (why do we not say increasing or decreasing order?) Many sorting algorithms with varying complexity and performance Here, we’ll study simple algorithms with horrible performance! 7/11/19 CSE 1321 Module 8 20

Bubble Sort and Exchange Sort Bubble sort (similar to Exchange sort) repeatedly pair-wise examine elements and swap them if needed This process “bubbles” elements to their correct positions in the list, thus called “Bubble” sort These sorts are slow due the high number of comparisons and swaps it makes to sort the list. Referred to as simple sort algorithms7/11/19 CSE 1321 Module 8 21

Bubble Sort https://www.youtube.com/watch?v=Cq7SMsQBEUw(Bubble Sort) 7/11/19 CSE 1321 Module 8 22

First pass of Bubble Sort(six comparisons) [8 , 9, 3, 1, 4, 2, 7] [8, 9, 3, 1, 4, 2, 7] [8, 3, 9, 1, 4, 2, 7][8, 3, 1, 9, 4, 2, 7][8, 3, 1, 4, 9, 2, 7][8, 3, 1, 4, 2, 9, 7][8, 3, 1, 4, 2, 7, 9] // 9 is now in the correct spot, so we don’t consider it anymore// Note: this is the result of the inner loop of BubbleSort 7/11/19 CSE 1321 Module 8 23

Second pass of Bubble Sort(five comparisons) [ 8, 3, 1, 4, 2, 7, 9][3, 8 , 1, 4, 2, 7, 9][3, 1, 8, 4, 2, 7, 9][3, 1, 4, 8, 2, 7, 9][3, 1, 4, 2, 8, 7, 9][3, 1, 4, 2, 7, 8, 9]// 8 is now in the correct spot, so we don’t consider it anymore// 7 is also in the correct place, but we don’t know it yet! 7/11/19 CSE 1321 Module 8 24

Third pass of Bubble Sort(four comparisons) [ 3, 1, 4, 2, 7, 8, 9] [1, 3, 4, 2, 7, 8, 9][1, 3, 4, 2, 7, 8, 9][1, 3, 2, 4, 7, 8, 9][1, 3, 2, 4, 7, 8, 9] // 7 is now in the correct spot, so we don’t consider it anymore // 1 is also in the correct place, but we don’t know it yet! 7/11/19 CSE 1321 Module 8 25

Fourth pass of Bubble Sort(three comparisons) [ 1, 3, 2, 4, 7, 8, 9 ][1, 3, 2, 4, 7, 8, 9][1, 2, 3 , 4, 7, 8, 9][1, 2, 3, 4, 7, 8, 9]// 4 is now in the correct spot, so we don’t consider it anymore // The array is actually sorted , but we don’t know it yet! // We still have to finish this out 7/11/19 CSE 1321 Module 8 26

Fifth pass of Bubble Sort(two comparisons) [ 1, 2, 3, 4, 7, 8 , 9][1, 2, 3, 4, 7, 8, 9][1, 2, 3, 4, 7, 8, 9]// 4 is now in the correct spot, so we don’t consider it anymore// The array is actually sorted, but we don’t know it yet! // We still have to finish this out 7/11/19 CSE 1321 Module 8 27

Last pass of Bubble Sort(one comparison) [ 1, 2, 3, 4, 7 , 8, 9][1, 2, 3, 4, 7, 8, 9]// Last comparison// Total comparisons: 6+5+4+3+2+1 7/11/19CSE 1321 Module 8 28

BubbleSort Code     for ( int i = 0; i < n-1 ; i++)      for (int j = 0; j < n-i-1 ; j++ )         if ( arr [j] > arr [j+ 1 ]){           // swap temp and arr [i]          int temp = arr[j];          arr[j] = arr[j+1];          arr[j+1] = temp;        }      }    } 7/11/19 CSE 1321 Module 4 29

Exchange Sort Working 7/11/19 CSE 1321 Module 8 30 List A = {8, 4, 2, 1} Exchange Sort finds the smallest value and then the next smallest etc. It does it by pair-wise comparisons and swaps. Element: 1 8 and 4, 4 is smaller so it gets swapped, A = 4, 8, 2, 1 4 and 2, 2 is smaller so it gets swapped, A = 2, 8, 4, 1 2 and 1, 1 is smaller so it gets swapped, A = 1 , 8, 4, 2 The first pass has been completed so now we start the same process starting with the second element in the list. Element: 2 8 and 4, 4 is smaller, swap and A= 1 , 4, 8, 2 4 and 2, 2 is smaller, swap and A = 1 , 2 , 8, 4 That completes that pass and now the first 2 values are in the correct position. Now the third pass looks at 8 and 4. Element: 3 4 is smaller, swap and A= 1 , 2 , 4, 8 We’re done, A= 1 , 2 , 4 , 8

Exchange Sort Algorithm // You should take the time to trace through this FOR each I from 1 to n FOR each J from I+1 to n IF (A[J] < A[I]) temp ← A[J] A[J] ← A[I] A[I] ← temp ENDIF ENDFORENDFOR 4/26/2018 CSE 1321 Module 8 17 Ps

Java Exchange Sort Algorithm for ( int i = 0; i < unsorted.length - 1; i++) { for (int j = i+1; j < unsorted.length; j++) { if (unsorted[j] < unsorted [i]) { int temp = unsorted [j]; unsorted [j] = unsorted [i]; unsorted [i] = temp; } } } 4/26/2018 CSE 1321 Module 8 18

C# Exchange Sort Algorithm for ( int i = 0; i < unsorted.Length - 1; i++) { for ( int j = i+1; j < unsorted.Length; j++) { if (unsorted[j] < unsorted [i]) { int temp = unsorted [j]; unsorted [j] = unsorted [i]; unsorted [i] = temp; } } } 4/26/2018 CSE 1321 Module 8 19

C++ Exchange Sort Algorithm for ( int i = 0; i < size - 1; i++) { for ( int j = i+1; j < size; j++) { if (unsorted[j] < unsorted [i]) { int temp = unsorted [j]; unsorted [j] = unsorted [i]; unsorted [i] = temp; } } } 4/26/2018 CSE 1321 Module 8 19

Selection Sort Selection sort works as follows: Find the smallest value in the list and move to it to the first position in the list. This results in the first element being sorted Consider the unsorted part of the list and apply the same process in step 1 Repeat step 2 on each un-sorted sub-list until the entire list is sortedhttps://www.youtube.com/watch?v=92BfuxHn2XE 7/11/19 CSE 1321 Module 8 35

Selection Sort Working 7/11/19 CSE 1321 Module 8 36 Original list: 11 9 17 5 12 Find the smallest in the list and swap it with the first element 5 9 17 11 12 Find the next smallest in the unsorted sub-list and swap with second element. It is already in the correct place 5 9 17 11 12 Find the next smallest in the unsorted sub-list and swap it with the third element. 5 9 11 17 12 Repeat 5 9 11 12 17 Notice that when the unsorted sub-list is of length 1, we are done

Selection Sort Algorithm FOR each I from 0 to n minPos ← I FOR each J from I + 1 to n IF (B[j] < B[minPos] ) minPos ← J ENDIF ENDFOR IF (I != minPos AND minPos < n ) temp ← B[minPos] B[minPos] ← B[ I ] B[ I ] ← temp ENDIF ENDFOR 4/26/2018 CSE 1321 Module 8 23 Ps

Bubble Sort and Selection Sort These algorithms are slow when run on large data sets because of the large number of swaps that are done during the processing. All of these algorithms (and the insertion sort) are considered to be to the order of n squared (i.e., O(n 2)). O(n 2) means that the runtime grows by the square of the size of the array. n == 2 means runtime of “4 time units”n == 3 means runtime of “9 time units”What about n == 1,000,000,000 - like a billion records from AT&T? 7/11/19 CSE 1321 Module 8 38

Insertion Sort Consider what you would do to sort a set of cards.  This approach is an insertion sort - taking a new item and place it correctly relative to the other items in the "finished" portion (left hand and right hand) You continually maintain two sets – unsorted set and a sorted set. For each card in the unsorted set, take it out of that set and place it correctly relative to the sorted set. https://www.youtube.com/watch?v=8oJS1BMKE64 7/11/19 CSE 1321 Module 8 39

Insertion Sort Working 7/11/19 CSE 1321 Module 8 40 List A= { 8, 4, 2, 1} //assume indexing start from 0 the key = 4, index = 1 and position =1 4 < 8 and position > 0 so the while loop shifts 8 into the 4’s position. A= 8,8,2,1 then position-- so now position =0. The while loop stops. key (4) gets assigned to A[position] so A = 4, 8, 2, 1 Now index goes to 2, key =2 and position =2 2 < 8 and position > 0 so the while loops shifts 8 into the 2’s position. A = 4, 8, 8, 1 then position-- so now position =1. The while loop continues. 2< 4 so the while loops shifts 4 into the first 8’s position. A = 4, 4, 8, 1, position -- so now position=0, the while loop ends key (2) gets assigned to A[position] so A = 2, 4, 8, 1 Then one more pass with 1, shifting 8, 4, 2 right 1 space each so then A = 1, 2, 4, 8

Insertion Sort Algorithm FOR each index from 1 to n key ← A[index] position ← index // Shift larger values to the right WHILE (position > 0 AND key < A[position-1]) A[position] = A[position - 1] position ← position - 1 ENDWHILE A [position] = key ENDFOR4/26/2018 CSE 1321 Module 8 30 Ps

Java Insertion Sort Algorithm public void insertionSort ( int[] list){ for (int index=1; index < list.length; index++) { int key = list [index]; int position = index; // Shift larger values to the right while (position > 0 && key < list[position-1]) { list [position] = list [position - 1]; position--; } list [position] = key; } } 4/26/2018 CSE 1321 Module 8 31

C# Insertion Sort Algorithm public void insertionSort ( int[] list){ for (int index = 1; index < list.Length; index++) { int key = list [index]; int position = index; // Shift larger values to the right while (position > 0 && key < list[position-1]) { list [position] = list [position - 1]; position--; } list [position] = key; }} 4/26/2018 CSE 1321 Module 8 32

C++ Insertion Sort Algorithm static void insertionSort ( int list[], int size){ for (int index=1; index < size; index++) { int key = list [index]; int position = index; // Shift larger values to the right while (position > 0 && key < list[position-1]) { list [position] = list [position - 1]; position--; } list [position] = key; }}4/26/2018 CSE 1321 Module 8 31

Sorting in a Java Program The Arrays and ArrayList classes contains sort methods. To use them, the data type you are sorting must be able to be naturally ordered or you must specify a comparatorThe Arrays class belongs to java.util.Arrays. int [] list1 = new int[10]; ArrayList <Person> errorList= new ArrayList<Person>();Arrays.sort(list1); errorList.sort(null); 4/26/2018 34 CSE 1321 Module 8

Sorting in a C# Program The Array and List classes contains sort methods. To use them, the data type you are sorting must implement the IComparble interface. List<string> myList= new List <string>();. . .myList.Sort (); To sort an array of integers int [] nums = new int [50]; … Array.Sort ( nums ); 4/26/2018 46 CSE 1321 Module 8

Sorting in a C++ Program Vectors can be sorted by including “algorithm”, but it’s complicated: #include <algorithm> Beyond the scope of this class 4/26/2018 47 CSE 1321 Module 8

Summary Search is the process of looking for a value in a list of values Searching can be done either in linear or binary fashion Linear approach is slow and takes to the order of O(n) Binary search is much faster and take to the order of O(log(n)) Bubble, Selection, and Insertion sort algorithms are relatively slow compared to other advanced sort method. They perform to the order of O(n2). Meaning that it takes about n2 swaps to sort a list of size n. The larger n, the slower it gets.7/11/19CSE 1321 Module 8 48