0% found this document useful (0 votes)
16 views

Practicalsolution

The document contains code snippets for sorting arrays and employee data from a file using different sorting algorithms like bubble sort, insertion sort. It includes functions for reading data from a file, sorting arrays or structures using the algorithms and writing the sorted data back to a file.

Uploaded by

Shweta Vardekar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Practicalsolution

The document contains code snippets for sorting arrays and employee data from a file using different sorting algorithms like bubble sort, insertion sort. It includes functions for reading data from a file, sorting arrays or structures using the algorithms and writing the sorted data back to a file.

Uploaded by

Shweta Vardekar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 45

1.

Slip 1_1 : Sort a random array of n integers (accept the value of n from user) in ascending order

by using bubble sort algorithm.

#include<stdio.h>

void bubble(int a[20],int n)

int i,j,temp;

for(i=1;i<n;i++)

for(j=0;j<n-i;j++)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

void generate(int a[20],int n)

int i;

for(i=0;i<n;i++)
a[i]=rand()%1000;

void display(int a[],int n)

{ int i;

for(i=0;i<n;i++)

printf("%d\t",a[i]);

void main()

int a[20],i,j,n;

printf("\nenter how many elemants:");

scanf("%d",&n);

generate(a,n);

printf("\n elements are:\n");

display(a,n);

bubble(a,n);

printf("\n after sorting elements are :\n");

display(a,n);

********************************************************

BY using 2nd method====

#include <stdio.h>
#define MAXSIZE 10

void main()

int array[MAXSIZE];

int i, j, num, temp;

printf("Enter the value of num \n");

scanf("%d", &num);

printf("Enter the elements one by one \n");

for (i = 0; i < num; i++)

scanf("%d", &array[i]);

printf("Input array is \n");

for (i = 0; i < num; i++)

printf("%d\n", array[i]);

/* Bubble sorting begins */

for (i = 0; i < num; i++)

for (j = 0; j < (num - i - 1); j++)

if (array[j] > array[j + 1])


{

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

printf("Sorted array is...\n");

for (i = 0; i < num; i++)

printf("%d\n", array[i]);

********************************************************

2. Slip 2_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using insertion sort algorithm.*

#include<stdio.h>

void insertion(int a[10],int n)

int i,j,key;

for(i=1;i<n;i++)

{
key=a[i];

for(j=i-1;j>=0 ;j--)

if(key<a[j])

a[j+1]=a[j]; //shifting

else break;

a[j+1]=key;

void generate(int a[20],int n)

int i;

for(i=0;i<n;i++)

a[i]=rand()%2000;

void display(int a[],int n)

{ int i;

for(i=0;i<n;i++)

printf("%d\t",a[i]);

}
main()

int a[20],i,j,n;

printf("\n Enter how many elemants:");

scanf("%d",&n);

generate(a,n);

printf("\n Elements are:\n");

display(a,n);

insertion(a,n);

printf("\n After sorting elements are :\n");

display(a,n);

********************************************************

3. Slip3_1 : Read the data from the file “employee.txt” and sort on age using insertion sort. Reading &
Display the file Insertion sort Function

#include<stdio.h>

typedef struct employee

int age;

char name[10];

}record;

record employee[100];
int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;

return (i-1);

void writefile(record *a,int n)

int i=0;

FILE *fp;

if((fp=fopen("sorted_on_age_emp.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

}}

void insertion(record *a,int n)

{
int i,j;

record t;

for(i=1;i<n;i++)

t=a[i];

for(j=i-1;j>=0 && a[j].age>t.age;j--)

a[j+1]=a[j];

a[j]=t;

main()

int n;

n=readfile(employee);

insertion(employee,n);

writefile(employee,n);

Email This

BlogThis!

Share to Twitter

Share to Facebook
Share to Pinterest

**************************************

4. Slip 4_1: Read the data from the file “employee.txt” and sort on age using Bubble sort.

Reading the contents of File & Display Function [10]

Bubble sort Function [5]

#include<stdio.h>

typedef struct employee

char name[10];

int age;

}record;

record employee[100];

int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

{while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;
}

return(i-1);

void writefile(record *a,int n)

int i=0;

FILE *fp;

if((fp=fopen("sorted_emp_on_age.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

void bubble_sort(record *a,int n)

int i,j; record t;

for(i=1;i<n;i++)

for(j=0;j<n-i;j++)

if(a[j].age>a[j+1].age)

{
t=a[j];

a[j]=a[j+1];

a[j+1]=t;

main()

int n;

n=readfile(employee);

bubble_sort(employee,n);

writefile(employee,n);

Email This

BlogThis!

Share to Twitter

Share to Facebook

Share to Pinterest

**************************************

5. Slip 5_1 :Read the data from the file “employee.txt” and sort on names in alphabetical order (use
strcmp)
using bubble sort

Reading the contents of File & Display Function [10]

Bubble sort Function [5]

#include<stdio.h>

typedef struct employee

char name[10];

int age;

}record;

record employee[100];

int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

{while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;

}}

return(i-1);

}
void writefile(record *a,int n)

int i=0;

FILE *fp;

if((fp=fopen("sorted_emp_on_name_bubble.txt","w+"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

void bubble_sort(record *a,int n)

int i,j; record t;

for(i=1;i<n;i++)

for(j=0;j<n-i;j++)

if(strcmp(a[j].name,a[j+1].name)>=0)

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}
}

main()

int n;

n=readfile(employee);

bubble_sort(employee,n);

writefile(employee,n);

Email This

BlogThis!

Share to Twitter

Share to Facebook

Share to Pinterest

******************************************

6. Slip6_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use
using Insertion sort

1.Reading the contents of File & Display Function [10]

2.Insertion sort Function [5]

#include<stdio.h>

typedef struct employee

{
int age; char name[10];

}record;

record employee[50];

int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;

}return (i-1);

void writefile(record *a,int n)

int i;

FILE *fp;

if((fp=fopen("sorted_on_name_insertion.txt","w"))!=NULL)

{
for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

void insertion(record *a,int n)

int i,j;

record t;

for(i=1;i<n;i++)

t=a[i];

for(j=i-1;j>=0 && (strcmp(a[j].name,t.name)>=0);j--)

a[j+1]=a[j];

a[j]=t;

main()

int n;
n=readfile(employee);

insertion(employee,n);

writefile(employee,n);

Email This

BlogThis!

Share to Twitter

Share to Facebook

Share to Pinterest

***********************

7. Slip 7_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using a

recursive Merge sort algorithm.

Accept & Display Function [8]

Merge Sort Function [7]

#include<stdio.h>

merge(int a[10],int l,int m,int u)

int c[10],i,j,k;

i=l;

j=m+1;

k=0;

while(i<=m && j<=u)


{

if(a[i]<a[j])

c[k]=a[i];

k++;i++;

else

c[k]=a[j];

k++;j++;

while(i<=m)

c[k]=a[i];

i++;k++;

while(j<=u)

c[k]=a[j];

k++;j++;

for(i=l,j=0;i<=u;i++,j++)

a[i]=c[j];

}
void generate(int a[10],int n)

int i;

for(i=0;i<n;i++)

a[i]=rand()%10;

merge_sort(int a[10],int i,int j)

int k=0;

if(i<j)

k=(i+j)/2;

merge_sort(a,i,k);

merge_sort(a,k+1,j);

merge(a,i,k,j);

main()

int i,n,a[10];

printf("how many elements:");

scanf("%d",&n);
generate(a,n);

printf("elements are:\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

merge_sort(a,0,n-1);

printf("\nafter sorting:\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

*******************************

8. Slip 8_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using

recursive Quick sort algorithm.

Accept & Display Function [8]

Quick Sort Function [7]

#include<stdio.h>

enum bool {false,true};

void disp(int a[],int l,int u)

int i;

for(i=l;i<=u;i++)

printf("%d\t",a[i]);

}
void quick(int a[],int l,int u)

int temp,piv,left,right;

enum bool pivot_places=false;

left=l;

right=u;

piv=l;

if(l>=u)

return;

printf("\nsublist:\n");

disp(a,l,u);

while(pivot_places==false)

while(a[piv]<=a[right] && piv!=right)

right--;

if(piv==right)

pivot_places=true;

if(a[piv]>a[right])

temp=a[piv];

a[piv]=a[right];

a[right]=temp;

piv=right;

while(a[piv]>=a[left] && piv!=left)


left++;

if(piv==left)

pivot_places=true;

if(a[piv]<a[left])

temp=a[piv];

a[piv]=a[left];

a[left]=temp;

piv=left;

disp(a,l,u);

quick(a,l,piv-1);

quick(a,piv+1,u);

void generate(int a[],int n)

int i;

for(i=0;i<n;i++)

a[i]=rand()%20;

main()

{
int a[10],n,i;

printf("how many elements:");

scanf("%d",&n);

generate(a,n);

printf("\nelements are:");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

quick(a,0,n-1);

printf("\nafter sorting:\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

*******************************************

9. Slip 9_1 :. Read the data from the „employee.txt‟ file and sort on age using Merge sort and write the
sorted

data to another file 'sorted_emp_on_age.txt'.

Reading & Display the file [10]

Merge sort Function [5]

include<stdio.h>

typedef struct employee

char name[10];

int age;
}record;

record employee[100];

int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

{while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;

}}

return(i-1);

void writefile(record *a,int n)

int i=0;

FILE *fp;

if((fp=fopen("sorted_emp_on_age_merge.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

}}
merge(record *a,int l,int m,int u)

record c[10]; int i,j,k;

i=l;

j=m+1;

k=0;

while(i<=m && j<=u)

if(a[i].age<a[j].age)

c[k]=a[i];

k++;i++;

else

c[k]=a[j];

k++;j++;

while(i<=m)

c[k]=a[i];

i++;k++;

while(j<=u)
{

c[k]=a[j];

k++;j++;

for(i=l,j=0;i<=u;i++,j++)

a[i]=c[j];

merge_sort(record *a,int i,int j)

int k=0;

if(i<j)

k=(i+j)/2;

merge_sort(a,i,k);

merge_sort(a,k+1,j);

merge(a,i,k,j);

main()

int n;

n=readfile(employee);

merge_sort(employee,0,n-1);
writefile(employee,n);

*************************************

10.Slip10_1 : Read the data from the "employee.txt‟ file and sort on age using Quick sort and write the
sorted

data to another file 'sortedemponage.txt'.

Reading & Display the file [10]

Quick sort Function [5]

#include<stdio.h>

enum bool {false,true};

typedef struct employee

int age; char name[10];

}record;

record employee[50];

int readfile(record *a)

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

{
while(!feof(fp))

fscanf(fp,"%d%s",&a[i].age,a[i].name);

i++;

}return (i-1);

void writefile(record *a,int n)

int i;

FILE *fp;

if((fp=fopen("sorted_on_age_quick.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);

//enum bool {false,true};

void quick(record *a,int l,int u)

record temp;int piv,left,right;

enum bool pivot_places=false;


left=l;

right=u;

piv=l;

if(l>=u)

return;

/*

printf("\nsublist:\n");

disp(a,l,u);*/

while(pivot_places==false)

while(a[piv].age<=a[right].age && piv!=right)

right--;

if(piv==right)

pivot_places=true;

if(a[piv].age>a[right].age)

temp=a[piv];

a[piv]=a[right];

a[right]=temp;

piv=right;

while(a[piv].age>=a[left].age && piv!=left)

left++;

if(piv==left)

pivot_places=true;
if(a[piv].age<a[left].age)

temp=a[piv];

a[piv]=a[left];

a[left]=temp;

piv=left;

//disp(a,l,u);

quick(a,l,piv-1);

quick(a,piv+1,u);

main()

int n;

n=readfile(employee);

quick(employee,0,n-1);

writefile(employee,n);

*****************************************

11. Slip11_1 : Create a random array of n integers. Accept a value x from user and use linear search
algorithm to

check whether the number is present in the array or not and output the position if the number is
present.

Accept & Display Function [8]


Linear Search Function [7]

#include<stdio.h>

int linear_search(int a[],int n,int sr)

int i;

for(i=0;i<n;i++)

if(a[i]==sr)

return i;

return -1;

void generate(int a[],int n)

int i;

for(i=0;i<n;i++)

a[i]=rand()%20;

void display(int a[],int n)

{ int i;

for(i=0;i<n;i++)

{
printf("%d\t",a[i]);

main()

int a[20],i,j,n,x,ans;

printf("\n Enter how many elemants:");

scanf("%d",&n);

generate(a,n);

printf("\n Elements are:\n");

display(a,n);

printf("\n Enter searching element : ");

scanf("%d",&x);

ans=linear_search(a,n,x);

if(ans==-1)

printf("\n %d is NOT found.",x);

else

printf("\n %d is found at %d position\n",x,ans+1);

***************************

12. Slip12_1 : Read the data from file 'cities.txt' containing names of 10 cities and their STD codes.

Accept a name of the city from user and use linear search algorithm to check whether the name is
present in the file and output the STD code, otherwise output “city not in the list”.

Accept & Display Function [8]

Linear Search Function [7]


#include<stdio.h>

#include<string.h>

typedef struct city

char name[20];

int code;

}record;

record city[100];

int read_file(record *a)

int i=0;

FILE *fp;

if((fp=fopen("cities.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name,&a[i].code);

i++;

}}

return (i-1);

void l_search(record *a,int n,char x[20])


{

int i;

for(i=0;i<n;i++)

if(strcmp(a[i].name,x)==0)

printf("\n%s=%d\n",a[i].name,a[i].code);

break;

if(i==n)

printf("\ncity not found\n");

main()

char x[20];

int n;

n=read_file(city);

printf("\nenter city name\n");

gets(x);

l_search(city,n,x);

**********************************

13. Slip13_1 : Read the data from file "sortedcities.txt" containing names of 10 cities and their STD
codes. Accept a name of the city from user and use binary search algorithm to check whether the name
is present in the file and output the STD code, otherwise output “city not in the list”.

Accept & Display Function [8]

Binary Search Function

#include<stdio.h>

#include<string.h>

typedef struct city

char name[30];

int code;

}record;

record city[100];

int read_file(record *a)

int i=0;

FILE *fp;

if((fp=fopen("sortedcities.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name,&a[i].code);

i++;

return (i-1);
}

void write_file(record *a,int n)

int i=0;

FILE *fp;

if((fp=fopen("sorted_cities.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"\n%s\t%d",a[i].name,a[i].code);

void sort(record *a,int n)

int i,j;

record t;

for(i=1;i<n;i++)

for(j=0;j<n-i;j++)

if(strcmp(a[j].name,a[j+1].name)>0)

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}
}

int read_file1(record *a)

int i=0;

FILE *fp;

if((fp=fopen("sorted_cities.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name,&a[i].code);

i++;

return (i-1);

void b_search(record *a,int n,char key[20])

int l,h,mid;

l=0;

h=n-1;

while(h>=l)

mid=(l+h)/2;

if(strcmp(key,a[mid].name)==0)

printf("\nSTD code:%d\n ",a[mid].code);

break;
}

else if(strcmp(key,a[mid].name)<0)

h=mid-1;

else

l=mid+1;

if(h<l)

printf("\ncity not in list\n");

main()

char key[20];

int n,m;

n=read_file(city);

sort(city,n);

write_file(city,n);

printf("\nenter city name\n");

scanf("%s",key);

b_search(city,n,key);

*****************************

14. Slip14_1 : Implement a stack library (stack.h) of integers using a static implementation of the stack
and implementing the Init(),Push(),Pop(),IsEmpty()& IsFull() these operations. Write a Menu driven
program that includes stack library and calls different stack operations Push & Pop Function each carries
Init Function IsEmpty Function & IsFull Function each carries 2 marks.
#include<stdio.h>

#include"stack.h"

main()

int n,i=0,ch;

init();

do

printf("\nchoices are:\n1:push into stack\n2:pop from stack\n3:check whether stack is empty or


not\n4:check whether stack is full or not\n5:peek(top) element of stack\n6:display elements of stack\
n7:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1: printf("\nenter element:");

scanf("%d",&n);

push(n);

break;

case 2:

printf("\ndeleted elements is:%d",pop());

break;

case 3: i=isempty();

if(i==1)

printf("\nstack is empty");

else
printf("\nstack is not empty");

break;

case 4: i=isfull();

if(i==1)

printf("\nstack is full");

else

printf("\nstack is not full");

break;

case 5: printf("\ntop element of stack is:%d",peek());

break;

case 6: display();

break;

case 7:exit(0);

}while(ch!=7);

***************************************************

16. Slip16_1 : Implement a list library (singlylist.h) for a singly linked list with the operations create,
Delete,display. Write a menu driven program to call these operations.

Create & Delete at front, Display Operation Carries 5 each Marks. [15]

#include<stdio.h>

#include<stdlib.h>

#include "singlylist.h"
main()

int n,ch,d;struct node *h;

do

printf("\nchoices are:\n1:Create Linked List\n2:Delete Linked List\n3:Display Linked


List\n3:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1: printf("\nEnter how many nodes :");

scanf("%d",&n);

h=create(n);

break;

case 2: printf("\n Enter position to be data deleted :");

scanf("%d",&d);

h=delete(h,d);

break;

case 3: display(h);

break; case 4: exit(0);

}while(ch!=4);

}
******************************

Slip18_1 : Implement a list library (doublylis.h) for a doubly linked list with the operations create,
Insert,display. Write a menu driven program to call these operations.

Create Insert at end, Display carries 5 mark each [15]

#include<stdio.h>

#include<stdlib.h>

#include "doublylist.h"

void main()

int n,ch,sr,d;

struct dnode *H;

do

printf("\nMENU\n");

printf("\n1.Create");

printf("\n2.Insert");

printf("\n3.Display");

printf("\n4.Exit\n");

printf("\nEnter Ur Choice\n");

scanf("%d",&ch);

switch(ch)

case 1:
printf("\nHow many nodes you want to \n");

scanf("%d",&n);

H=create(n);

break;

case 2: printf("\n Enter position to be data added :");

scanf("%d",&d);

H=insert(H,d);

break;

case 3:printf("\n Link list is :\n");

display(H);

break;

case 4: exit(0);

break;

default :printf("\n Invalid choice ");

}while(ch!=4);

***************************

25. Slip25_1 : Implement a list library (singlylist.h) for a singly linked list with the operations create,
Delete,display. Write a menu driven program to call these operations.

Create & Delete at front, Display Operation Carries 5 each Marks. [15]
#include<stdio.h>

#include<stdlib.h>

#include "singlylist.h"

main()

int n,ch,d;struct node *h;

do

printf("\nchoices are:\n1:Create Linked List\n2:Delete Linked List\n3:Display Linked


List\n3:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1: printf("\nEnter how many nodes :");

scanf("%d",&n);

h=create(n);

break;

case 2: printf("\n Enter position to be data deleted :");

scanf("%d",&d);

h=delete(h,d);

break;

case 3: display(h);
break;

case 4: exit(0);

}while(ch!=4);

************************

You might also like