0% found this document useful (0 votes)
3 views

Binary Search

The document provides pseudocode for three sorting algorithms: Binary Search, Merge Sort, and Quick Sort, along with their respective flowcharts. Each algorithm is designed to efficiently search or sort an array by dividing it into smaller parts and recursively processing those parts. The flowcharts visually represent the steps involved in each algorithm's execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Binary Search

The document provides pseudocode for three sorting algorithms: Binary Search, Merge Sort, and Quick Sort, along with their respective flowcharts. Each algorithm is designed to efficiently search or sort an array by dividing it into smaller parts and recursively processing those parts. The flowcharts visually represent the steps involved in each algorithm's execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Binary Search (Pseudocode)

BinarySearch(arr, left, right, key):


If left > right:
Return -1 // Element not found

mid = (left + right) / 2

If arr[mid] == key:
Return mid // Element found

Else If arr[mid] > key:


Return BinarySearch(arr, left, mid - 1, key) // Search left half

Else:
Return BinarySearch(arr, mid + 1, right, key) // Search right half

Merge Sort (Pseudocode)


MergeSort(arr, left, right):
If left < right:
mid = (left + right) / 2

MergeSort(arr, left, mid) // Sort left half


MergeSort(arr, mid+1, right) // Sort right half

Merge(arr, left, mid, right) // Merge sorted halves

Merge(arr, left, mid, right):


Create temp arrays Left[] and Right[]

Copy elements from arr[left...mid] to Left[]


Copy elements from arr[mid+1...right] to Right[]

i = j = 0, k = left
While i < size(Left) and j < size(Right):
If Left[i] <= Right[j]:
arr[k] = Left[i]
i=i+1
Else:
arr[k] = Right[j]
j=j+1
k=k+1

Copy remaining elements of Left[] (if any) to arr


Copy remaining elements of Right[] (if any) to arr

3. Quick Sort (Pseudocode)


QuickSort(arr, low, high):
If low < high:
pivotIndex = Partition(arr, low, high) // Find pivot
QuickSort(arr, low, pivotIndex - 1) // Sort left half
QuickSort(arr, pivotIndex + 1, high) // Sort right half

Partition(arr, low, high):


pivot = arr[high] // Select last element as pivot
i = low - 1
For j from low to high - 1:
If arr[j] < pivot:
i=i+1
Swap(arr[i], arr[j])

Swap(arr[i+1], arr[high]) // Place pivot at correct position


Return i+1 // Return pivot index
1. Merge Sort Flowchart
+----------------------+
| Start |
+----------------------+
|
v
+----------------------+
| Is array size <= 1 ? |
+----------------------+
|
Yes / | \ No
(Sorted) v
+----------------------+
| Divide array into two|
| halves |
+----------------------+
|
v
+----------------------+
| Recursively sort both|
| halves |
+----------------------+
|
v
+----------------------+
| Merge sorted halves |
+----------------------+
|
v
+----------------------+
| Sorted Output |
+----------------------+
|
v
+----------------------+
| End |
+----------------------+
2. Quick Sort Flowchart
+----------------------+
| Start |
+----------------------+
|
v
+----------------------+
| Is array size <= 1 ? |
+----------------------+
|
Yes / | \ No
(Sorted) v
+----------------------+
| Choose pivot element |
+----------------------+
|
v
+----------------------+
| Partition: |
| Elements < Pivot |
| Elements > Pivot |
+----------------------+
|
v
+----------------------+
| Recursively sort both|
| partitions |
+----------------------+
|
v
+----------------------+
| Combine partitions |
+----------------------+
|
v
+----------------------+
| Sorted Output |
+----------------------+
|
v
+----------------------+
| End |
+----------------------+
3. Binary Search Flowchart
Steps:

Set low = 0 and high = n-1.

Find mid = (low + high) / 2.


If arr[mid] == target, return mid (found).

If arr[mid] > target, search the left half (high = mid - 1).

If arr[mid] < target, search the right half (low = mid + 1).

Repeat until low > high, then return not found.

+----------------------+
| Start |
+----------------------+
|
v
+----------------------+
| Set low = 0, |
| high = n - 1 |
+----------------------+
|
v
+----------------------+
| Is low > high? |
+----------------------+
| Yes | No |
(Not Found) v
+----------------------+
| Compute mid = (low + high) / 2 |
+----------------------+
|
v
+----------------------+
| Is arr[mid] == target? |
+----------------------+
| Yes | No |
(Found) v
+----------------------+
| Is arr[mid] > target? |
+----------------------+
| Yes | No |
(high = mid-1) (low = mid+1)
| |
v v
(Repeat process until found)

You might also like