The Quicksort : Quicksort « Search Sort « C Tutorial

Home
C Tutorial
1.Language
2.Data Type
3.String
4.printf scanf
5.Operator
6.Statement
7.Array
8.Function
9.Structure
10.Pointer
11.Memory
12.Preprocessor
13.File
14.Data Structure
15.Search Sort
16.Wide Character String
17.assert.h
18.ctype.h
19.math.h
20.setjmp.h
21.signal.h
22.stdio.h
23.stdlib.h
24.string.h
25.time.h
26.wctype.h
C / ANSI-C
C++
C++ Tutorial
Visual C++ .NET
C Tutorial » Search Sort » Quicksort 
15.6.1.The Quicksort
#include <string.h>
  #include <stdio.h>
  #include <stdlib.h>


  /* Quicksort setup function. */
  void quick(char *items, int count)
  {
    qs(items, 0, count-1);
  }

  int qs(char *items, int left, int right)
  {

    register int i, j;
    char x, y;

    i = left; j = right;
    x = items[(left+right)/2];

    do {
      while((items[i< x&& (i < right)) i++;
      while((x < items[j]) && (j > left)) j--;

      if(i <= j) {
        y = items[i];
        items[i= items[j];
        items[j= y;
        i++; j--;
      }
    while(i <= j);

    if(left < j)
       qs(items, left, j);
    if(i < right)
       qs(items, i, right);

  }


  int main(void)
  {

    char s[255];

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

    return 0;
  }
Enter a string:ewqqwerewqqwer12343211234
The sorted string is: 11122233344eeeeqqqqrrwwww.
15.6.Quicksort
15.6.1.The Quicksort
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.