C/C++: Trier les Membres d’une structure

Author:


Download

#include 

struct contact {
  char nom[40];
  char prenom[40];
  char adresse[20];
  char ville[3];
  char codepostal[11];
};

void tri_struct(struct contact elements[], int n_gauche, int n_droite)
{

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

  i = n_gauche;
  j = n_droite;
  x = elements[(n_gauche+n_droite)/2].codepostal;

  do {
    while((strcmp(elements[i].codepostal,x) < 0) && (i < n_droite)) i++;
    while((strcmp(elements[j].codepostal,x) > 0) && (j > n_gauche)) j--;
    if(i <= j) {
      temp = elements[i];
      elements[i] = elements[j];
      elements[j] = temp;
      i++; j--;
    }
  } while(i <= j);

  if(n_gauche < j) tri_struct(elements, n_gauche, j);
  if(i < n_droite) tri_struct(elements, i, n_droite);
}

 void quick_struct(struct contact elements[], int count)
{
  tri_struct(elements,0,count-1);
}

Leave a Reply

Your email address will not be published. Required fields are marked *