Sorting Structures : structure sort « Structure « C Tutorial

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C Tutorial » Structure » structure sort 
9. 3. 1. Sorting Structures
struct address {
    char name[40];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
  };
  /* A Quicksort for structures of type address. */
  void quick_struct(struct address items[]int count)
  {
    qs_struct(items,0,count-1);
  }

  int qs_struct(struct address items[]int left, int right)
  {

    register int i, j;
    char *x;
    struct address temp;

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

    do {
      while((strcmp(items[i].zip,x0&& (i < right)) i++;
      while((strcmp(items[j].zip,x0&& (j > left)) j--;
      if(i <= j) {
        temp = items[i];
        items[i= items[j];
        items[j= temp;
        i++; j--;
      }
    while(i <= j);

    if(left < jqs_struct(items, left, j);
    if(i < rightqs_struct(items, i, right);
  }

  int main(void){
   struct address addrs[4{
    "A. "" 1st St""AAAA""Ga""55555",
    "B. "" 2nd Ave""AA""Pa""33333",
    "C. "" 3rd Blvd""VVV""OOOOO""99999",
    "D. "" Fourth Dr""EEE""MMMMM""44444"
  };

    quick_struct(addrs, 4);

    int i;
    for(i =0;i<4;i++){
       printf("%s \n",addrs[i].zip);
    }

  }
33333
44444
55555
99999
9. 3. structure sort
9. 3. 1. Sorting Structures
w__w___w_.__ja__v___a2_s_.___com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.