快速排序算法出有关问题,求大神解释错在哪儿
快速排序算法出问题,求大神解释错在哪儿?
算法中,找哨兵,是选取左中右三个数,然后选择三数中中间值作为哨兵。但程序中有两个处理方法。第一种就是在找哨兵时交换已经比较过的数,减少后续的交换次数。这种出问题。另外一种就只是找出哨兵所在位置,不交换,没有出问题。为什么会出现这种情况?求大神解释下,交换会带来什么问题?
------解决思路----------------------
先
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express
点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装
再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
------解决思路----------------------
------解决思路----------------------
稍微读了一下代码:
int m = low + (low + high)/2; 你确定m是这么计算的?
------解决思路----------------------
------解决思路----------------------
参考代码段
http://pan.baidu.com/s/17yIcQ
------解决思路----------------------
int m = low + (low + high)/2; 其中low=2,high=4,算一下m是多少
int swap(int *a, int s, int t)
{
int temp = a[s];
a[s] = a[t];
a[t] = temp;
}
int Partition(int *a, int low, int high)
{
int pivotkeydata,i,j,temp;
int m = low + (low + high)/2;
/************************************************************************/
/* 在找哨兵时交换 */
/************************************************************************/
// if (a[low] > a[high])
// swap(a,low,high);
// if (a[m] > a[high])
// swap(a,m,high);
// else if (a[low] < a[m])
// swap(a,m,low);
/************************************************************************/
/* 在找哨兵时不交换 */
/************************************************************************/
if (a[low] > a[high])
{
swap(a,low,high);
}
if (a[m] > a[high])
m = high;
else if (a[m] < a[low])
m = low;
pivotkeydata = a[m];
//pivotkeydata = a[low];
i = low;
j = high;
while(i<j)
{
while(i<j && a[j] >= pivotkeydata)
j--;
swap(a,i,j);
while(i<j && a[i] <= pivotkeydata)
i++;
swap(a,i,j);
}
return i;
}
int QSort(int *a, int low, int high)
{
int pivot;
if (low < high)
{
pivot = Partition(a,low,high);
QSort(a,low,pivot);
QSort(a,pivot+1,high);
}
return 0;
}
int quickSort(int *a, int n)
{
QSort(a,0,n-1);
return 0;
}
算法中,找哨兵,是选取左中右三个数,然后选择三数中中间值作为哨兵。但程序中有两个处理方法。第一种就是在找哨兵时交换已经比较过的数,减少后续的交换次数。这种出问题。另外一种就只是找出哨兵所在位置,不交换,没有出问题。为什么会出现这种情况?求大神解释下,交换会带来什么问题?
------解决思路----------------------
先
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express
点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装
再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
------解决思路----------------------
// 快速排序
void quick_sort (int data[],
size_t left, size_t right) {
size_t p = (left + right) / 2;
int pivot = data[p];
size_t i = left, j = right;
while (i < j) {
for (; ! (i >= p
------解决思路----------------------
pivot < data[i]); ++i);
if (i < p) {
data[p] = data[i];
p = i;
}
for (; ! (j <= p
------解决思路----------------------
data[j] < pivot); --j);
if (j > p) {
data[p] = data[j];
p = j;
}
}
data[p] = pivot;
if (p - left > 1)
quick_sort (data, left, p - 1);
if (right - p > 1)
quick_sort (data, p + 1, right);
}
------解决思路----------------------
稍微读了一下代码:
int m = low + (low + high)/2; 你确定m是这么计算的?
------解决思路----------------------
void qicksort(int a[],int left,int right)
{
int i,j;
if(left<right)
{
i=left;
j=right;
int temp=a[left];
do
{
while(a[j]>temp&&i<j)
j--;
if(i<j)
{
a[i]=a[j];
i++;
}
while(a[i]<temp&&i<j)
i++;
if(i<j)
{
a[j]=a[i];
j--;
}
} while (i<j);
a[i]=temp;
qicksort(a,left,i-1);
qicksort(a,i+1,right);
}
}
------解决思路----------------------
参考代码段
http://pan.baidu.com/s/17yIcQ
------解决思路----------------------
int m = low + (low + high)/2; 其中low=2,high=4,算一下m是多少