Important alert: (current site time 8/20/2014 10:16:37 AM EDT)
 

VB icon

SHAKER SORT

Email
Submitted on: 11/26/2003 10:52:28 AM
By: Renjini S 
Level: Beginner
User Rating: Unrated
Compatibility: C
Views: 5575
 
     This code is used to sort an array of integers. The algorithm used is SHAKER SORT. This is an improvement to the bubble sort.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :SHAKER SORT
//**************************************
#include <stdio.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: SHAKER SORT
// Description:This code is used to sort an array of integers.
The algorithm used is SHAKER SORT. This is an improvement to the bubble sort.
// By: Renjini S
//
// Inputs:Array elements
//
// Returns:Sorted Array
//
// Assumes:/*
SHAKER SORT
 This is an improvement to the bubble sort.
 Instead of always reading the array in the same
 direction,alternate passes could reverse
 direction.In this way,greatly out-of-place
 elements travel quickly to the correct position.
 It is called "Shaker sort" because it imparts
 the effect of a shaking motion to the array.
 Although the shaker sort improves the bubble 
 sort, it still executes on the order of an
 n-squared algorithm.
*/
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7165&lngWId=3//for details.//**************************************

#include <stdio.h>
#define ARRAYSIZE 10
void shaker(int *,int);
void main()
{
int i,a[ARRAYSIZE];
printf("Enter the array elements\n");
for(i=0;i<ARRAYSIZE;i++)
scanf("%d",&a[i]);
shaker(a,ARRAYSIZE);
printf("Sorted array\n");
for(i=0;i<ARRAYSIZE;i++)
printf(" %d",a[i]);
printf("\n");
}
void shaker(int *a,int n)
{
int i;
int exchange;
int t;
do
{
exchange=0;
for(i=n-1;i>0;--i)
{
if(a[i-1]>a[i])
{
t=a[i-1];
a[i-1]=a[i];
a[i]=t;
exchange=1;
}
}
for(i=1;i<n;++i)
{
if(a[i-1]>a[i])
{
t=a[i-1];
a[i-1]=a[i];
a[i]=t;
exchange=1;
}
}
}while(exchange);
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.