您的位置:首页 > 其它

快速排序

2017-08-17 11:41 148 查看
c++模板函数

template <typename T>
int _quickSort(T* list, int low, int heigh)
{
if (low >= heigh) { return low; }

T temp = list[low];
while (low < heigh)
{
while ((list[heigh] >= temp) && (low < heigh))
{
--heigh;
}

if (low < heigh)
{
list[low] = list[heigh];
++low;
}

while ((list[low] <= temp) && (low < heigh))
{
++low;
}

if (low < heigh)
{
list[heigh] = list[low];

--heigh;
}
}

list[low] = temp;

return low;
}

template <typename T>
void QuickSort(T* list, int low, int heigh)
{
if (low >= heigh) { return; }

int a = _quickSort(list, low, heigh);
QuickSort(list, low, a - 1);
QuickSort(list, a + 1, heigh);
}


test

int main()
{
int sort[] = {6, 56, 1, 23, 35, 42, 14, 6, 7, 4};

QuickSort(sort, 0, (sizeof(sort) - 1) / sizeof(int));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: