62,615
社区成员
发帖
与我相关
我的任务
分享
public class QSort{
public static void quickSort(Comparable[] a ){
qSort(a,0,a.length-1);
}
private static void qSort(Comparable[] a,int low,int high){
int pivotLocation;
if(low<high){
pivotLocation=partition(a,low,high);
qSort(a,low,pivotLocation-1);
qSort(a,pivotLocation+1,high);
}
}
private static int partition(Comparable[] a,int low,int high){
Comparable pivot=a[low];
while(low<high){
while(low<high && a[high].compareTo(pivot)>=0) high--;
a[low]=a[high];
while(low<high && a[low].compareTo(pivot)<=0) low++;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
public static void main(String[] args){
String[] s1={"two","one","three","four","five","seven"};
quickSort(s1);
for(int i=0;i<s1.length;i++){
System.out.print(s1[i]+" ");
}
System.out.println("");
}
}public class QSort{
public static void quickSort(Comparable[] a ){
qSort(a,0,a.length-1);
}
private static void qSort(Comparable[] a,int low,int high){
int pivotLocation;
if(low<high){
pivotLocation=partition(a,low,high);
qSort(a,low,pivotLocation-1);
qSort(a,pivotLocation+1,high);
}
}
private static int partition(Comparable[] a,int low,int high){
Comparable pivot=a[low];
while(low<high){
while(low<high && a[high].compareTo(pivot)>=0) high--;
a[low]=a[high];
while(low<high && a[low].compareTo(pivot)<=0) low++;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
public static void main(String[] args){
String[] s1={"two","one","three","four","five","seven"};
quickSort(s1);
for(int i=0;i<s1.length;i++){
System.out.print(s1[i]+" ");
}
System.out.println("");
}
}
/**
* 划分算法 以枢纽为关键字把数组划分为,小于关键字的一组和大于关键字的一组
*
* @param array
* 划分的目标数组
* @param left
* 左边开始位置
* @param right
* 右边开始位置
* @param key
* 枢纽关键字
* @return 枢纽的正确位置
*/
private int partition(int[] array, int left, int right, int key) {
int indexRight = right - 1;
while (true) {
while (key > array[left])
left++;
while (right >= 0 && key <= array[right])
right--;
if (left >= right) {
break;
} else {
swap(array, left, right);
}
}
swap(array, indexRight, left);
return left;
}
/**
* 中值划分,获取中值,并排序各项
*
* @param array
* 需要排序的数组
* @param left
* 数组左端开始处
* @param right
* 数组右端开始出
* @return 数组中值
*/
private int getCenter(int[] array, int left, int right) {
int center = (left + right) / 2;
if (array[left] > array[center]) {
swap(array, left, center);
}
if (array[left] > array[right]) {
swap(array, left, right);
}
if (array[center] > array[right]) {
swap(array, center, right);
}
swap(array, center, right - 1);
return array[right - 1];
}
/**
* 快速排序-递归的划分数组
*
* @param array
* 排序的数组
* @param left
* 左边开始排序位置
* @param right
* 右边开始排序位置
*/
private void quiklySort(int[] array, int left, int right) {
int size = right - left + 1;
if(size <= 1) {
return;
}else if(size == 2){
if (array[left] > array[right]) {
swap(array, left, right);
}
}else if(size == 3){
getCenter(array, left, right);
}else{
int center = getCenter(array, left, right);
int partition = partition(array, left, right, center);
quiklySort(array, left, partition - 1);
quiklySort(array, partition + 1, right);
}
}
/**
* 快速排序-递归
*
* @param array
* 排序的数组
*/
public void quickSort(int[] array) {
quiklySort(array, 0, array.length - 1);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/scliuqiang/archive/2009/05/08/4161402.aspx
public class QuickSort
{
/**
* JAVA排序算法实现代码-快速(Quick Sort)排序。
*/
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
System.out.print("排序前: ");
for (int i = 0; i < a.length; i++)
System.out.printf("%3s", a[i]);
System.out.println("");
int Index = a.length;
Quicksort(0, Index - 1, Index); // 快速排序
// 排序后结果
System.out.print("排序后: ");
for (int i = 0; i < a.length; i++)
System.out.printf("%3s", a[i]);
System.out.println("");
}
public static void Quicksort(int Left, int Right, int Index) {
int i, j, k; // 循环计数变量
int Pivot; // 枢纽变量
int Temp; // 暂存变量
i = Left; // 设定左指针
j = Right; // 设定右指针
Pivot = a[Left]; // 取最左边的元素
if (i < j) {
do {
while (a[i] <Pivot && i < Right) // 从左往右找比Pivot大的值
{
i++;
}
while (a[j] > Pivot && j > Left) // 从右往左找比Pivot小的值
{
j--;
}
if (i < j) // 交换a[i]和a[j]
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
}
} while (i < j);
if (i > j) {
Temp = a[Left]; // 交换a[Left]和a[j]
a[Left] = a[j];
a[j] = Temp;
// 打印目前排序结果
System.out.print("排序中: ");
for (k = 0; k <= Index; k++) {
System.out.printf("%3s", a[k]);
}
System.out.println("");
}
Quicksort(Left, j - 1, Index); // 排序左半边
Quicksort(j + 1, Right, Index); // 排序右半边
}
}
}
import java.util.*;
public class TestSort
{
public static void main(String[] args)
{
int[] a={1,22,4,456,5,77,9999,888};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}