quick sort
1
using System;
2
namespace MyQSort{
3
public class QSort //可以写成static public class QSort
4
{
5
private static int[] toBeSort;
6
private static void swap(int a,int b){
7
int c;
8
c=toBeSort[a];
9
toBeSort[a]=toBeSort[b];
10
toBeSort[b]=c;
11
}
12
///<sumary>
13
///分割函数
14
///</sumary>
15
private static int Partition(int low,int high){
16
int pivoKey=toBeSort[low];
17
while(low<high){
18
while(low<high && (toBeSort[high] >= pivoKey)) --high;
19
swap(low,high);
20
while(low<high && (toBeSort[low] <= pivoKey)) ++low;
21
swap(low,high);
22
}
23
return low;
24
}
25
private static void QQSort(int low,int high){
26
int pivoLoc;
27
if(low<high){
28
pivoLoc=Partition(low,high);
29
QQSort(low,pivoLoc-1);
30
QQSort(pivoLoc+1,high);
31
}
32
}
33
public static int[] QuickSort(int [] a){
34
toBeSort=a;
35
QQSort(0,toBeSort.Length-1);
36
return toBeSort;
37
}
38![]()
39
};
40
}
41![]()
using System;2
namespace MyQSort{3
public class QSort //可以写成static public class QSort4
{5
private static int[] toBeSort;6
private static void swap(int a,int b){7
int c;8
c=toBeSort[a];9
toBeSort[a]=toBeSort[b];10
toBeSort[b]=c;11
}12
///<sumary>13
///分割函数14
///</sumary>15
private static int Partition(int low,int high){16
int pivoKey=toBeSort[low];17
while(low<high){18
while(low<high && (toBeSort[high] >= pivoKey)) --high;19
swap(low,high);20
while(low<high && (toBeSort[low] <= pivoKey)) ++low;21
swap(low,high);22
}23
return low;24
}25
private static void QQSort(int low,int high){26
int pivoLoc;27
if(low<high){28
pivoLoc=Partition(low,high);29
QQSort(low,pivoLoc-1);30
QQSort(pivoLoc+1,high);31
}32
}33
public static int[] QuickSort(int [] a){34
toBeSort=a;35
QQSort(0,toBeSort.Length-1);36
return toBeSort;37
}38

39
};40
}41





浙公网安备 33010602011771号