0% found this document useful (0 votes)
55 views13 pages

Program 1: Bubble Sort

The document contains 6 programs that implement different sorting algorithms: bubble sort, insertion sort, binary search, quick sort, heap sort, and counting sort. Each program includes the code to implement the respective algorithm, sample input/output, and comments explaining the program.

Uploaded by

shm2290
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views13 pages

Program 1: Bubble Sort

The document contains 6 programs that implement different sorting algorithms: bubble sort, insertion sort, binary search, quick sort, heap sort, and counting sort. Each program includes the code to implement the respective algorithm, sample input/output, and comments explaining the program.

Uploaded by

shm2290
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Program 1: Bubble Sort

#include<stdio.h>

#include<conio.h>

void bubble(int arr[],int len)

for(int x=0;x<len-1;x++)

for(int y=0;y<len-x-1;y++)

if(arr[y]>arr[y+1])

int temp=arr[y];

arr[y]=arr[y+1];

arr[y+1]=temp;

void main()

clrscr();

int arr[20],len;

printf("Enter Number of elements to be sorted\n");

scanf("%d",&len);

printf("Enter Elements\n");

for(int x=0;x<len;x++)
{

printf("Element %d:",x+1);

scanf("%d",&arr[x]);

bubble(arr,len);

printf("\nSorted array is\n");

for(x=0;x<len;x++)

printf("%d ",arr[x]);

getch();

/*Output

Enter Number of elements to be sorted

Enter Elements

Element 1:2

Element 2:4

Element 3:1

Element 4:3

Element 5:5

Sorted array is

12345

*/
Program 2 : Insertion Sort

#include<stdio.h>

#include<conio.h>

void insert(int arr[],int len)

for(int x=1;x<len;x++)

int temp=arr[x];

for(int y=x-1;arr[y]>temp && y>=0;y--)

arr[y+1]=arr[y];

arr[y+1]=temp;

void main()

clrscr();

int arr[20],len;

printf("Enter Number of elements to be sorted\n");

scanf("%d",&len);

printf("Enter Elements\n");

for(int x=0;x<len;x++)

printf("Element %d:",x+1);

scanf("%d",&arr[x]);

}
insert(arr,len);

printf("\nSorted array is\n");

for(x=0;x<len;x++)

printf("%d ",arr[x]);

getch();

/* Output

Enter Number of elements to be sorted

Enter Elements

Element 1:3

Element 2:2

Element 3:5

Element 4:4

Element 5:1

Sorted array is

12345

*/
Program 3: Binary Search

#include<stdio.h>

#include<conio.h>

#include<string.h>

int binrysrc(int arr[],int item,int len)

int low=0,high=len-1,mid=(low+high)/2;

while(low<=high && arr[mid]!=item)

if(arr[mid]<item)

low=mid+1;

else

high=mid-1;

mid=(low+high)/2;

if(arr[mid]==item)

return mid;

else

return -1;

void main()

clrscr();

int arr[50],item,n;

printf("Enter size of array :");

scanf("%d",&n);

printf("Enter array elements\n");


for(int i=0;i<n;i++)

printf("Element %d:",i+1);

scanf("%d",&arr[i]);

printf("Enter element to be searched:");

scanf("%d",&item);

int pos = binrysrc(arr,item,n);

if(pos!=-1)

printf("Item found at %d",pos+1);

else

printf("Item not found");

getch();

/*

Output

Enter size of array :5

Enter array elements

Element 1:1

Element 2:3

Element 3:5

Element 4:7

Element 5:9

Enter element to be searched:5

Item found at 3

*/
Program 4: Quick Sort

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int arr[50],n;

int part(int a[],int p,int r)

int x=a[r];

int i=p-1;

for(int j=p;j<=r;j++)

{ if(a[j]<=x)

{ i++;

int temp=a[i];

a[i]=a[j];

a[j]=temp;

return i;

int randpart(int a[],int p,int r)

{ int i=random(r)+p;

int temp=a[i];

a[i]=a[r];

a[r]=temp;

return part(a,p,r);

void quick(int a[],int p,int r)


{

if(p<r)

{ int q=randpart(a,p,r);

quick(a,p,q-1);

quick(a,q+1,r);

void main()

{ clrscr();

printf("Enter size of array :");

scanf("%d",&n);

printf("Enter elements of array\n");

for(int i=0;i<n;i++)

scanf("%d",&arr[i]);

quick(arr,0,n-1);

printf("The sorted array is\n");

for(int x=0;x<n;x++)

printf("%d ",arr[x]);

getch();

/* Output

Enter size of array :8

Enter elements of array

72413865

The sorted array is

1 2 3 4 5 6 7 8 */
Program 5: Heap Sort

#include<iostream.h>

#include<conio.h>

#define left(x) (2*x+1)

#define right(x) (2*x+2)

#define parent(x) int((x-1)/2)

void arrprint(int arr[],int size)

{ for(int x=0;x<size;x++)

cout<<arr[x]<<" ";

cout<<endl;

getch();

void maxheapi(int a[],int heapsize,int i)

{ int l = left(i);

int r = right(i);

int largest=i;

if(l<heapsize && a[largest]<a[l])

{ largest = l; }

if(r<heapsize && a[largest]<a[r])

{ largest = r; }

if(i!=largest)

{ int temp=a[largest];

a[largest]=a[i];

a[i]=temp;

maxheapi(a,heapsize,largest);

}
int buildmax(int a[],int arrsize)

{ cout<<"Building Max Heap\n";

for(int x=arrsize/2-1;x>=0;x--)

{ maxheapi(a,arrsize,x);

arrprint(a,arrsize);

return arrsize;

void heapsort(int a[],int &heapsize,int arrsize)

{ cout<<"Applying Heapsort\n";

for(int x=arrsize;x>=1;x--)

{ int temp = a[heapsize-1];

a[heapsize-1] = a[0];

a[0]=temp;

heapsize--;

maxheapi(a,heapsize,0);

arrprint(a,arrsize);

void main()

{ clrscr();

int arr[50],arrsize,heapsize;

cout<<"Enter heapsize\n";

cin>>arrsize;

for(int x=0;x<arrsize;x++)

cout<<"Element "<<x+1<<" : ";


cin>>arr[x];

heapsize=buildmax(arr,arrsize);

heapsort(arr,heapsize,arrsize);

getch();

/* Output

Enter heapsize

Element 1 : 3

Element 2 : 2

Element 3 : 4

Element 4 : 5

Element 5 : 1

Building Max Heap

53421

Applying Heapsort

43125

32145

21345

12345

*/
Program 6: Counting Sort

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int* countsrt(int a[],int len,int range)

int *c = (int*) calloc(range+1,sizeof(int)); //c[0,1,...,range]

int *b = (int*) calloc(len,sizeof(int));

for(int i=0;i<len;i++)

c[a[i]]++;

for(i=1;i<=range;i++)

c[i]+=c[i-1];

for(i=len-1;i>=0;i--)

b[--c[a[i]]]=a[i];

return b;

void main()

clrscr();

int len,*arr,range;

cout<<"Enter length of array:";

cin>>len;
arr = (int*) calloc(len,sizeof(int));

for(int x=0;x<len;x++)

cout<<"Enter number"<<x+1<<":";

cin>>arr[x];

cout<<"Enter range:";

cin>>range;

int *b = countsrt(arr,len,range);

for(x=0;x<len;x++)

cout<<b[x]<<" ";

getch();

/*Output

Enter length of array:5

Enter number1:2

Enter number2:4

Enter number3:1

Enter number4:3

Enter number5:5

Enter range:5

12345

*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy