Simple Algorithm Examples

To explain examples of algorithms I used C programming language, you can use your comfortable language

If you don’t know, what is an algorithm then click below link

Click here —>

#### Algorithm for the addition of array elements

``````Start
Step 1: initialize sum =0
Step 2: iterate loop from 0 to 9 and increment by 1
Step 3:add sum with each increment
Step 4: print sum
Stop``````

#### Implementation in C

``````#include <stdio.h>
int main()
{
int arr[10]={1,1,1,1,1,2};
int i, sum=0;
for(i=0; i<9; i++)
{
sum =sum+ arr[i];
}
printf("Sum= %d\n", sum);
return 0;
}``````

#### Output

``Sum = 7``

#### Algorithm for array traversal

``````step 1.[Initialize counter] set k=lower_bound
step 2.Repeat steps 3 and 4 while k<= Upper_bound
step 3.[visit element] Apply a process to A[k]
step 4.[Counter increment] set k=k+1
[End of loop]
step 5.Exit``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int k=0;
int A[9]={10,20,30,40,50,60,70,80,90};
while(k<=8)
{
printf("%d\n",A[k]);
k=k+1;
}
return 0;
}``````

#### Output

``10 20 30 40 50 60 70 80 90``

#### Algorithm to insert an element in an array

``````step 1.[Initialize counter] set  J=N-1
step 2.Repeat steps 3 and 4 while J>= pos
step 3.[move j th element down] set  A[J+1]= A[J]
step 4.[Counter Decrement] set J=J-1
[End of loop]
step 5.[Element insert] set  A[pos]=item
step 6.[Reset N] setN= N+1
step 7.Exit``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int j, pos=5,item=6, n=8, A[9]={1,2,3,4,5,7,8,9};
j=n-1;
while(j>=pos)
{
A[j+1]=A[j];
j=j-1;
}
A[pos]=item;
n=n+1;
for (j=0;j<n;j++)
printf("%d",A[j]);
return 0;
}``````

#### Output

``1 2 3 4 5 6 7 8 9``

#### Algorithm to Delete an element in an array

``````step 1.[Initialize counter] set  J=pos
step 2.Repeat steps 3 and 4 while J<= N-1
step 3.[move  j th  element up] set  A[J]= A[J+1]
step 4.[Counter Increment ] set J=J+1
[END of loop]
step 5.[Reset N] set N= N-1
step 6.Exit``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int j, pos=5, n=8, A[9]={1,2,3,4,5,7,8,9};
j=pos;
while(j<=n-1)
{
A[j]=A[j+1];
j=j+1;
}
n=n-1;
for (j=0;j<n;j++)
printf("%d",A[j]);
return 0;
}``````

#### Output

``1 2 3 4 5 8 9``

#### Algorithm for linear search

``````step 1.[enter item at the end of A] set  A[N+1]=item
step 2.[initialize counter] set pos=1
step 3.[search for item]
Repeat while A[pos] != item
Set pos=pos+1
[end of loop]
step 4.[successful] if pos =N+1 then set pos =0
step 5.exit``````

#### Implementation in C

``````# include <stdio.h>
int main()
{
int A[5]={4,5,6,8,9};
int n=5;
int item =6;
A[n+1]=item;
int pos=1;
while(A[pos]!=item)
{
pos=pos+1;
}
if (pos==n+1)
pos=0;
printf("pos=%d ",pos);
return 0;
}``````

#### Output

``pos=2``

#### Algorithm for finding the length of a string

``````step 1.[initialize counter] set  J=0
step 2.Repeat step3 while STR[J] != NULL
step 3.[increment the counter] J=J+1
[end of loop]
step 4.[Set length] length =J
step 5.exit``````

#### Implementation in C

``````#include <stdio.h>
int main()
{
char str[80]={"hi iam anees"};
int j=0, length;
while(str[j]!= '\0')
{
j++;
length =j;
}
printf("length = %d\n",length);
return 0;
}``````

#### Output

``length = 12``

#### Algorithm for Extracting a substring from  a string

``````step 1.[initialize counter] set i=pos, j=0
step 2.Repeat step 3 to 6
while STR[i] != NULL and N>0
step 3.Set subStr[j]=Str[i]
step 4.[increment the counter] i=i+1
step 5.[increment the counter] j=j+1
step 6.[Reset N] set N=N-1
[end of loop]
step 7.Set substr[j] =Null
step 8.exit``````

#### Implementation in C

``````# include<stdio.h>
int main()
{
char str[50]={"hi iam anees"};
char substr[50]={"anees"};
int i, j, m=6, n=5;
i=m;
j=0;
while (str[i]!='\0'&& n>0)
{
substr[j]=str[i];
i=i+1;
j=j+1;
n=n+1;
}
substr[j]='\0';
puts(substr);
return 0;
}``````

#### Output

``anees``

#### Algorithm for inserting a string in the main string

``````step 1.[initialize counter] set I=0, J=0 and K=0
step 2.Repeat step 3 to 4 while Data[J] != NULL
step 3.If  I =loc
Repeat while str[k]!=NULL
New_data[J]=str[K]
[increment the counter] J=J+1
[increment the counter] k=k+1
[end of  inner loop]
Else
New_data[J]=data[I]
[Increment counter] J=J+1
[End of if]
step 4.[increment counter] I=I+1
[End of outer loop]
step 5.Set  new_data[J]=NULL
step 6.Exit``````

#### Implementation in C

``````# include<stdio.h>
int main()
{
int loc=3, i=0, j=0, k=0;
char data[50]={"hi  anees"}, str[50]={"i am "},new_data[50];
while(data[i]!='\0')
{
if(i==loc)
{
while(str[k]!='\0')
{
new_data[j]=str[k];
j=j+1;
k=k+1;
}
}
else
{
new_data[j]=data[i];
j=j+1;
}
i=i+1;
}
new_data[j]='\0';
puts(new_data);
return 0;
}``````

#### Output

``hi iam anees``

#### Algorithm for Deleting  a substring from the main string

``````step 1. [initialize counter] set I=0, J=0
step 2.Repeat step 3 to 6 while Data[I] != NULL
step 3.If  I =loc
Repeat while N>0
[increment counter] I=I+1
[Reset N] N=N-1
[End of inner loop]
[End of if]
step 4.Set New_data[J]=data[I]
step 5.[increment the counter] J=J+1
step 6.[increment counter] I=I+1
[End of outer loop]
step 7. Set New_data[J]=NULL
step 8.Exit``````

#### Implementation in C

``````# include <stdio.h>
int main()
{
int loc=3,i,j,n=3;
char Data[50]={"hi iam anees"},new_Data[50]={"iam "};
i=0;
j=0;
while (Data[i]!='\0')
{
if(i==loc)
{
while(n>0)
{
i=i+1;
n=n-1;
}
}
new_Data[j]=Data[i];
j=j+1;
i=i+1;
}
new_Data[j]='\0';
puts(new_Data);
return 0;
}``````

#### Output

``hi anees``

#### Algorithm for linked list traversal (or) iteration

``````Step 1. [initialize pointer p] set p=start
Step 2. Repeat steps 3 and 4 while p!=NULL
Step 3.         Apply the process to p->data
Step 4.        [p now points to the next head] set p= p->next
Step 5.[End of loop]
Step 6.Exit``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node* head_node;
struct node* second_node;
struct node* third_node;
/*allocate memory to 3 nodes */
head_node =(struct node*)malloc(sizeof (struct node));
second_node =(struct node*)malloc(sizeof(struct node));
third_node = (struct node*)malloc(sizeof(struct node));
head_node->data=1;/*insert data into the head_node */
head_node->next=second_node;/* link the head_node with second_node*/
second_node->data=2;
second_node->next=third_node;
third_node->data=3;
third_node->next=NULL;
struct node *p =head_node;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return 0;
}``````

#### Output

``1 2 3``

#### Algorithm for searching a linked list

``````Step 1: [initialize pointer p] set p =start
Step 2: Repeat steps 3 and 4 (while p!=NULL)
Step 3: if(item ==p->data)then return (true) :- exit
Step 4:p=p->next
[End of both if and while loops]
Step 5: exit``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}* head_node;
void create_linkedlist (int A[],int n)/* function to create linked ist */
{
int i;
struct node *temp, *p;/* first node creation */
head_node =(struct node*)malloc(sizeof(struct node));
head_node->data=A[0];
head_node->next=NULL;
p=head_node;
for(i=0;i<n;i++)/*rest of the node's created by using loop*/
{
temp =(struct node*)malloc(sizeof(struct node));
temp->data=A[i];
temp->next=NULL;
p->next=temp;
p=temp;
}
}
void search_linkedlist(struct node *p, int item)
{
while(p!=NULL)
{
if(p->data==item)
{
printf("found item");
exit(0);
}
p=p->next;
}
printf("not found item");
}
int main()
{
int A[]={10,20,30,40,50,60,70,80,90};
create_linkedlist(A,8);/*function call*/
search_linkedlist(head_node, 10);/* function call*/
return 0;
}``````

#### Output

``found item``

#### Algorithm for counting no of elements in a linked list

``````Step 1.[initializes counter] set N=0
Step 2.[initializes pointer] set p=start
Step 3.Repeat steps  4 and 5 (while p!=NULL)
Step 4.[increment counter] set N=N+1
Step 5.[update pointer] p=p->next;
[End of loop]
Step 6. Return``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}* head_node;
void create_linkedlist (int A[],int n)/* function to create linked ist */
{
int i;
struct node *temp, *p;/* first node creation */
head_node =(struct node*)malloc(sizeof(struct node));
head_node->data=A[0];
head_node->next=NULL;
p=head_node;
for(i=1;i<n;i++)/*rest of the node's created by using loop*/
{
temp =(struct node*)malloc(sizeof(struct node));
temp->data=A[i];
temp->next=NULL;
p->next=temp;
p=temp;
}
}
void count_linkedlist(struct node *p)
{
int n;
n=0;
while(p!=NULL)
{
n=n+1;
p=p->next;
}
{
printf("no of elements =%d",n);
}
}
int main()
{
int A[]={10,20,30,40,50,60,70,80,90};
create_linkedlist(A,8);/*function call*/
count_linkedlist(head_node);/* function call*/
return 0;
}``````

#### Output

``no of elements =9``

#### Algorithm for the sum of elements in a LinkedList

``````Step  1.[initialize pointer] set p= head node
Step  2.set sum=0
Step  3.Repeat steps 4 and 5 (while p!=NULL)
Step  4. Set sum=sum+p->data;
Step  5. P=p->next;
[End of loop]
Step 6. Return sum``````

#### Implementation in C

``````#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}* head_node=NULL;
void create_linkedlist (int A[],int n)/* function to create linked ist */
{
int i;
struct node *temp, *p;/* first node creation */
head_node =(struct node*)malloc(sizeof(struct node));
head_node->data=A[0];
head_node->next=NULL;
p=head_node;
for(i=1;i<n;i++)/*rest of the node's created by using loop*/
{
temp =(struct node*)malloc(sizeof(struct node));
temp->data=A[i];
temp->next=NULL;
p->next=temp;
p=temp;
}
}
void sum_linkedlist(struct node *p)
{
int sum;
sum=0;
while(p!=NULL)
{
sum=sum+p->data;
p=p->next;
}
{
printf("sum of elements =%d",sum);
}
}
int main()
{
int A[]={10,20,30,40,50,60,70,80};
create_linkedlist(A,8);/*function call*/
sum_linkedlist(head_node);/* function call*/
return 0;
}``````

#### Output

``sum of elements =360``

#### Resource

• https://www.udemy.com/course/datastructurescncpp/

### Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up