0 Replies - 392 Views - Last Post: 30 January 2009 - 11:14 AM Rate Topic: -----

#1 atik97   User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Shaker sort

Posted 30 January 2009 - 11:14 AM

Description:
#include <string.h>
  #include <stdio.h>
  #include <stdlib.h>


  void shaker(char *items, int count)
  {
    register int a;
    int exchange;
    char t;

    do {
      exchange = 0;
      for(a=count-1; a > 0; --a) {
        if(items[a-1] > items[a]) {
          t = items[a-1];
          items[a-1] = items[a];
          items[a] = t;
          exchange = 1;
        }
      }

      for(a=1; a < count; ++a) {
        if(items[a-1] > items[a]) {
          t = items[a-1];
          items[a-1] = items[a];
          items[a] = t;
          exchange = 1;
        }
      }
    } while(exchange); /* sort until no exchanges take place */
  }

  int main(void)
  {

    char s[255];

    printf("Enter a string:");
    gets(s);
    shaker(s, strlen(s));
    printf("The sorted string is: %s.n", s);

    return 0;
  }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1