Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 8 – Searching & Sorting Algorithms

Similar presentations


Presentation on theme: "Module 8 – Searching & Sorting Algorithms"— Presentation transcript:

1 Module 8 – Searching & Sorting Algorithms
Ps Module 8 – Searching & Sorting Algorithms 4/22/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 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; 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 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/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 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; } CSE 1321 Module 8 19 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 C# 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 25 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 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; CSE 1321 Module 8 32 4/26/2018

12 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); CSE 1321 Module 8 4/26/2018


Download ppt "Module 8 – Searching & Sorting Algorithms"

Similar presentations


Ads by Google