List of Practicals of Algorithm Semester-1 Part 2
List of Practicals of Algorithm Semester-1 Part 2
#include <stdio.h>
#include <conio.h>
int main()
struct student
char name[80];
float salary;
char DOB[80];
};
scanf("%s", stud1.name);
scanf("%f", &stud1.salary);
scanf("%s", stud1.DOB);
getch();
return 0;
}
Bubble sort
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
int arr[5]={25,7,31,13,2};
int i, j, temp;
system ("cls");
printf("Bubble sort.\n\n");
printf("%d\t",arr[i]);
if(arr[j]>arr[j+1])
{
temp= arr[j];
arr[j]= arr[j+1];
arr[j+1]= temp;
printf("\n");
printf("%d\t",arr[i]);
return 0;
}
Selection sort
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
int arr[5]={25,17,31,2};
int i,j,temp;
printf("Selection Sort.\n\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
for(j=i+1;j<=4;j++)
{
if(arr[i]>arr[j])
temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
printf("\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
return 0;
}
Insertion Sort
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
int arr[5]={1,2,3,4,5};
int i,j,k,temp;
printf("Insertion sort.\n\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=1;i<=4;i++)
for(j=0;j<i;j++)
if(arr[j]>arr[i])
temp=arr[j];
arr[j]=arr[i];
for(k=i;k>j;k--)
arr[k]=arr[k-1];
arr[k+1]=temp;
printf("\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
return 0;
}
Linear Search
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
int arr[10]={11,2,9,4,5,6,7,8,9,10};
int i,num;
scanf("%d",&num);
for(i=0;i<=9;i++)
if(arr[i]==num)
break;
if(i==10)
else
return 0;
}
Binary Search
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
int arr[10]={1,2,3,4,5,6,7,8,9,90};
int mid,lower=0,upper=9,num,flag=1;
scanf("%d",&num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower-upper)/2)
if(arr[mid]==num)
flag=0;
break;
if(arr[mid]>num)
upper=mid-1;
else
lower=mid+1;
if(flag)
return 0;
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int i;
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
scanf("%s", s[i].firstName);
scanf("%f", &s[i].marks);
printf("Displaying Information:\n\n");
puts(s[i].firstName);
printf("\n");
return 0;
}
Accessing an array using malloc
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
scanf("%d",&n);
if (ptr == NULL) {
exit(0);
else {
ptr[i] = i + 1;
}
return 0;
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
int* ptr;
int n, i;
n = 5;
if (ptr == NULL) {
exit(0);
else {
ptr[i] = i + 1;
return 0;
#include<stdio.h>
#include<conio.h>
struct Date
{
int mm,dd,yy;
};
struct Employee
char name[50];
int emp_id;
};
main()
{ int i;
struct Employee e;
clrscr();
scanf("%s",&e.name);
scanf("%d",&e.emp_id);
printf("(dd-mm-yy) : ");
scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
printf("(dd-mm-yy) : ");
scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
printf("\nName : %s",e.name);
printf("\nEmployee ID : %d",e.emp_id);
getch();
#include <stdio.h>
#include <stdlib.h>
int main()
int index = 0, i = 0, n,
int ans;
marks = (int*)malloc(sizeof(
// malloc or not?
if (marks == NULL) {
}
else {
"using malloc\n");
do {
scanf("%d", &ans);
if (ans == 1) {
index++;
marks = (int*)realloc(
marks,
(index + 1)
* sizeof(
if (marks == NULL) {
else {
printf(
///beginning address of
///allocated memory
marks[i]);
free(marks);
return 0;
}
#include <stdio.h>
#include<conio.h>
struct marks
int total;
};
main()
{
int i; //Declaring and initializing array of structures of type 'marks' . . . .
struct marks totals; // One more structure variable 'totals' to store totals
printf("\nstudent total\n\n");
printf("%s\t %d\n %s\t %d\n %s\t %d\n", "subject1", totals.sub1, "subject2", totals.sub2, "subject3",
totals.sub3);
}
d) Thus, free command is very important to de-allocate the memory from all the 3 previous programs.
b. Write a program to insert data at the end of the array.
#include <stdio.h>
void main()
scanf("%d", &n);
arr[n];
scanf("%d",&arr[i]);
scanf("%d", &value);
arr[n] = value;
printf("%d \t",arr[i]);
}
}
#include <stdio.h>
void main()
scanf("%d", &n);
arr[n];
scanf("%d",&arr[i]);
scanf("%d", &value);
arr[0] = value;
printf("%d \t",arr[i]);
}
Write a program to insert data after any particular location of an array.
#include<stdio.h>
#define size 5
int main()
scanf("%d%d",&pos,&element);
if(pos <= size && pos >= 0)
arr[i] = arr[i-1];
arr[pos] = element;
else
printf("Invalid Position\n");
return 0;
}
Write a program to delete data after any particular location of an array.
#include<stdio.h>
void main()
int a[100],i,n,pos;
printf("\nEnter no of elements\n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
scanf("%d",&pos);
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
n=n-1;
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
}
Write a program to delete data at the end of the array.
#include <stdio.h>
void main()
scanf("%d", &n);
int arr[n];
scanf("%d",&arr[i]);
printf("%d \t",arr[i]);
}
5: d)Write a program to delete data at the beginning of the array.
Doubt