Stack & Queue
Stack
Algorithm to Add(push) an element to Stack.
1. Declaration of Stack. Top, item, Stack [1….M].
2. Add an element to a Stack.
3. If(Top <M)
{
Top=Top+1;
Stack[Top]= item;
}
else
{
print “ Overflow ”
}
4. Output will be updated.
Algorithm to Delete(pop) an element from Stack.
1.Declaration of Stack. Top, item, Stack [1….M].
2.Delete an element from a Stack.
3.If(Top = = 0)
print “Stack is Empty”
else
{
item=Stack[Top]
Top=Top-1;
}
4.Output will be updated.
Queue
Algorithm to Add an element at a Queue.
1. Declaration of Queue. front, rear, item, Queue [1….M].
2. Add an element to a Queue.
3. If(rear = = 0 )
{
front=rear=1;
Queue[rear]=item;
}
4. If(rear<M)
{
rear=rear+1;
Queue[rear]=item;
}
else
{
print “ Overflow ”
}
5. Output will be updated.
Algorithm to Delete an element from Queue.
1.Declaration of Queue. front, rear, item, Queue [1….M].
2.Delete an element from Queue.
3. if(rear = = front )
{
item=Queue[front];
rear=front=0;
}
4. if (front !=0)
{
item=Queue[front];
front=front+1;
}
else
{
print “ Queue is empty ”
}
5.Output will be updated.
Sorting & Searching
Sorting
Algorithm of Bubble sort
(Bubble Sort) BUBBLE (DATA, N)
Here DATA is an array with N element. This algorithm sorts the elements in
DATA.
1. Repeat Steps 2 and 3 for K= 1 too N-1.
2. [initializes pass pointer PTR ]. Set PTR = 1.
3. Repeat While PTR <= N-K : [Executes pass]
a) if DATA[PTR] > DATA[PTR]+1;
Interchange DATA[PTR] and DATA[PTR]+1;
End of if structure.
b) Set PTR=PTR+1;
[end of inner loop]
[end of Step 1 outer loop]
4. Exit.
Algorithm of Insertion sort
(Insertion Sort) INSERSION(A, N)
This algorithm sorts the array A with N elements.
1. Set A[0]=-∞ . [Initializes sentinel element]
2. Repeat Steps 3 to 5 for K=2,3…..N;
3. Set TEMP = A[k] and PTR= K-1;
4. Repeat while TEMP < A[PTR]
a) Set A[PTR+1]=A[PTR].[moves elements forward]
b) Set PTR=PTR-1;
[End of loop ]
5. Set A[PTR+1]=TEMP.[Inserts element in proper place]
[end of Step 2 loop]
6. Return
Algorithm of Selection sort
(Selection sort) SELECTION(A, N)
This algorithm sorts the array A with elements.
1. Repeat Steps 2 and 3 for K= 1,2…N-1;
2. Call MIN(A, K, N, LOC)
3. [Interchange A[k] and A[LOC]]
Set Temp = A[K] , A[K]=A[LOC] and A[LOC]=TEMP.
[Ends of loop 1]
4. Exit
Algorithm of Marge Sort
1.
Sreach
Algorithm of Binary Search
1. Declaration A [ ], X, left, right, mid;
2. first=0, last=n-1, mid=(first + last)/2;
3. While(fast<=last)
if(A[mid]<X)
first=mid+1;
4. else if (A[mid]= = X)
print “ Found ”;
else
last=mid-1;
mid=( first + last)/2;
5. if (first > last)
Print “Not Found”
6. Exit.
Algorithm of Linear Search
1. Declaration A[ ], X.
2. c=0 ;
3. if (A[ c] = = X)
print “ Found”
4. if(c = = 0)
print “Not Found”
5. Exit.
1 D & 2D Array
1D Array
Algorithm of Array Traversing
1. [Initialize counter] ,Set K=LB.
2. Repeat Steps 3 and 4 while K<= UB
3. [Visit element] Apply Process to LA[K]
4. [increase counter] Set K=K+1;
[End loop of 1]
5. exit
Algorithm of Array Insertion
1. Initialize a counter I=N
2. Loop (while >=K)
3. LA[I+1]=LA[I]
4. I=I-1;
5. End loop
6. Set LA[K] = item;
7. N=N+1;
8. Exit;
9.
Algorithm of Array Delation
1. Set item=LA[K]
2. Loop for I= K to N-1;
3. Set LA[I]=LA[I+1]
4. End loop
5. N=N-1;
6. Exit;
2D Array / Matrix
Algorithm of Matrix Addition
1. Declaration A[ i][j] , B[ i][j], C[ i][j];
2. Input : A[ i][j]
3. Input : B[ i][j]
4. C[ i][j]= A[ i][j]+ B[ i][j];
5. Output : C[ i][j]
Algorithm of Matrix Transpose
1. Declaration A[ i][j] , T[ i][j];
2. Input : A[ i][j]
3. T[ j][ i ]= T[ i][j];
4. Output : T[ j][ i];
Algorithm of Matrix Multiplication
1. Declaration : first [10][10],second[10][10],multiply[10][10],sum=0;
2. Input: first[10][10],second[10][10];
3. If(n!=p)
Print “Matrix can’t be multiply”;
else
Input: first[10][10],second[10][10];
4. Sum= Sum+ first[c][k]*second[k][d];
multiply[c][d]=Sum;
Sum=0;
5. Output : multiply[c][d].
Al Islam - 2024