24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
}
void quicksort(int a[],int n){ qsort(a,0,n); }
//简单示例
#include
#include \存放于个人函数库中 int main(void){
int i,a[11]={0,11,12,5,6,13,8,9,14,7,10}; for(i=0;i<11;printf(\ printf(\ quicksort(a,10);
for(i=0;i<11;printf(\ printf(\}
C++,递归快排(随机) 1 #include
28 29 30 31 32 33 VB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
for(int i(0);i!=7;++i){
cout< cout< '快速排序算法,对字符串数组进行排序 Private Sub quicksort(ByRef arrValue() As String,ByVal intLx As Integer,ByVal intRx As Integer) 'arrValue()是待排的数组,intLx,intRx为左右边界 Dim strValue As String Dim I As Integer Dim j As Integer Dim intLoop As Integer I = intLx j = intRx Do While arrValue(I) <= arrValue(j) And I < j: I = I + 1: Wend If I < j Then strValue = arrValue(I) arrValue(I) = arrValue(j) arrValue(j) = strValue End If While arrValue(I) <= arrValue(j) And I < j: j = j - 1: Wend If I < j Then strValue = arrValue(I) arrValue(I) = arrValue(j) arrValue(j) = strValue End If Loop Until I = j I = I - 1: j = j + 1 If I > intLx Then Call quicksort(arrValue,intLx,I) End If If j < intRx Then Call quicksort(arrValue,j,intRx) End If End Sub Private Sub Form_Load() Dim arr(8) As String arr(0) = \ arr(1) = \ arr(2) = \ arr(3) = \ 38 39 40 41 42 43 44 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 arr(4) = \ arr(5) = \ arr(6) = \ arr(7) = \ arr(8) = \ Call quicksort(arr,0,UBound(arr)) End Sub public class QuickSort { public static void sort(Comparable[] data,int low,int high) { // 枢纽元,一般以第一个元素为基准进行划分 int i = low; int j = high; if (low < high) { // 从数组两端交替地向中间扫描 Comparable pivotKey = data[low]; // 进行扫描的指针i,j;i从左边开始,j从右边开始 while (i < j) { while (i < j && data[j].compareTo(pivotKey) > 0) { j--; }// end while if (i < j) { // 比枢纽元素小的移动到左边 data[i] = data[j]; i++; }// end if while (i < j && data[i].compareTo(pivotKey) < 0) { i++; }// end while if (i < j) { // 比枢纽元素大的移动到右边 data[j] = data[i]; j--; }// end if }// end while // 枢纽元素移动到正确位置 data[i] = pivotKey; // 前半个子表递归排序 sort(data,low,i - 1); // 后半个子表递归排序 sort(data,i + 1,high); }// end if }// end sort public static void main(String[] args) { 37 38 39 40 41 42 43 44 45 C# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // 在JDK1.5版本以上,基本数据类型可以自动装箱 // int,double等基本类型的包装类已实现了Comparable接口 Comparable[] c = { 4,9,23,1,45,27,5,2 }; sort(c,0,c.length - 1); for (Comparable data : c) { System.out.println(data); } } } namespace temp{ public class QuickSort{ /// ///排序 /// /// /// /// private static void Sort(int[] numbers,int left,int right){ //左边索引小于右边,则还未排序完成 if (left < right){ //取中间的元素作为比较基准,小于他的往左边移,大于他的往右边移 int middle = numbers[(left + right) / 2]; int i = left - 1; int j = right + 1; while (true){ while (numbers[++i] < middle && i Sort(numbers,left,i - 1); Sort(numbers,j + 1,right); } } /// /// /// /// private static void Swap(int[] numbers,int i,int j){ int number = numbers[i]; numbers[i] = numbers[j]; 35 36 37 38 39 40 41 42 43 44 45 46 47 PHP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 numbers[j] = number; } public static void Main(){ int[] max = { 6,5,2,9,7,4,0 }; Sort(max,0,max.Length-1); StringBuilder temp =new StringBuilder(); for (int i = 0; i < max.Length; i++) temp.Append(max[i].ToString()+\ Console.WriteLine(temp.ToString().Substring(0,temp.Length-1)); Console.ReadLine(); } } } //php快速排序算法 //注意:若从网页中直接复制以下代码,应先去掉每一行开始的空白部分,否则会报 //语法错误,这是因为网页显示是不支持制表符,而产生一段空白。 function quickSort($arr){ $len = count($arr); if($len <= 1) { return $arr; } $key = $arr[0]; $left_arr = array(); $right_arr = array(); for($i=1; $i<$len; $i++){ if($arr[$i] <= $key){ $left_arr[] = $arr[$i]; } else{ $right_arr[] = $arr[$i]; } } $left_arr = quickSort($left_arr); $right_arr = quickSort($right_arr); return array_merge($left_arr, array($key), $right_arr); } $arr = array(49,38,65,97,76,13,27); print_r(quickSort($arr)); //输出:Array( [0] => 13 [1] => 27 [2] => 38 [3] => 49 [4] => 65 [5] => 76 [6] => 97) ?>