Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void cs_sort(int *,int);
- void swap(int *,int *);
- int main(void) {
- int n,lower,upper,i;
- clock_t start,end;
- double total;
- printf("Enter no. of elements:");
- scanf("%d",&n);
- printf("Enter the range:");
- scanf("%d %d",&lower,&upper);
- int *array=(int *)malloc(n*sizeof(int));
- srand((unsigned)time(NULL));
- for(i=0;i<n;i++)
- array[i]=lower+rand()%(upper-lower);
- start=clock();
- cs_sort(array,n);
- end=clock();
- total=(double)(end-start)/CLOCKS_PER_SEC;
- for(i=0;i<n;i++){
- printf("%d,",array[i]);
- }
- printf("\n\nTime taken for cocktail shaker sort: %lf",total);
- return 0;
- }
- void cs_sort(int *a,int n){
- int i,swapped=1,start,end;
- start=0;
- end=n-1;
- while(swapped){
- swapped=0;
- for(i=start;i<end;i++){
- if(a[i]>a[i+1]){
- swap(a+i,a+i+1);
- swapped++;
- }
- }
- if(!swapped)
- break;
- end--;
- for(i=end;i>=start;i--){
- if(a[i]>a[i+1]){
- swap(a+i,a+i+1);
- swapped++;
- }
- }
- start++;
- }
- }
- void swap(int *a,int *b){
- int temp;
- temp=*a;
- *a=*b;
- *b=temp;
- }
Add Comment
Please, Sign In to add comment