Ps Module 8 Searching Sorting Algorithms 10172021 CSE


![C# Linear Search Algorithm public bool linear. Search (int[] array, int target) { for C# Linear Search Algorithm public bool linear. Search (int[] array, int target) { for](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-3.jpg)

![C# Binary Search Algorithm public static bool Binary. Search(int[] search. Array, int find) { C# Binary Search Algorithm public static bool Binary. Search(int[] search. Array, int find) {](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/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/cafbc2f660783b633dd9daf3512acc42/image-10.jpg)
![C# Insertion Sort Algorithm public void insertion. Sort (int[] list) { for (int index C# Insertion Sort Algorithm public void insertion. Sort (int[] list) { for (int index](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-11.jpg)

- Slides: 12

Ps Module 8 – Searching & Sorting Algorithms 10/17/2021 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
![C Linear Search Algorithm public bool linear Search int array int target for C# Linear Search Algorithm public bool linear. Search (int[] array, int target) { for](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-3.jpg)
C# Linear Search Algorithm public bool 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
![C Binary Search Algorithm public static bool Binary Searchint search Array int find C# Binary Search Algorithm public static bool Binary. Search(int[] search. Array, int find) {](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-5.jpg)
C# Binary Search Algorithm public static bool Binary. Search(int[] search. Array, int find) { bool 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 12

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

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

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

C# 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 25
![Insertion Sort Algorithm FOR each index from 2 to n key Aindex position Insertion Sort Algorithm FOR each index from 2 to n key ← A[index] position](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-10.jpg)
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
![C Insertion Sort Algorithm public void insertion Sort int list for int index C# Insertion Sort Algorithm public void insertion. Sort (int[] list) { for (int index](https://slidetodoc.com/presentation_image_h2/cafbc2f660783b633dd9daf3512acc42/image-11.jpg)
C# 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 32

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> my. List= new List<string>(); ◦. . . ◦ my. List. Sort(); • To sort an array of integers ◦ int [] nums= new int [50]; ◦ … ◦ Array. Sort(nums); 4/26/2018 CSE 1321 Module 8 12