0% found this document useful (0 votes)
3 views2 pages

Algorithm Practical File-1

The document provides implementations of various sorting and searching algorithms, including Quick Sort, Heap Sort, Linear Search, and Binary Search. Each algorithm is presented with its respective code in C++. The document also mentions that outputs and sample runs can be included as screenshots.

Uploaded by

abhi8609073
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)
3 views2 pages

Algorithm Practical File-1

The document provides implementations of various sorting and searching algorithms, including Quick Sort, Heap Sort, Linear Search, and Binary Search. Each algorithm is presented with its respective code in C++. The document also mentions that outputs and sample runs can be included as screenshots.

Uploaded by

abhi8609073
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

Algorithm Design Techniques Lab Practical

1. Quick Sort
void quick(int a[], int l, int h){
int i=l,j=h,p=a[l],t;
while(i<j){
while(a[i]<=p && i<h) i++;
while(a[j]>p) j--;
if(i<j){ t=a[i]; a[i]=a[j]; a[j]=t; }
}
t=a[l]; a[l]=a[j]; a[j]=t;
quick(a,l,j-1); quick(a,j+1,h);
}

2. Heap Sort
void heapify(int a[],int n,int i){
int l=2*i+1,r=2*i+2,largest=i,t;
if(l<n && a[l]>a[largest]) largest=l;
if(r<n && a[r]>a[largest]) largest=r;
if(largest!=i){
t=a[i]; a[i]=a[largest]; a[largest]=t;
heapify(a,n,largest);
}
}

5. Linear Search
int linear(int a[],int n,int x){
for(int i=0;i<n;i++)
if(a[i]==x) return i;
return -1;
}

6. Binary Search
int binary(int a[],int l,int h,int x){
if(l<=h){
int m=(l+h)/2;
if(a[m]==x) return m;
else if(x<a[m]) return binary(a,l,m-1,x);
else return binary(a,m+1,h,x);
}
return -1;
}
Outputs and sample runs can be added as screenshots here.

You might also like