A slightly improved version of BUBBLE SORTING.
void shaker_sort(char *str,int count)
{
int i,exchange=0;
do{
exchange = 0;
for(i=1;i<count-1;i++)
{
if(str[i]>str[i+1])
{
exchange = 1;
swap_items(&str[i],&str[i+1]);
}
}
for(i=count-1;i>0;i--)
{
if(str[i-1]>str[i])
{
exchange = 1;
swap_items(&str[i-1],&str[i]);
}
}
}while(exchange);
}
void swap_items(char *a,char *b)
{
(*a)^=(*b)^=(*a)^=(*b);
}
A Collection Of Weird and Interesting C Programs by Balaji
Friday, November 2, 2007
Sorting Algorithm - 2 (Shaker Sort)
Posted by
Balaji V
at
10:58 AM
1 comments
Labels: SORTING ALGORITHMS
Sorting Algorithm - 1 (BUBBLE SORT)
A very primitive, simple and Well known Sorting algorithm.
void bubble_sort(char *str, int count)
{
int i,j;
for(i=0;i<count;i++)
{
for(j=count-1;j>i;j++)
{
if(str[j-1]>str[j])
{
swap_terms(&str[i],&str[j]);
}
}
}
}
void swap_terms(char *a,char *b)
{
(*a)^=(*b)^=(*a)^=(*b);
}
Posted by
Balaji V
at
10:24 AM
0
comments
Labels: SORTING ALGORITHMS
Subscribe to:
Posts (Atom)






