Download presentation
Presentation is loading. Please wait.
1
Module 8 – Searching & Sorting Algorithms
Ps Module 8 – Searching & Sorting Algorithms 6/9/2019 CSE 1321 Module 8
2
Linear Search Algorithm
METHOD LinearSearch (parameter: list G, target) BEGIN FOR each element temp in list G IF (temp == target) RETURN true ENDIF ENDFOR RETURN false END LinearSearch Ps 5 4/26/2018 CSE 1321 Module 8
3
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; 6 4/26/2018 CSE 1321 Module 8
4
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 IF (mid+1 >= high) RETURN false ENDWHILE END BinarySearch Ps 4/26/2018 CSE 1321 Module 8
5
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/2018 CSE 1321 Module 8
6
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 ENDFOR Ps CSE 1321 Module 8 17 4/26/2018
7
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; } CSE 1321 Module 8 18 4/26/2018
8
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 Ps CSE 1321 Module 8 23 4/26/2018
9
Java Selection Sort Algorithm
for (int i = 0; i < B.length - 1; i++) { int minPos = i; for (int j = i + 1; j < B.length; j++) if (B[j] < B[minPos]) minPos = j; } if (i != minPos && minPos < B.length) int temp = B[minPos]; B[minPos] = B[i]; B[i] = temp; CSE 1321 Module 8 24 4/26/2018
10
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 ENDFOR Ps CSE 1321 Module 8 30 4/26/2018
11
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; CSE 1321 Module 8 31 4/26/2018
12
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 comparator The Arrays class belongs to java.util.Arrays. int [] list1 = new int[10]; ArrayList <Person> errorList= new ArrayList<Person>(); Arrays.sort(list1); errorList.sort(null); CSE 1321 Module 8 4/26/2018
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.