#include <stdio.h>
#include <stdlib.h>
#define MAXITEM 100
typedef char ElemType[5];//?
//希尔排序
typedef struct node
{
int key;
ElemType data;
}elemnode[MAXITEM];
void shellsort(elemnode r,int n)
{
int j,i,gap;
gap=n/2;
while(gap>0)
{
for(i=gap+1;i<=n;i++)
{
j=i-gap;
while(j>0)
if(r[j].key>r[j+gap].key)
{
r[0]=r[j];
r[j]=r[j+gap];
r[j+gap]=r[0];
j=j-gap;
}
else j=0;
}
gap/=2;
}
printf("After sort:/n");
for(i=1;i<=n;i++) printf("%6d",r[i].key);
printf("/n");
for(i=1;i<=n;i++) printf("%6s",r[i].data);
printf("/n");
}
//thought:potect every small grounp,keep them in sort;O(nlogn);
//快排
void quicksort(elemnode r, int s,int t)//sort from element[s]->element[t]
{
int i=s,j=t;
r[0]=r[s];
printf("sort:/n");
while(i<j)
{
while(j>i&&r[0].key<r[j].key) j--;//search from right to left,change the value if key<r[0].key
if(i<j)
{
r[i]=r[j];
i++;
}
while(i<j&&r[i].key<=r[0].key) i++;//search from left to right ,change the value if key>r[0].key
if(i<j)
{
r[j]=r[i];
j--;
}
}
r[i]=r[0];
//printf("%2d",r[i]);
if(s<i) quicksort(r,s,j-1);
if(i<t) quicksort(r,j+1,t);
}
//thought:sort the first element each time,put the element small than first to left ,big to right,loop
int main()
{
int i,n=7;
elemnode e={0,"",3,"jack",32,"Tom",43,"Rose",72,"Hack",42,"Ela",53,"Merry",68,"Batty"};
//shellsort(e,7);
quicksort(e,1,7);
printf("After sort:/n");
for(i=1;i<=n;i++) printf("%6d",e[i].key);
printf("/n");
for(i=1;i<=n;i++) printf("%6s",e[i].data);
printf("/n");
printf("Hello world!/n");
return 0;
}