//冒泡算法
void BubbleSort(char *pChar, const int len)
{
char temp;
for(int i=0; i<len; ++i)
{
for(int j=i+1; j<len; ++j)
{
if( *(pChar+i) > *(pChar+j) )
{
temp = *(pChar+i);
*(pChar+i) = *(pChar+j);
*(pChar+j) = temp;
}
}
}
return;
}
//划分区间,选取一个数为基准点,对其进行区间划分,前半区间小于它,后半区间大于它,记录基准点最终位置,返回其值
int Partition(char *pChar, int m, int n)
{
// char pivot = *pChar;
int PrePos = m;
int BackPos = n;
int PivotPos = m;
char temp;
while(PrePos<BackPos)
{
for(;PrePos<BackPos;BackPos--) //后半区间遍历,找到后半区间比它小的,置换位置,把后半区间的值放入前半区间
{
if(*(pChar+BackPos) < *(pChar+PrePos))
{
temp = *(pChar+BackPos);
*(pChar+BackPos) = *(pChar+PrePos);
*(pChar+PrePos) = temp;
break;
}
}
for(;PrePos<BackPos;PrePos++) //前半区间遍历,找到前半区间比它大的,置换位置,把前半区间的值放入后半区间
{
if(*(pChar+PrePos) > *(pChar+BackPos))
{
temp = *(pChar+BackPos);
*(pChar+BackPos) = *(pChar+PrePos);
*(pChar+PrePos) = temp;
break;
}
}
}
PivotPos=PrePos;
return PivotPos;
}
//快速排序算法
//1.划分区间 2.递归
void QuickSort(char *pChar, int m, int n)
{
int PivotPos = m;
if(m<n) //递归,直到区间的间距小于等于1
{
PivotPos = Partition(pChar, m, n);
QuickSort(pChar, m, PivotPos-1);
QuickSort(pChar, PivotPos+1, n);
}
return;
}
//shell插入
static void ShellInsert(char *pChar, const int dis, const int len)
{
int PrePos = 0;
int CurPos = 0;
char tmp;
for(int i=dis; i<len; i++)
{
PrePos = i - dis;
CurPos = i;
tmp = *(pChar+CurPos);
while((PrePos >= 0))
{
if(*(pChar+PrePos) > tmp)
{
*(pChar+CurPos) = *(pChar+PrePos);
CurPos = PrePos;
}
PrePos = PrePos - dis;
}
*(pChar+CurPos) = tmp;
}
return;
}
//shell排序算法,选取间距dis,字符串中每间隔dis的值为一组,分组进行排序
void ShellSort(char *pChar, const int len)
{
int dis = len/2;
while(dis > 0)
{
ShellInsert(pChar, dis, len);
dis = dis/2;
}
return;
}
void main()
{
char list[100];
char temp;
int i = 0,len = 0;
char *pCList = NULL;
printf("input charaters!\n");
while((temp = getchar()) != '\n')
{
list[i]=temp;
i++;
}
list[i]='\0';
len=strlen(list);
pCList = (char*)malloc(sizeof(char)*len);
for(int n=0; n<len; ++n)
{
*(pCList+n)=list[n];
}
// BubbleSort(pCList,len);
// QuickSort(pCList,0,len-1);
ShellSort(pCList,len);
return;
}