This process continues until the key element is found. If there is no key element present in the given array then its searches fail. Algorithm Algorithms can be implemented as algorithms that are recursive or non-recursive.
ALGORITHM BinSrch ( A[0 … n-1], key)
//implements non-recursive binary search //i/p: Array A in ascending order, key k //o/p: Returns position of the key matched else -1 l 0 r n-1 while l ≤ r do m ( l + r) / 2 if key = = A[m] return m else if key < A[m] r m-1 else l m+1 return -1
|
C(n) = C(n/2) + 1 a = 1 b = 2 f(n) = n0 ; d = 0 case 2 holds: C(n) = Θ (nd log n) = Θ (n0 log n) = Θ ( log n)
|
Features ● Developed by C.A.R. Hoare ● Efficient algorithm ● NOT stable sort ● Significantly faster in practice, than other algorithms Algorithm
ALGORITHM Quicksort (A[ l …r ]) //sorts by quick sort //i/p: A sub-array A[l..r] of A[0..n-1],defined by its left and right indices l and r //o/p: The sub-array A[l..r], sorted in ascending order if l < r Partition (A[l..r]) // s is a split position Quicksort(A[l..s-1]) Quicksort(A[s+1..r]
ALGORITHM Partition (A[l ..r]) //Partitions a sub-array by using its first element as a pivot //i/p: A sub-array A[l..r] of A[0..n-1], defined by its left and right indices l and r (l <r) //o/p: A partition of A[l..r], with the split position returned as this function‘s value p→A[l] i→l j→r + 1; Repeat repeat i→i + 1 until A[i] >=p //left-right scan repeat j→j – 1 until A[j] < p //right-left scan if (i < j) //need to continue with the scan swap(A[i], a[j]) until i >= j //no need to scan swap(A[l], A[j]) return j
|
Best Case | O (n log n) |
Worst Case | O (n2) |
Average Case | O (n log n ) |
ALGORITHM Mergesort ( A[0… n-1] ) //sorts array A by recursive mergesort //i/p: array A //o/p: sorted array A in ascending order
if n > 1 copy A[0… (n/2 -1)] to B[0… (n/2 -1)] copy A[n/2… n -1)] to C[0… (n/2 -1)] Mergesort ( B[0… (n/2 -1)] ) Mergesort ( C[0… (n/2 -1)] ) Merge ( B, C, A )
ALGORITHM Merge ( B[0… p-1], C[0… q-1], A[0… p+q-1] ) //merges two sorted arrays into one sorted array //i/p: arrays B, C, both sorted //o/p: Sorted array A of elements from B & C I →0 j→0 k→0 while i < p and j < q do if B[i] ≤ C[j] A[k] →B[i] i→i + 1 else A[k] →C[j] j→j + 1 k→k + 1 if i == p copy C [ j… q-1 ] to A [ k… (p+q-1) ] else copy B [ i… p-1 ] to A [ k… (p+q-1) ]
|
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define M 2 #define N (1<<M)
typedef double datatype; #define DATATYPE_FORMAT "%4.2g" typedefdatatype mat[N][N]; typedefstruct { intra, rb, ca, cb; } corners; void identity(mat A, corners a) { int i, j; for (i = a.ra; i<a.rb; i++) for (j = a.ca; j <a.cb; j++) A[i][j] = (datatype) (i == j); } void set(mat A, corners a, datatype k) { int i, j; for (i = a.ra; i<a.rb; i++) for (j = a.ca; j <a.cb; j++) A[i][j] = k; } voidrandk(mat A, corners a, double l, double h) { int i, j; for (i = a.ra; i<a.rb; i++) for (j = a.ca; j <a.cb; j++) A[i][j] = (datatype) (l + (h - l) * (rand() / (double) RAND_MAX)); } void print(mat A, corners a, char *name) { int i, j; printf("%s = {\n", name); for (i = a.ra; i<a.rb; i++) { for (j = a.ca; j <a.cb; j++) printf(DATATYPE_FORMAT ", ", A[i][j]); printf("\n"); } printf("}\n"); }
void add(mat A, mat B, mat C, corners a, corners b, corners c) { intrd = a.rb - a.ra; int cd = a.cb - a.ca; int i, j; for (i = 0; i<rd; i++) { for (j = 0; j < cd; j++) { C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] + B[i + b.ra][j + b.ca]; } } } void sub(mat A, mat B, mat C, corners a, corners b, corners c)
{ intrd = a.rb - a.ra; int cd = a.cb - a.ca; int i, j; for (i = 0; i<rd; i++) { for (j = 0; j < cd; j++) { C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] - B[i + b.ra][j
+ b.ca]; } } } voidfind_corner(corners a, inti, int j, corners *b)
{ intrm = a.ra + (a.rb - a.ra) / 2; int cm = a.ca + (a.cb - a.ca) / 2; *b = a; if (i == 0) b->rb = rm; else b->ra = rm; if (j == 0) b->cb = cm; else b->ca = cm; } voidmul(mat A, mat B, mat C, corners a, corners b, corners c)
{ cornersaii[2][2], bii[2][2], cii[2][2], p; mat P[7], S, T; int i, j, m, n, k; m = a.rb - a.ra; assert(m==(c.rb-c.ra)); n = a.cb - a.ca; assert(n==(b.rb-b.ra));
k = b.cb - b.ca; assert(k==(c.cb-c.ca)); assert(m>0); if (n == 1) { C[c.ra][c.ca] += A[a.ra][a.ca] * B[b.ra][b.ca]; return; }
for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++)
{ find_corner(a, i, j, &aii[i][j]); find_corner(b, i, j, &bii[i][j]); find_corner(c, i, j, &cii[i][j]); } } p.ra = p.ca = 0; p.rb = p.cb = m / 2;
#define LEN(A) (sizeof(A)/sizeof(A[0])) for (i = 0; i < LEN(P); i++) set(P[i], p, 0); #define ST0 set(S,p,0); set(T,p,0)
ST0; add(A, A, S, aii[0][0], aii[1][1], p); add(B, B, T, bii[0][0], bii[1][1], p); mul(S, T, P[0], p, p, p); ST0; add(A, A, S, aii[1][0], aii[1][1], p); mul(S, B, P[1], p, bii[0][0], p);
ST0; sub(B, B, T, bii[0][1], bii[1][1], p); mul(A, T, P[2], aii[0][0], p, p);
ST0; sub(B, B, T, bii[1][0], bii[0][0], p); mul(A, T, P[3], aii[1][1], p, p);
ST0; add(A, A, S, aii[0][0], aii[0][1], p); mul(S, B, P[4], p, bii[1][1], p);
ST0; sub(A, A, S, aii[1][0], aii[0][0], p); add(B, B, T, bii[0][0], bii[0][1], p); mul(S, T, P[5], p, p, p);
ST0; sub(A, A, S, aii[0][1], aii[1][1], p); add(B, B, T, bii[1][0], bii[1][1], p); mul(S, T, P[6], p, p, p); add(P[0], P[3], S, p, p, p); sub(S, P[4], T, p, p, p); add(T, P[6], C, p, p, cii[0][0]); add(P[2], P[4], C, p, p, cii[0][1]); add(P[1], P[3], C, p, p, cii[1][0]); add(P[0], P[2], S, p, p, p); sub(S, P[1], T, p, p, p); add(T, P[5], C, p, p, cii[1][1]) } int main()
{ mat A, B, C; cornersai = { 0, N, 0, N }; corners bi = { 0, N, 0, N }; corners ci = { 0, N, 0, N }; srand(time(0)); randk(A, ai, 0, 2); randk(B, bi, 0, 2); print(A, ai, "A"); print(B, bi, "B"); set(C, ci, 0); mul(A, B, C, ai, bi, ci); print(C, ci, "C"); return 0 }
|
XY = (Xl*2n/2 + Xr)(Yl*2n/2 + Yr) = 2n XlYl + 2n/2(XlYr + XrYl) + XrYr |
|
Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Elements | -1 | -6 | 0 | 7 | 9 | 23 | 54 | 82 | 10 |
Comparison | 3 | 2 | 3 | 4 | 1 | 3 | 2 | 3 | 4 |
Min Heap | Max Heap |
The key present at the root node in a Min-Heap must be less than or equal to one of the keys present in all of its children. | The key present at the root node in a Max-Heap must be greater than or equal to one of the keys present in all of its children. |
The minimum main factor present at the root is present in a Min-Heap. | The maximum main factor present at the root is in the Max-Heap. |
The ascending priority is used by A Min-Heap. | The descending priority is used by the Max-Heap. |
The smallest component has priority in the design of a Min-Heap. | In the construction of a Max-Heap, priority is given to the largest element. |
The smallest item in a Min-Heap is the first to pop from the heap. | In a Max-Heap, the first to pop from the heap is the largest element. |