👉 S.Y.B.C.S .DS PROGRAM SLIPS 👈

Data structure Programs

0 1,199

 

 

 

 

 

 

1 .Sort a random array of integers (accpet all string the value of  from user)is acending order by using bubble sort alorithem…

#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]);

}

}

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);

}

 

Slip 2 : 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);

}

 

Slip3 : 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);

}

 

Slip 4 : Read the data from the file “employee.txt” and sort on age using Bubble sort. Reading the contents of File & Display Function

#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);

}

 

 

Slip 5  :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  …

Bubble sort Function

#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);

}

Slip 6 : 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 .

   2.Insertion sort Function  .

#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);

}

 

. Slip  7 : 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

   Merge Sort Function

#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]);

}

 

Slip 8 : 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

   Quick Sort Function

#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]);

}

 

Slip 9  : 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 ..

   Merge sort Function

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);

}

 

Slip10  : 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  ..                        

 

Quick sort Function

 

#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);

}

Leave A Reply

Your email address will not be published.