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
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
the resource or guide to learn data structures and algorithms
I will share with you the website that helped me to effortlessly understand the data structures and algorithms, believe me, i found, this is the best website to understand the data structures and algorithms concepts online.
What is a GeeksforGeeks website all about?
It is a website that teaches computer science subjects like data structures, algorithms and one of the greatest website to learn data structure and algorithm
The books I bought to learn data structures and algorithms are
- Data Structures by Seymour Lipschutz
- Data Structures using c by Reema Thareja
I feel after studying both the books that the books are not enough to clear the concept
FOR EXAMPLE: IF YOU READ REEMA THAREJA BOOK, SHE EXPLAINED THE CONCEPT OF LINKED LIST IN ONE LONG PROGRAM WITH ALL ITS OPERATION LIKE TRAVERSING, INSERTION, DELETION, SEARCHING, BECAUSE OF THAT, IT BECOMES DIFFICULT TO UNDERSTAND THE CONCEPT
when you don’t know how to create a linked list?, then what’s the point of learning traversing linked list algorithm
things have to be learned in a logical order to remember otherwise it’s just a waste of time and effort.
If you read the linked list concept at GeeksforGeeks the explanation is in logical order like, what is a linked list?
Why we use a linked list? How the linked list is represented? What is the syntax of a node? how to create a linked list with 3 nodes
he explained, how to create a linked list with pictorial representation like how to insert an element in the node
on the other hand Reema Theraja book have algorithms without having a clear explanation of how to create a linked list first, things have to be in order to understand .
when we don’t understand the concept from the book
we usually choose alternative resource as youtube but videos I found on youtube is not up marked, so far I found GeeksforGeeks is the best resource to learn data structures.
GeeksforGeeks website is a good resource to learn data structures and algorithms for free.
Leave a Reply