Ps Module 5 Searching Sorting Algorithms Linear Search

  • Slides: 12
Download presentation
Ps Module 5 – Searching & Sorting Algorithms

Ps Module 5 – Searching & Sorting Algorithms

Linear Search Algorithm METHOD Linear. Search (parameter: list G, target) BEGIN FOR each element

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 END Linear. Search Ps

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 (int i = 0; i < array. Length; i++) { // If we find a match, return true if (array[i] == target) return true; } return false; }

Binary Search Algorithm METHOD Binary. Search (parameters: array G, target) BEGIN low = 0,

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 Ps

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) { 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; }

Exchange Sort Algorithm // You should take the time to trace through this FOR

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 Ps

C# Exchange Sort Algorithm for (int i = 0; i < unsorted. Length -

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; } } }

Selection Sort Algorithm FOR each I from 0 to n min. Pos = I

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 Ps

C# Selection Sort Algorithm for (int i = 0; i < B. Length -

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; } }

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 = 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

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 = 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; } }

Sorting in a C# Program • The Array and List classes contains sort methods.

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);