You are on page 1of 7

Name : Munia akter

ID : 181-35-2293
Section: B

Course Name : Algorithms Design & Analysis


Course Teacher : DR. MD. ASRAF ALI
Course Code : SE 215
Week 1:
Linear Search Algorithm:

Algo Linear Search()


{ Set int A[100],n,item,I;
Scanf(“%d”,&n); [ array size ]
Scanf(“%d”,&item); [searching item]
For( i = 0; i < n ; i++){
If(item == a[i]){
Printf(“Item found at : %d index”,i);
Break;
}
}
Insertion Sorting Algorithm :
Algo Insertion sort( a[N])
{
For i = 1 to N-1 do {
Key = a[i]
J=i–1
While ( j> = 0 && key < a[j] ){
a[j+1] = a[j]
j = j -1
}
A[j+1] = key;
}
}
Week 2 :
# Merge two sorted array –
#include <stdio.h>

int merge(int *arr1, int len1, int *arr2, int len2, int *arr) {
int i = 0, j = 0, k = 0;
while((j < len1) && (k < len2)) {
arr[i++] = (arr1[j] < arr2[k])?arr1[j++]:arr2[k++];
}

while(j < len1) arr[i++] = arr1[j++];


while(k < len2) arr[i++] = arr2[k++];

return i;
}

void print_arr(int *arr, int len) {


int i = 0;
while(i < len) {
printf("%d ", arr[i++]);
}
printf("\n");
}
int main() {
int arr1[] = {3, 5, 7, 10, 15, 19, 21, 34, 67};
int arr2[] = {1, 4, 7, 9, 18, 26, 30, 56, 71, 81, 94};
int arr[32] = {0};
int len = 0;
int l1 = sizeof(arr1) / sizeof(int);
int l2 = sizeof(arr2) / sizeof(int);

printf("First Array: ");


print_arr(arr1, l1);
printf("Second Array: ");
print_arr(arr2, l2);

len = merge(arr1, l1, arr2, l2, arr);


printf("Merged Array: ");
print_arr(arr, len);
return 0;
}

Week 6 :
Graph:
BFS algorithm:

Step 1: Initialize all nodes to the ready state (STATUS = 1)


Step 2: Put the starting node A in Queue and change its status to the waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty:
Step 4:
Remove the front node N of Queue. Process N and change the status of N
to the processed state
(STATUS = 3)
Step 5:
Add to the rear of QUEUE all the neighbors of N that are in the steady state
(STATUS= 1),
and change their status to the waiting state (STATUS = 2).
[End of Step 3 loop]
Step 6: Exit.

DFS Algorithm:
Step 1: Initialize all nodes to the ready state (STATUS = 1)
Step 2: Put the starting node A onto STACK and change its status to the
waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until STACK is empty:
Step 4:
POP the top node N of STACK. Process N and change its status to the
processed state(STATUS=3)
Step 5:
Push onto STACK all neighbors of N that are still in the ready
state(STATUS=1), and change their
Status to the waiting state(STATUS=2).
[End of Step 3 loop]

Step 6: Exit.

You might also like