0% found this document useful (0 votes)
15 views8 pages

Algorithm For Binary Search - Algorithm For Quick Sort - Algorithm For Merge Sort

The document describes algorithms for binary search, quicksort, and mergesort. It provides pseudocode for: 1) Binary search which recursively searches a sorted array to find an item by comparing it to the middle element of the array. 2) Quicksort which uses a partition operation to rearrange elements in the array smaller than a pivot to the left and larger to the right, and then recursively sorts each subarray. 3) Mergesort which recursively divides the array into single elements and then merges the sorted subarrays back together.

Uploaded by

sunilmt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Algorithm For Binary Search - Algorithm For Quick Sort - Algorithm For Merge Sort

The document describes algorithms for binary search, quicksort, and mergesort. It provides pseudocode for: 1) Binary search which recursively searches a sorted array to find an item by comparing it to the middle element of the array. 2) Quicksort which uses a partition operation to rearrange elements in the array smaller than a pivot to the left and larger to the right, and then recursively sorts each subarray. 3) Mergesort which recursively divides the array into single elements and then merges the sorted subarrays back together.

Uploaded by

sunilmt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

• Algorithm for Binary search

• Algorithm for Quick sort


• Algorithm for Merge sort
Algorithm for Binary search
Binary (a[ ], item, low, high)
1. Mid (variable)
2. If (low > high)
1. Return -1
3. End if
4. Mid = (low+high)/2
5. if(item == a[mid])
1. Return mid
6. End if
7. If (item<a[mid])
1. Return binary(item , a, low, mid-1)
8. Else
1. Return binary(item , a, mid+1, high)
9. End if
Algorithm of Partition
• PARTITION( A[ ] , low , high)
1. i, j, pivot
2. Pivot = A[high] ; i = low ; j = high-1; // initialization
3. While (i <=j)
4. While(pivot > a[i]) do
1. i = i + 1
5. End while
6. While (pivot <= a[j]) do
1. j=j–1
7. End while
8. If(i < j )
1. Swap ( a[i] and a[j])
9. Else swap(pivot , a[i])
10. End While.
11. Return i;
Algorithm for Quick sort
1. Quick (a[ ] , low , high)
2. i (variable)
3. If ( low < high)
1. i = PARTITION( A , low , high)
2. Quick (a , low , i-1)
3. Quick (a , i+1, high)
4. End if
Algorithm for Merge sort
Algorithm for Merge
Algorithm SimpleMerge (A, low , mid ,high)
1. i = k = low
2. j = mid + 1
3. While ( i < = mid and j <= high)
4. If (A[i] < A[j] ) then
1. C[K++] = A[i++]
5. Else
1. C[K++] = A[j++]

6. End while
• While (i <= mid )
• C[K] = A[i]
• K = K+1
• i = i+1
• End while
• While (j <= high)
• C[K] = A[j]
• K = K+1
• j = j+1
• End while
• Repeat for i = low to high
• A[i] = C[i]
• End for
Algorithm for Mergesort
Algorithm Mergesort(A[ ] , low , high)
• Mid (variable)
• If(low < high)
• Mid = (low + high)/2
• Mergesort( a , low, mid)
• Mergesort(a, mid+1, high)
• Simplemerge ( a, low , mid, high )
• End if

You might also like