Unit 2
Unit 2
2.1 Searching
Searching: The process of finding a specific item in a data set.
. Two primary methods of searching are Linear Search and Binary Search.
Pseudocode:
1. Set i = 0
2. Repeat steps 3 and 4 while i < n
3. If A[i] == key, then
return i // Element found at index i
4. Set i = i + 1
5. Return -1 // Element not found in the array
Pseudocode:
1. Set low = 0
2. Set high = n - 1
3. Repeat steps 4 and 5 while low <= high
4. Set mid = (low + high) / 2
5. If A[mid] == key, then
return mid // Element found at index mid
Else if A[mid] < key, then
low = mid + 1 // Search in the right half
Else
high = mid - 1 // Search in the left half
6. Return -1 // Element not found in the array
2.2 Sorting
The process of arranging items in a specific order (e.g., ascending or
descending).
Pseudocode:
Algorithm BubbleSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A
Pseudocode:
Algorithm SelectionSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A
Pseudocode:
Algorithm InsertionSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A
Pseudocode:
Pseudocode:
Algorithm RadixSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A