0% found this document useful (0 votes)
18 views17 pages

Ds Practical To Print

Uploaded by

venilshah570
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Ds Practical To Print

Uploaded by

venilshah570
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DATA STRUCTURES

Practical:
22 Write a program to implement bubble sort.

#include<stdio.h>
#include<stdlib.h>

void main() {
int i, n;
printf("\n\t******** BUBBLE SORT ********");
printf("\n\n\tEnter number of elements: ");
scanf("%d",&n); int arr[n];

for(i=0;i<n;i++){ printf("\n\tEnter
element no. %d: ",i+1);
scanf("%d",&arr[i]);
}

bubble(arr, n);

printf("\n\n\tSorted elements using Bubble sort are: \n\n");


for(i=0;i<n;i++){ printf("\t%d",arr[i]);
}
getch();
}

int bubble(int arr[], int n){

int i, j, k, temp, pass=n;

for(j=0;j<n;j++){
printf("\n\tPass %d: ",j);
for(i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}

for(k=0;k<n;k++){
printf("\t%d",arr[k]);
}
}
}

35
DATA STRUCTURES

Output:

36
DATA STRUCTURES

Practical:
23 Write a program to implement selection sort.
#include<stdio.h>
#include<stdlib.h>

void main(){
int i,n;
printf("\n\t******** SELECTION SORT ********");
printf("\n\n\tEnter number of elements: ");
scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){
printf("\n\tEnter element no. %d: ",i+1);
scanf("%d",&arr[i]);
}

selection(arr, n);

printf("\n\n\tSorted elements using Selection sort are: \n\n");


for(i=0;i<n;i++){ printf("\t%d",arr[i]);
}
getch();
}

int selection(int arr[], int n){


int i,j,pass, temp, min_index;

for(pass=0;pass<n;pass++){
min_index=pass;
printf("\n\n\tPass %d :",pass);

for(i=pass+1;i<n;i++){
if(arr[i]<arr[min_index])
min_index=i;
}
temp=arr[pass];
arr[pass]=arr[min_index];
arr[min_index]= temp;

for(j=0;j<n;j++){
printf("\t%d",arr[j]);
}
}
}

37
DATA STRUCTURES

Output:

38
DATA STRUCTURES

Practical:
24 Write a program to implement quick sort.

#include<stdio.h>
#include<conio.h>

void create(int a[],int); void


display(int a[],int n); void
quicksort(int a[],int first,int last); int
main() { int a[10],n,first=0,last;
printf("\n enter size of array ");
scanf("%d",&n); printf("\n enter
array \n");

create(a,n); last=n-1; printf("\n


BEFORE SORTING ");
display(a,n); printf("\n AFTER
SORTING");
quicksort(a,first,last); display(a,n);

getch(); return
0;
}

void create(int a[],int n)


{
int i;
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void display(int a[],int n)


{
int i;
printf("ELEMNETS "); for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
}

void quicksort(int a[],int first,int last)

39
DATA STRUCTURES

{ int
i,j,k,temp,flag=1,key;

if(first<last)
{ i=first;
j=last+1;
key=a[first];
while(flag)

{ i=i+1;
while(a[i]<key)
i=i+1; j=j-1;
while(a[j]>key) j=j-
1; if(i<j) {
temp=a[i]; a[i]=a[j];
a[j]=temp; } else {
flag=0; } }
temp=a[first];
a[first]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}

40
DATA STRUCTURES

OUTPUT :
enter size of array 7

enter array
11
14
13
12 7
16
15

BEFORE SORTING ELEMNETS 11 14 13 12 7 16 15


AFTER SORTINGELEMNETS 7 11 12 13 14 15 16

41
DATA STRUCTURES

Practical: 25 Write a program to implement insertion sort.


#include<stdio.h>
#include<conio.h> #include<stdlib.h>

void main(){
int i,n;
printf("\n\t******** INSERTION SORT ********");
printf("\n\n\tEnter number of elements: ");
scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){
printf("\n\tEnter element no. %d: ",i+1);
scanf("%d",&arr[i]);
}

insertion(arr, n);

printf("\n\n\tSorted elements using Insertion sort are: \n\n");


for(i=0;i<n;i++){ printf("\t%d",arr[i]);
}
getch();
}

int insertion(int arr[],int n){


int i,j, temp;

for(i=1;i<n;i++){
j=i;
printf("\n\n\tTarget index %d :",i);
while(j>=1){ if(arr[j]<arr[j-1]){
temp=arr[j]; arr[j]=arr[j-1];
arr[j-1]=temp;
}
j--;
}
for(j=0;j<n;j++){
printf("\t%d",arr[j]);
}
}
}

42
DATA STRUCTURES

Output:

43
DATA STRUCTURES

Practical: 26 Write a program to implement merge sort.

#include<stdio.h>
#include<conio.h>

void create(int a[],int); void


display(int a[],int n); void
mergesort(int a[],int b[],int,int); int
main() { int a[10],b[10],n,m;
printf("\n enter size of 1st array ");
scanf("%d",&n); printf("\n enter
size of 2nd array ");
scanf("%d",&m);
printf("\n enter aray one 1 \n");

create(a,n); printf("\n enter


array 2\n"); create(b,m);

printf("\n BEFORE SORTING ");


display(a,n); display(b,m);
mergesort(a,b,n,m);

getch(); return
0;
}

void create(int a[],int n)


{
int i;
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void display(int a[],int n)


{
int i;
printf("ELEMNETS "); for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
}

44
DATA STRUCTURES

void mergesort(int a[],int b[],int n,int m)


{ int
c[30],i,j,k;
i=0; j=0;
k=0;

do
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;

k++;
}while(i<n && j<m);

while(i<n)
{
c[k]=a[i];
i++;
k++; }

while(j<m)
{
c[k]=b[j];
j++;
k++;
}
printf("\n AFTER SORTING :");
display(c,m+n);
}

45
DATA STRUCTURES

OUTPUT :
enter size of 1st array 5

enter size of 2nd array 4

enter aray one 1


11
33
55
22
88

enter array 2
30
40
20
60

BEFORE SORTING ELEMNETS 11 33 55 22 88 ELEMNETS 30 40 20 60


AFTER SORTING :ELEMNETS 11 30 33 40 20 55 22 60 88

46
DATA STRUCTURES

Practical: 27 Write a program to implement sequential search.


#include<stdio.h>
#include<conio.h>

int main()
{
int a[10],i,n,flag=0,key; printf("\n Enter
the Number less than 10 ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter A[%d]",i);
scanf("%d",&a[i]);
}
printf("\n\n Enter The Key Element to be search::");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element is Present at %d position",i+1);
flag=1;
}
}
if(flag==0) printf("\n Element Not
Found...");
getch();
return 0;
}

47
DATA STRUCTURES

OUTPUT :
Enter the Number less than 10 ::5
Enter A[0]10
Enter A[1]20
Enter A[2]40
Enter A[3]30
Enter A[4]50
Enter The Key Element to be search::40

Element is Present at 3 position

48
DATA STRUCTURES

Practical: 28 Write a program to implement binary search.


#include<stdio.h>
#include<conio.h>
#define size 10 void
sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\n After Sorting....\n\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);

int main()
{
int a[size],flag,key,pos,i,n; printf("\n Enter
the Number Of Elements:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter Elements A[%d]",i);
scanf("%d",&a[i]);
}
sort(a,n);
printf("\n\nEnter key element that you want to search\n");
scanf("%d",&key);
pos=bin_ser(a,key,n); if(pos==-1)
printf("\n\nElement Not Found\n");
else
printf("\n\nElement Found at %d Position\n",pos+1);
getch();
return 0;
}

49
DATA STRUCTURES

int bin_ser(int a[],int key,int n)


{
int high,low,mid;
high=n-1; low=0;
while(high>=low)
{
mid=(low+high)/2;
if(key==a[mid])
{
//printf("element found");
return mid;
}
else
{
if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
return -1;
}

50
DATA STRUCTURES

OUTPUT:
Enter the Number Of Elements:5

Enter Elements A[0]10


Enter Elements A[1]50
Enter Elements A[2]30
Enter Elements A[3]40
Enter Elements A[4]20

After Sorting....

10 20 30 40 50

Enter key element that uou want to search : 30

Element Found at 3 Position

51

You might also like