You are on page 1of 14

1.

character sorting
#include<stdio.h>
#include<string.h>
char str[100];
void charsort(char str[])
{
char temp;
int i,j,n;
n=strlen(str);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(str[i]>str[j])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
}
int main()
{
int a[100];
printf("Enter a string: ",a);
scanf("%s",a);
charsort(a);
printf("sorted string is: %s",a);
return 0;
}

2.binary search
#include<stdio.h>
int bin_search(int*,int,int,int);
int main()
{
int arr[20],n,i,ele,r;
printf("Enter the array size:");
scanf("%d",&n);
printf("Read the array:\n");
for(i=0;i<n;i++)
{
printf("\nEnter the value at arr[%d]:",i);
scanf("%d",&arr[i]);
}
printf("\nEnter the element you want to search:");
scanf("%d",&ele);
r=bin_search(arr,ele,0,n-1);
if(r!=-1)
{
printf("The search is successfull!\n");
printf("The element %d is present at %d index\
n",ele,r);
}
else
printf("The search is Unsuccessfull!");

return 0;
}
int bin_search(int arr[],int ele,int low,int high)
{
int i,mid;
if(low<=high)
{
mid=(low+high)/2;
if(ele<arr[mid])
return bin_search(arr,ele,low,mid-1);
else if(ele>arr[mid])
return bin_search(arr,ele,mid+1,high);
else
return mid;
}
return -1;
}

3.merge sort
#include<stdio.h>
void mergesort(int a[],int low,int high);
void merge(int[],int,int,int);

void mergesort(int a[],int low,int high)


{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}

void merge(int a[],int low,int mid,int high)


{
int b[50],i=low,j=mid+1,k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
b[k++]=a[i++];
}
else
{
b[k++]=a[j++];
}
}
while(i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(i=low;i<k;i++)
a[i]=b[i];
}
void main()
{
int a[10],n,i;
printf("\n Enter the no. of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n Elements after Sorting Are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);

4.quicksort
#include<stdio.h>
void quicksort(int*,int,int);
int partition(int*,int,int);
void swap(int*,int,int);
int main()
{
int arr[20],n,i,ele,r;
printf("Enter the array size:");
scanf("%d",&n);
printf("Read the array:\n");
for(i=0;i<n;i++)
{
printf("\nEnter the value at arr[%d]:",i);
scanf("%d",&arr[i]);
}
quicksort(arr,0,n-1);
printf("The Sorted Array is:");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
printf("\n");
return 0;
}

void quicksort(int a[],int low,int high)


{
int j;
if(low<high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}

int partition(int a[],int low,int high)


{

int i=low,j=high,pivot=a[low];

while(i<j)
{
while((a[i]<=pivot)&&(i<high))
i++;
while((a[j]>pivot)&&(j>low))
j--;
if(i<j)
swap(a,i,j);
if(i==j)
break;
}
swap(a,low,j);

return j;
}

void swap(int a[],int i,int j)


{
int temp;

temp=a[i];
a[i]=a[j];
a[j]=temp;
}

5.prims algorithm
#include<stdio.h>
#include<conio.h>
int visited[10]={0}, cost[10][10],
min, mincost=0;
int input(int);
int display(int);
int prims(int);

int input(int num)


{
int i, j;
printf("\nEnter the adjacency
matrix\n\n");
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("value of
cost[%d][%d] : ",i,j);
scanf("%d",
&cost[i][j]);
}
}
return 0;
}
int display(int num)
{
int i, j;
printf("\nThe cost of
adjacency matrix\n\n");
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("%d", cost[i]
[j]);
printf("\t");
}
printf("\n");
}
return 0;
}

int prims(int num)


{
int i, j, ne=1, a, b, u, v;
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
if(cost[i][j]==0)
cost[i][j]=999;
}
}
visited[1]=1;
while(ne < num)
{

for(i=1,min=999;i<=num;i+
+)
for(j=1;j<=num;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
printf("\n Edge %d:(%d
- %d) cost:%d",ne++,a,b,min);
mincost=mincost+min;
visited[b]=1;
cost[a][b]=cost[b]
[a]=999;
}
printf("\n\n\n Minimun cost=
%d",mincost);
}
int main()
{
int num;
printf("\n\t\t\tPrim's
Algorithm");
printf("\n\nEnter the number
of nodes= ");
scanf("%d", &num);
input(num);
display(num);
prims(num);
getch();
return 0;
}

You might also like