Ps Module 8 Searching Sorting Algorithms 1132022 CSE


![Java Linear Search Algorithm public boolean linear. Search (int[] array, int target) { for Java Linear Search Algorithm public boolean linear. Search (int[] array, int target) { for](https://slidetodoc.com/presentation_image_h2/6e6fbb7d49eb4acb7b92afab19d0338a/image-3.jpg)

![Java Binary Search Algorithm public static boolean Binary. Search(int[] search. Array, int find) { Java Binary Search Algorithm public static boolean Binary. Search(int[] search. Array, int find) {](https://slidetodoc.com/presentation_image_h2/6e6fbb7d49eb4acb7b92afab19d0338a/image-5.jpg)




![Insertion Sort Algorithm FOR each index from 2 to n key ← A[index] position Insertion Sort Algorithm FOR each index from 2 to n key ← A[index] position](https://slidetodoc.com/presentation_image_h2/6e6fbb7d49eb4acb7b92afab19d0338a/image-10.jpg)
![Java Insertion Sort Algorithm public void insertion. Sort (int[] list) { for (int index=1; Java Insertion Sort Algorithm public void insertion. Sort (int[] list) { for (int index=1;](https://slidetodoc.com/presentation_image_h2/6e6fbb7d49eb4acb7b92afab19d0338a/image-11.jpg)

- Slides: 12
Ps Module 8 – Searching & Sorting Algorithms 1/13/2022 CSE 1321 MODULE 8 1
Linear Search Algorithm METHOD Linear. Search (parameter: list G, target) BEGIN FOR each element temp in list G IF (temp == target) RETURN true ENDIF ENDFOR RETURN false Ps END Linear. Search 5 4/26/2018 CSE 1321 MODULE 8
Java Linear Search Algorithm public boolean linear. Search (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; } 6 4/26/2018 CSE 1321 MODULE 8
Binary Search Algorithm METHOD Binary. Search (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 IF (mid+1 >= high) RETURN false ENDIF ENDWHILE END Binary. Search 4/26/2018 CSE 1321 MODULE 8 Ps 10
Java Binary Search Algorithm public static boolean Binary. Search(int[] search. Array, int find) { boolean found = false; int low = 0, mid = 0, high = search. Array. length; while (!found) { mid = (low + high) / 2; if (find == search. Array[mid]) return true; else if (find < search. Array[mid]) high = mid; else low = mid; if (low >= high - 1) return false; } return found; } 4/26/2018 CSE 1321 MODULE 8 11
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[I] ← temp ENDIF ENDFOR 4/26/2018 CSE 1321 Module 8 Ps 17
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
Selection Sort Algorithm FOR each I from 0 to n min. Pos ← I FOR each J from I + 1 to n IF (B[j] < B[min. Pos]) min. Pos ← J ENDIF ENDFOR IF (I != min. Pos AND min. Pos < n) temp ← B[min. Pos] ← B[I] ← temp ENDIF ENDFOR 4/26/2018 CSE 1321 Module 8 Ps 23
Java Selection Sort Algorithm for (int i = 0; i < B. length - 1; i++) { int min. Pos = i; for (int j = i + 1; j < B. length; j++) { if (B[j] < B[min. Pos]) min. Pos = j; } if (i != min. Pos && min. Pos < B. length) { int temp = B[min. Pos]; B[min. Pos] = B[i]; B[i] = temp; } } 4/26/2018 CSE 1321 Module 8 24
Insertion Sort Algorithm FOR each index from 2 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 list [position] = key Ps ENDFOR 4/26/2018 CSE 1321 Module 8 30
Java Insertion Sort Algorithm public void insertion. Sort (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
Sorting in a Java Program • • The Arrays and Array. List 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 comparator The Arrays class belongs to java. util. Arrays. int [] list 1 = new int[10]; Array. List <Person> error. List= new Array. List<Person>(); Arrays. sort(list 1); error. List. sort(null); 4/26/2018 CSE 1321 Module 8 34