1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void tri_Qksort(int m,int n,char * szStr[]);
int partition(int m,int n,char * szStr[]);
int main(int argc, char *argv[])
{
char szStr[7][200] = {{"un"},{"trois"},{"quatre"},{"cinq"},{"AAA"},{"B"},{"b"}};
int iDeb=0,iFin = 7,i;
/*Affichage tableau non trié*/
for(i = 0;i<7;++i){
printf("%s ",szStr[i]);
}
/* Appel du tri. */
tri_Qksort(iDeb,iFin,szStr);
printf("\n");
/* Affichage tableau trié. */
for(i = 0;i<7;++i){
printf("%s ",szStr[i]);
}
getch();
return 0;
}
int partition(int m,int n,char * szStr[])
{
char pivot[200];
strcpy(pivot,szStr[m]);
int i = m-1;
int j = n+1; // indice final du pivot
while (1)
{
do
{
j--;
} while (strcmp(szStr[j],pivot)<0);
do
{
i++;
}while (strcmp(szStr[i],pivot)>0);
if (i<j)
{
char temp[200];
strcpy(temp,szStr[i]);
strcpy(szStr[i],szStr[j]);
strcpy(szStr[j],temp);
}
else
{
return j;
}
}
}
void tri_Qksort(int m,int n,char * szStr[]) {
if (m<n) {
int p = partition(m,n,szStr);
tri_Qksort(m,p,szStr);
tri_Qksort(p+1,n,szStr);
}
} |
Partager