Program
Program
h>
#include< conio.h>
void shellsort(int a[],int n)
{
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
{
for(j = m;j< n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
}
}
}
}
}
main()
{
int a[10],i,n;
clrscr();
printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shellsort(a,n);
printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}
/**** C Program For Implementation Of Bubble Sort. Sorting names entered by the
user *****/
#include< stdio.h>
#include< conio.h>
#define MAX 10
char name[MAX][15];
void sort(int n)
{
int pa,cp,i,j,k,kk=0;
char temp[15];
pa=n-1;
cp=n-1;
for(i=1;i< =pa;i++)
{
for(j=1;j< =cp;j++)
{
kk=kk+1;
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
printf("\n List after %d pass is ",i);
for(k=1;k< =n;k++)
printf("\n\t\t %s",name[k]);
getch();
}
clrscr();
printf("\n\t\t Total Comparisions Done : %d",kk);
}
void main()
{
int n,i,j;
clrscr();
printf("Enter How Many Names : ");
scanf("%d",&n);
if(n>MAX)
printf("\n\t\tArray Size IS Only %d",MAX);
else
{
printf("\n\t\tEnter %d Names :\n",n);
for(i=1;i< =n;i++)
{
printf("\t\t");
scanf("%s",name[i]);
}
sort(n);
printf("\n\n\t\tSorted List ");
for(i=1;i< =n;i++)
printf("\n\t\t%s",name[i]);
}
getch();
}
/********************* OUTPUT *******************
Enter How Many Names : 4
Enter 4 Names :
Malcolm
Lionel
Mayank
Pinto
List after 1 pass is
Lionel
Malcolm
Mayank
Pinto
List after 2 pass is
Lionel
Malcolm
Mayank
Pinto
List after 3 pass is
Lionel
Malcolm
Mayank
Pinto
*/
Posted by Lionel at 3:06 AM 2 comments
Labels: C Program, Data Structures, Sorting
Heap Sort
/************* C Program To Sort An Array Using Heap Sort *************/
#include < stdio.h>
#include < conio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x = *y;
*y = temp;
}
void heapsort(int a[],int n)
{
int i,s,f;
for(i=1;i< n;++i)
{
s=i;
f=(s-1)/2;
while( a[f]< a[s])
{
swap(&a[f],&a[s]);
s=f;
if(s==0)
break;
f=(s-1)/2;
}
}
for(i=n-1;i>=1;--i)
{
swap(&a[0],&a[i]);
f=0;
s=1;
if(i==1)
break;
if(i>2)if(a[2]>a[1])s=2;
while( a[f]< a[s] )
{
swap(&a[f],&a[s]);
f=s;
s=2*f+1;
if(i>s+1 )if(a[s+1]>a[s])s=s+1;
if (s>=i)
break;
}
}
}
void main()
{
int a[100],n,i;
clrscr();
printf("\t\tHEAP SORT\n");
printf("\nEnter The Number Of Elements\t: ");
scanf("%d",&n);
printf("\nEnter Elements\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\t\t\tSorted List\n");
for(i=0;i< n;++i)
printf("\t%d",a[i]);
getche();
}
/***************** OUTPUT ******************
HEAP SORT
Enter The Number Of Elements : 6
Enter Elements
45
12
3
1
78
6
Sorted List
1 3 6 12 45 78
*/
Posted by Lionel at 3:02 AM 2 comments
Labels: C Program, Data Structures, Sorting
Radix Sort Algorithm
/* C program to sort an array using radix sort LINKED LIST implementation*/
#include < stdio.h>
#include < conio.h>
#include < stdlib.h>
void radix(int a[],int n,int m)
{
typedef struct node
{
int data;
struct node * next;
}NODE;
NODE * ptr,*start,*prev;
NODE *front[10], *rear[10];
int k=1,i,j,y,p;;
/*creating initial linked list*/
start=NULL;
for(i=0;i< n;++i)
{
ptr=(NODE *)malloc(sizeof(NODE));
ptr->data=a[i];
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
prev->next=ptr;
prev=ptr;
}
/*radix sort*/
for(i=1;i< =m;++i)
{
for(j=0;j< 10;++j)
front[j]=NULL;
/*placing elements into queues*/
ptr=start;
while(ptr!=NULL)
{y=ptr->data/k %10;/*y is the digit*/
if(front[y]==NULL)
{
front[y]=ptr;
rear[y]=ptr;
}
else
{
rear[y]->next=ptr;
rear[y]=ptr;
}
ptr=ptr->next;
}
start=NULL;
for(j=0;j< 10;++j)
if(front[j]!=NULL)
{
if(start==NULL)
start=front[j];
else rear[p]->next=front[j];
p=j;
}
rear[p]->next=NULL;
k=k*10;
}
/*copying back to array*/
ptr=start;
for(i=0;i< n;++i,ptr=ptr->next)
a[i]=ptr->data;
}
void main()
{
int a[100],n,i,m;
char temp;
do
{
clrscr();
printf("===========================RADIX SORT===================================
========\n");
printf("ENTER NUMBER OF NUMBERS AND NUMBER OF DIGITS\n");
scanf("%d%d",&n,&m);
printf("ENTER ELEMENTS\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
radix(a,n,m);
printf("SORTED LIST\n");
for(i=0;i< n;++i)
printf("%d ",a[i]);
printf("\nDO YOU wish to continue?[y/n]\n");
scanf("%c",&temp);
}while(temp=='y'|| temp=='Y');
printf("\n----------------------------------------------------------------------
-----------\n");
getch();
}
/*OUTPUT:
===========================RADIX SORT===========================================
Enter number of numbers and number of digits
4
2
enter elements
25
65
35
45
sorted list
25 35 45 65
Do you wish to continue?[y/n]
n
--------------------------------------------------------------------------------
*/
Posted by Lionel at 2:54 AM 2 comments
Labels: C Program, Data Structures, Sorting
Wednesday, February 27, 2008
Shuttle Sort - Simple Insertion Sort
#include< stdio.h>
#include< conio.h>
void shutsort(int a[],int n)
{
int j,i=1,mid;
while(i< n)
{
j=i-1;
while(j>=0)
{
if(a[j]>a[j+1])
{
mid = a[j];
a[j] = a[j+1];
a[j+1]=mid;
j--;
}
else
break;
}
i++;
}
}
main()
{
int a[10],i,n;
clrscr();
printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shutsort(a,n);
printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}