初学数据结构
#include <stdio.h>
typedef int KeyType;
struct DataType
{
KeyType key;
};
/*算法*/
void QuickSort(struct DataType a[],int low,int high) //a[] 待排序数组,low 与 high 标记最低位与最高位下标
/*用递归方法对数据元素 a[low] -- a[high] 进行排序*/
{
int i = low,j = high;
struct DataType temp;
temp = a[i];
while(i < j)
{
while(i < j && temp.key <= a[j].key) /*这个要有 i<j 条件*/
{
j --;
}
if(i < j) /*这个要有 if 语句作条件判断*/
{
a[i] = a[j];
i++;
}
while(i < j && temp.key >= a[i].key) /*这个要有 i<j 条件*/
{
i++;
}
if(i < j) /*这个要有 if 语句作条件判断*/
{
a[j] = a[i];
j--;
}
}
a[i] = temp; /*这个 a[i] 与 a[j] 都正确*/
if(low < i) /*这个要有 if 语句作条件判断*/
{
QuickSort(a,low,i-1); /*这个不知道为什么是 i-1*/
}
if(i < high) /*这个要有 if 语句作条件判断*/
{
QuickSort(a,j+1,high); /*这个不知道为什么是 j+1*/
}
}
/*测试程序*/
int main()
{
int i;
int n = 21;
struct DataType test[21] = {1,16,45,87,23,5,9,34,10,6,26,64,5,7,89,6,24,100,4,7,19};
QuickSort(test,0,20); /*注意这个的参数及参数传递*/
for(i = 0; i < n; i++)
{
printf(" %d ",test[i].key);
}
printf("\n");
//system("pause"); //暂停
return 0;
}