网上关于快速排序的资料,要么就是原理讲得明白,但是例子根原理不贴边,并且运行出错.要么就是例子是对的,原理看不懂
比如:
void quickSort(int *a,size_t left,size_t right) { size_t p = (left + right)/2; int key = a[p]; for(size_t i = left,j = right; i < j;) { while(!(key < a[i] || p < i)) i++; if(i < p) { a[p] = a[i]; p = i; } while(j>0 && !(j < p || a[j] < key)) j--; if(p < j) { a[p] = a[j]; p = j; } } a[p] = key; if(p - left > 1) quickSort(a,left,p-1); if(right - p > 1) quickSort(a,p + 1, right); }百度百科的这个c++优化版,运行没问题,但是我看不懂他的原理,所以无法改成可逆序的有比如:
Function QuickSort(MyArray() As Long, L, R, Optional FangXiang As Boolean = True)
'对单独数组(下标是连续的)进行排序,首次调用时候,L是数组的最小下标,R是最大下标
'FangXiang=True 是正排序,为False 是逆排序
Dim i, j, X, Y
i = L
j = R
'找出数组的中点
X = MyArray((L + R) / 2)
While (i <= j)
If FangXiang Then '※正排序
'找出比中点大的数
While (MyArray(i) < X And i < R)
i = i + 1
Wend
'找出比中点小的数
While (X < MyArray(j) And j > L)
j = j - 1
Wend
Else '※逆排序
'找出比中点大的数
While (MyArray(i) > X And i < R)
i = i + 1
Wend
'找出比中点小的数
While (X > MyArray(j) And j > L)
j = j - 1
Wend
End If
'互换这两个数
If (i <= j) Then
Y = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = Y
i = i + 1
j = j - 1
End If
'用于指明重复次数的全局变量
'gIterations = gIterations + 1
Wend
'未完成时递归调用
If (L < j) Then QuickSort MyArray(), L, j, FangXiang
If (i < R) Then QuickSort MyArray(), i, R, FangXiang
End Function
这段代码,原理看着很清楚,但是运行时递归错误,希望各位老大能给我一个简单的,结构清晰的,易懂的讲讲他们的原理,并且有源码更好,谢谢快速排序性能优化百度
比如:
void quickSort(int *a,size_t left,size_t right) { size_t p = (left + right)/2; int key = a[p]; for(size_t i = left,j = right; i < j;) { while(!(key < a[i] || p < i)) i++; if(i < p) { a[p] = a[i]; p = i; } while(j>0 && !(j < p || a[j] < key)) j--; if(p < j) { a[p] = a[j]; p = j; } } a[p] = key; if(p - left > 1) quickSort(a,left,p-1); if(right - p > 1) quickSort(a,p + 1, right); }百度百科的这个c++优化版,运行没问题,但是我看不懂他的原理,所以无法改成可逆序的有比如:
Function QuickSort(MyArray() As Long, L, R, Optional FangXiang As Boolean = True)
'对单独数组(下标是连续的)进行排序,首次调用时候,L是数组的最小下标,R是最大下标
'FangXiang=True 是正排序,为False 是逆排序
Dim i, j, X, Y
i = L
j = R
'找出数组的中点
X = MyArray((L + R) / 2)
While (i <= j)
If FangXiang Then '※正排序
'找出比中点大的数
While (MyArray(i) < X And i < R)
i = i + 1
Wend
'找出比中点小的数
While (X < MyArray(j) And j > L)
j = j - 1
Wend
Else '※逆排序
'找出比中点大的数
While (MyArray(i) > X And i < R)
i = i + 1
Wend
'找出比中点小的数
While (X > MyArray(j) And j > L)
j = j - 1
Wend
End If
'互换这两个数
If (i <= j) Then
Y = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = Y
i = i + 1
j = j - 1
End If
'用于指明重复次数的全局变量
'gIterations = gIterations + 1
Wend
'未完成时递归调用
If (L < j) Then QuickSort MyArray(), L, j, FangXiang
If (i < R) Then QuickSort MyArray(), i, R, FangXiang
End Function
这段代码,原理看着很清楚,但是运行时递归错误,希望各位老大能给我一个简单的,结构清晰的,易懂的讲讲他们的原理,并且有源码更好,谢谢快速排序性能优化百度
public static function QuickSort(input:*,left:int,right:int,order:Boolean=DESC,fun:Function=null):void
{
var len:int=input.length;
if(len<=1)return;
var p:int=(left+right)/2,key:*=input[p],i:int,j:int; for(i=left,j=right;i<j;)
{
while(i<right&&input[i]>key)
i++;
while(j>left&&input[j]<key)
j--;
if(i<j)
{
var temp:*=input[i];
input[i]=input[j];
input[j]=temp;
i++;
j--;
}
} if(j>left)
QuickSort(input,left,j);
if(i<right)
QuickSort(input,i,right);
}