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

DS AllLab Program

The document discusses programs for sparse matrix representation and transpose, insertion sort, quicksort, shellsort, and a stack program. The sparse matrix program reads in a matrix, determines if it is sparse, represents it sparsely, and finds the transpose. The sorting programs implement insertion sort, quicksort, and shellsort using functions and recursion. The stack program performs push, pop, and display operations on a stack.

Uploaded by

Jayanth P D
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

DS AllLab Program

The document discusses programs for sparse matrix representation and transpose, insertion sort, quicksort, shellsort, and a stack program. The sparse matrix program reads in a matrix, determines if it is sparse, represents it sparsely, and finds the transpose. The sorting programs implement insertion sort, quicksort, and shellsort using functions and recursion. The stack program performs push, pop, and display operations on a stack.

Uploaded by

Jayanth P D
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Write a program to check whether the given matrix is sparse or not and represent
the matrix in sparse representation and determine the transpose of sparse
representation.

/PROGRAM FOR SPARSE MATRIX AND TRANSPOSE


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],s[10][10],i,j,m,n,nz=0,z=0,r=0,t;
clrscr();
printf("enter no of rows and cols\n");
scanf("%d%d",&m,&n);
printf("enter matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==0)
z++;
else
nz++;
}
}
if(nz>z)
printf("not a sparse matrix\n");
else
{
printf("it is a sparse matrix\n");
//finding sparse matrix representation
s[r][0]=m;
s[r][1]=n;
s[r][2]=nz;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
r++;
s[r][0]=i;
s[r][1]=j;
s[r][2]=a[i][j];
}
}
}
printf("sparse matrix representation is\n");
for(i=0;i<=r;i++)//always one extra row
{
for(j=0;j<3;j++) //always three coloumns
{
printf("%d ",s[i][j]);
}
printf("\n");
}

//finding transpose ->will have only 3 cols,


//just exchange 0th col and 1st col
for(i=0;i<=r;i++)
{
t=s[i][0];
s[i][0]=s[i][1];
s[i][1]=t;
}
printf("transpose is \n");
printf("sparse matrix representation is\n");
for(i=0;i<=r;i++)//always one extra row
{
for(j=0;j<3;j++) //always three coloumns
{
printf("%d ",s[i][j]);
}
printf("\n");
}
}
getch();
}

2. Write a Program to implement Insertion sort using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,p,a[20];
clrscr();
printf("\t\t\t\t---Insertion Sort---\n\n\n");
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
for(j=i-1;j>=0;j--)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("the sorted array\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

3. Write a Program to implement Quick sort using recursion.

#include<stdio.h>
#include<conio.h>
int number[25];
void quicksort(int first,int last)
{
int i,j,pivot,temp;
clrscr();
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while (number[i]<=number[pivot]&&i<last)
i++;
while (number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(first,j-1);
quicksort(j+1,last);
}
}
void main()
{
int i,n;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&number[i]);
}
quicksort(0,n-1);
printf("sorted array");
for(i=0;i<n;i++)
{
printf(" %d",number[i]);
}
getch();
}

4. Write a Program to implement Shell sort using functions.

#include<stdio.h>
#include<conio.h>
void shellsort(int a[], int num)
{
int gap,j,k,temp;
for(gap=num/2;gap>0;gap=gap/2)
{
for(j=gap;j<num;j++)
{
for(k=j-gap;k>=0;k=k-gap)
{
if(a[k+gap]>=a[k])
{
break;
}
else
{
temp=a[k];
a[k]=a[k+gap];
a[k+gap]=temp;
}
}
}
}
}
void main()
{
int a[20],k,i,num;
printf("Enter the number of elements\n");
scanf("%d",&num);
printf("Enter the elements\n");
for(k=0;k<num;k++)
{
scanf("%d",&a[k]);
}
shellsort(a,num);
printf("sorted array is\n");
for(k=0;i<num;k++)
{
printf("%d",a[k]);
}
getch();
}

5. Develop a program for STACK that performs following primitive operations: push,
pop and display

You might also like