Algorithms 
Introduction By: 
Dr. Ghulam Rasool
Algorithm 
• An algorithm is a well-defined list of steps for solving a 
particular problem 
• The efficiency of an algorithm depends on space and time
Insertion into an array Insert(A,N,K, item) 
• A is array with N elements and K is a +ve integer such that 
K<=N. This algorithm inserts an item into kth location in 
array A 
1 Set J=N 
2 Repeat steps 3 and 4 while J>=K 
3 Set A[J+1]= A[j] 
4 Set J=J-1 
5 Set A[k]= item 
6 Set N=N+1 
7 Exit
Deletion from array Delete(A, N, K, item) 
1 Set item =A[K] 
2 Repeat for j= K to N-1 
Set A[j]= A[J+1] 
3 Set N=N-1 
4 Exit 
Matrix Multiplication(A,B,C, M, N, P) 
1 Repeat steps 2 to 4 for i = 1 to M 
2 Repeat steps 3 to 4 for j= 1 to N 
3 Set C[i][j]= 0 
4 Repeat for k= 1 to P 
Set C[i][j]= C[i][j]+ A[i][k] * B[k][j] 
5 Exit
Assignment1b 
• A magic square is a square matrix of integers 
such that sum of every row, the sum of every 
column and sum of each of the diagonal are 
equal. For example 
• Write an algorithm to read set of integers for 
square matrix and then decide whether the 
matrix represent a magic square or not. 
4 15 14 1 
9 6 7 12 
5 10 11 8 
16 3 2 13

Introduction to Algorithm

  • 1.
    Algorithms Introduction By: Dr. Ghulam Rasool
  • 2.
    Algorithm • Analgorithm is a well-defined list of steps for solving a particular problem • The efficiency of an algorithm depends on space and time
  • 3.
    Insertion into anarray Insert(A,N,K, item) • A is array with N elements and K is a +ve integer such that K<=N. This algorithm inserts an item into kth location in array A 1 Set J=N 2 Repeat steps 3 and 4 while J>=K 3 Set A[J+1]= A[j] 4 Set J=J-1 5 Set A[k]= item 6 Set N=N+1 7 Exit
  • 4.
    Deletion from arrayDelete(A, N, K, item) 1 Set item =A[K] 2 Repeat for j= K to N-1 Set A[j]= A[J+1] 3 Set N=N-1 4 Exit Matrix Multiplication(A,B,C, M, N, P) 1 Repeat steps 2 to 4 for i = 1 to M 2 Repeat steps 3 to 4 for j= 1 to N 3 Set C[i][j]= 0 4 Repeat for k= 1 to P Set C[i][j]= C[i][j]+ A[i][k] * B[k][j] 5 Exit
  • 5.
    Assignment1b • Amagic square is a square matrix of integers such that sum of every row, the sum of every column and sum of each of the diagonal are equal. For example • Write an algorithm to read set of integers for square matrix and then decide whether the matrix represent a magic square or not. 4 15 14 1 9 6 7 12 5 10 11 8 16 3 2 13