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

Ds 2

The document contains algorithms and programs for various sorting and searching techniques: 1. The first program implements a sparse matrix and checks if a matrix is sparse by counting the number of zero elements. 2. The second program performs bubble sort on an input array. 3. The third program performs linear search to find an element in an array. 4. Further programs implement merge sort, quick sort to sort arrays.

Uploaded by

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

Ds 2

The document contains algorithms and programs for various sorting and searching techniques: 1. The first program implements a sparse matrix and checks if a matrix is sparse by counting the number of zero elements. 2. The second program performs bubble sort on an input array. 3. The third program performs linear search to find an element in an array. 4. Further programs implement merge sort, quick sort to sort arrays.

Uploaded by

Achint Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Program No 3

Aim – Write A program to implement sparse matrix & check whether it is sparse or not.
ALGORITHM
1. First create an array a [ 10 ] [ 10 ] , i ,j , m , n & the program takes the number of rows and columns of the
matrix.
2. Then the elements are entered.
3. If the matrix contains maximum number of elements as 0, then it is a sparse matrix.
4. Else not.
5. The result is printed.
6. Exit.

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
static int a[10][10];
int i,j,m,n;
int count=0;
clrscr();
printf("Enter the order of the matrix \n");
scanf("%d%d",&m,&n);
printf("Enter the co-effients of the matrix \n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a [ i ] [ j ] );
if(a [ i ] [ j ] ==0)
{
++count;
}
}
}
printf("\n Matrix is :-\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
if(count > ((m*n)/2))
{
printf("The given matrix is sparse matrix \n");
}
else
{
printf("The given matrix is not a sparse matrix \n");
}
printf("There are %d number of zeros",count);
getch();
}
OUTPUT
Enter the order of the matrix
3
3
Enter the co-effients of the matrix
1
2
0
3
6
7
0
2
7

Matrix is :-
1 2 0
3 6 7
0 2 7
The given matrix is not a sparse matrix
There are 2 number of zeros
Program No 12
Aim- Program to implement bubble sorting
ALGORITHM
1. Input array
2. BubbleSort()
2.1 for i=0 till i<size, i++
for j=0 till j<size-1, j++
if AR[j]>AR[j+1]
tmp=AR[j]
AR[j]=AR[j+1]
AR[j+1]=tmp;
print array

PROGRAM
#include<stdio.h>
#include<conio.h>
void BubbleSort(int[],int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n How many elements do you want to add ");
scanf("%d",&N);
printf("\n Enter Array elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
BubbleSort(AR,N);
printf("\n\n The sorted array is ");
for(i=0;i<N;i++)
{
printf("\t%d",AR[i]);
}
getch();
}

void BubbleSort(int AR[],int size)


{
int tmp,i,j,k;
for(i=0;i<size;i++)
{
for(j=0;j<(size-1)-i;j++)
{
if(AR[j]>AR[j+1])
{
tmp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=tmp;
}
}
printf("\n Array after iteration %d is : ",i+1);
for(k=0;k<size;k++)
{
printf("\t%d",AR[k]);
}
}
}
Program No 13
Aim-Program to perform linear search
ALGORITHM
1. Input array
2. Lsearch()
2.1 compare array elements with item
2.2 if found the return i else -1
3. if index ==-1 not found
else found
PROGRAM

#include<stdio.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n Enter no of elements ");
scanf("%d",&N);
printf("\n Enter Array Elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
printf("\n Enter element to be searched ");
scanf("%d",&ITEM);
index=Lsearch(AR,N,ITEM);
if(index==-1)
printf("\n Element not found ");
else
printf("\n\n Element found at Position: %d ",index+1);
getch();
}
int Lsearch(int AR[],int size, int item)
{
int i;
for(i=0;i<size;i++)
{
if(AR[i]==item)
return i;
}
return -1;
}
Program No 14
Aim- Program to perform merge sort
ALGORITHM
1. input array
2. merge_sort()
2.1 if l<r
mid =(l+r)/2
merge_sort(arr,beg,mid)
merge_sort(arr,mid+1,end)
merge_sort(arr,beg,mid,end)
3. display sorted array
PROGRAM
#include<conio.h>
#include<stdio.h>
void merge(int a[],int, int, int);
void merge_sort(int a[],int, int);
void main()
{
int arr[10], i, n;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
merge_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}

void merge(int arr[],int beg, int mid, int end)


{
int i=beg, j=mid+1, index=beg, temp[10], k;
while((i<=mid) && (j<=end))
{
if(arr[i] < arr[j])
{
temp[index] = arr[i];
i++;
}
else
{
temp[index] = arr[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index] = arr[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
temp[index] = arr[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
arr[k] = temp[k];
}

void merge_sort(int arr[], int beg, int end)


{
int mid,j;
if(beg<end)
{
mid = (beg+end)/2;
merge_sort(arr, beg, mid);
merge_sort(arr, mid+1,end);
merge(arr, beg, mid,end);
}
}
Program No 15
Aim- Program to perform quick sort
ALGORITHM
1. input array
2. quick_sort()
if beg<end
loc=partion(a,beg,end)
quick_sort(a,beg,loc-1)
quick_sort(a,loc+1,end)
3. partition()
3.1 loc=left=beg
right=end
flag=0
while flag!=1
while a[loc] <= a[right] && loc!=right
right—
if loc==right
flag =1
else if a[loc]>a[right]
temp = a[loc]
a[loc] = a[right]
a[right] = temp
loc = right
if flag!=1
while a[loc] >= a[left] && loc!=left
left++
if loc==left
flag =1
else if a[loc] <a[left]
temp = a[loc]
a[loc] = a[left]
a[left] = temp
loc = left
3.2 return loc

PROGRAM
#include <stdio.h>
#include <conio.h>
#define size 100
int partition(int a[], int beg, int end);
void quick_sort(int a[], int beg, int end);
void main()
{
int arr[size], i, n;
printf("\n Enter number of elements ");
scanf("%d", &n);
printf("\n Enter the elements of the array ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
printf("\n The sorted array is: ");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}

int partition(int a[], int beg, int end)


{
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}

void quick_sort(int a[], int beg, int end)


{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quick_sort(a, beg, loc-1);
quick_sort(a, loc+1, end);
}
}

You might also like