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

Ilovepdf Merged

Uploaded by

oxford.2015.iit3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Ilovepdf Merged

Uploaded by

oxford.2015.iit3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Algorithm BinSrch(a,i,l,x) {

// Given an array a[I:l] of elements in non decreasing


order, 1≤ i ≤ l,
// determine whether x is //present and if so, return j
such that x =a[j]; else return 0.
if (l=i) then
{ // if Small(P)
if (x=a(i)) then return i;
else return 0;
}
else {
// Reduce P into a smaller subproblem.
mid :=[(i + l)/2 ];
if (x=a[mid]) then return mid;
else if (x<a[mid] then
return BinSrch (a,i, mid -1,x);
else return BinSrch (a, mid +1,l,x);
}
}
Algorithm BinSearch (a,n,x) {
low :=1;high :=n;
while (low ≤ high) do {
mid :=(low +high)/2;
if ( x<a[mid]) then high :=mid – 1;
else if (x>a[mid]) then low :=mid + 1;
else return mid;
}
return 0;
}
Algorithm BubbleSort (a,n)
{
for i := 1 to n do
{
for j :=1 to n-i do
{
if ( a[j] > a[j+1] )
{
// swap a[j] and a[j+1]
temp := a[j]; a[j] := a[j+1];a[j + 1] := temp;
}
}
}
}
Algorithm DandCLargest (a,k,j)
{
if (k = j) Return a[n];
else
{
mid := (k + j) /2;
x1= DandCLargest (a,k,mid);
x2= DandCLargest (a,mid+1,j);
if (x1 > x2) Return x1;
else Return x2;
}
}

First Call for Recursion:


DandCLargest (a,1,n);
Algorithm Greedy Knapsack(a,n)
// Objects are sorted in the non-increasing order of p[i]/w[i]
{
for i := 1 to n do x[i] := 0.0;
U := m;
for i := 1 to n do
{
if (w[i] > U ) then break;
x[i] := 1; U:=U - w[i];
}
}
if (i <= n) then x[i] := U / w[i];
Algorithm IBacktrack(n)
// This schema describes the backtracking process.
// All solutions are generated in x{1: n} and printed
// as soon as they are determined.
{
k := 1;
while (k ≠ 0) do
{
if (there remains an untried x[k] €T (x[1],x[2],…..,
x[k-1] and Bk(x[1],….,x[k])is true)then
{
if(x[1],….,x[k] is a path to an answer node)
then write(x[1:k]);
k :=k+1; // Consider the next set.
}
else k:= k-1;// Backtrack to the previous set.
}
}
}
Algorithm insertionSort(a,n)
{
for i := 2 to n do
{
value := a[i]; j := i - 1;
done := false;
repeat
if a[j] > value
{
a[j + 1] := a[j]; j := j - 1;
if (j < 1) done := true;
}
else
done := true;
until done;
a[j + 1] := value;
}
}
Algorithm Merge_Sort(low, high) {
// Small(P) is true if there is only one element to sort .
// In this case the list is already sorted.
if (low < high) then {
//If there are more than one element
//Divide P into subproblems.
mid := [(low + high)/2] ;
// Solve the subproblems.
MergeSort(low, mid);
MergeSort (mid + 1, high);
// Combine the solutions.
Merge(low, mid, high);
}
}
Algorithm N Queen(k,n)
// Using backtracking, this procedure prints all
//possible placement of n queens on an n x n
//chessboard so that they are nonattacking.
{
for i:=1 to n do
{
}
}
if place(k, i) then
{
}
x[k] := i;
if (k = n] then write (x[1 ; n]);
else NQueens (k+1,n);
}
}
}
Algorithm place(k,i)
// Returns true if a queen can be placed in kth row and
//ith column. Otherwise it returns false. x[]is a
//global array whose first (k-1) values have been set.
//Abs(r) returns the absolute value of r.
{
for j :=1 to k-1 do
if((x[j] =i) // Two in the same column
or (Abs(x[j]-i) = Abs(j-k)))
// or in the same diagonal
then return false;
return true;
}
Greedy algorithm to generate shortest paths
Algorithm Shortest paths (v, cost ,dist, n)
// dist[j]. 1≤ j≤ n, is set to the length of the shortest
// path from vertex v to vertex j in a digraph G with n
// vertices, dist[v]is set to zero. G is represented by its
// cost adjacency matrix cost[1:n ,1;n]
{
for i:=1 to n do
{ // Initialize S.
S[i] :=false; dist[i]:=cost[v,i];
}
S[v] :=true; dist[v] :=0.0; //put v in S.
for num := 2 to n do
{
// Determine n - 1 paths from v.
Choose u from among those vertices not
in S such that dist[u] is minimum;
S[n] :=true;//put u in S.
for (each w adjacent to u with S[w] = false ) do
// Update distances.
if(dist[w] > dist[u] + cost[u,w])) then
dist[w] :=dist[u] + cost[u, w];
}
}
Kruskal’s Algorithm
Float kruskal (int E[ ][ ], float cost[ ][ ], int n, int t[ ][2])
{
int parent[w];
consider heap out of edge cost;
for (i=1; i<=n; i++)
parent[i] = -1;
//Each vertex in different set
i=0;
mincost = 0;
while((i<n-1) && (heap not empty))
{
Delete a minimum cost edge (u,v) from the heap and re heapify;
j = Find(u); k = Find(v); // Find the set
if (j != k)
{
i++;
t[i][1] = u;
t[i][2] = v;
mincost += cost[u][v];
Union(j, k);
}
if (i != n-1)
printf(“No spanning tree \n”);
else
return(mincost);
}
}

Time complexity of the above algorithm is O(n log n)


Algorithm Linear search (a, req_ele)
{
Flag=0
for i := 1 to n do
{
If (a[i] = req_ele) then
{
Flag=1;
Pos = i;
break;
}
}
If (flag) then
Write “element found at ‘pos’ position”
Else
Write “element not found”
End if
}
place there is no Combine step for the Quick sort.

1. Algorithm QuickSort (p,q)


2. // Sorts the elements a[p],…, a[q] which reside in the global
3. // array a[1 ;n] into ascending order ; a[n+1] is considered to
4. // be defined and must be ≥ all the elements in a[1 :n]
5. {
6. if (p < q) then // if there are more than one element
7. {
8. // divide P into two sub problems.
9. j : = Partition (a,p,q +1);
10. // j is the position of the partitioning element.
11. // Solve the subproblems.
12. QuickSort (p,j -1),
13. QuickSort ( j +1,q)
14. // there is no need for combining solutions.
15. }
16. }
Algorithm for Sorting by partitioning.

1. Algorithm Partition (a,m,p)


2. //Within a[m],a[m+1],….a[p-1] the elements are
3. //rearranged in such a manner that if initially t = a[m],
4. // then after completion a[q] = t for some q between m
5. // and p-1, a[k]≤ t for m≤ k<q, and a [k] ≥ t
6. // for q< k<p. q is returned. Set a[p] = ∞.
7. {
8. v :=a[m], I :=m; j : = p;
9. repeat
10. {
11. repeat
12. i :=i+1;
13. until (a[i]≥v);
14. repeat
15. j : = j – 1;
16. until (a[j] ≤ v);
17. if (I < i) then interchange (a,I,j);

18. } until (I ≥ j);


19. a[m] : = a[j]; a[j] : = v; return j ;
20. }

1. Algorithm Interchange ( a , i, j)
2. //Exchange a[i] with a [j]
3. {
4. p : = a[i];
5. a [i] : = a[j]; a[j] : p;
6. }
Algorithm Selection Sort (a, n)
// Sort the array a[1 : n] into non decreasing order.
{
for i := 1 to n do
{
min:=i;
for k : i + 1 to n do
If (a[k]< a[min]) then min:=k;
t :=a[i]; a[i] := a[min];a[min]:= t;
}
}

You might also like