快速排序如何实现? 1:用两个数组,在确定轴值之后,来回交换移动,最终实现快速排序(能搜到的都是在一个数组内来回交换移动):2:要求详细源代码;3:java实现;4:万分感谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这里有快速排序的算法演示,楼主可以参考下!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); // 排序右半边 } }} /** * 划分算法 以枢纽为关键字把数组划分为,小于关键字的一组和大于关键字的一组 * * @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 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(""); }}这个程序是根据严蔚敏 吴伟民编著的数据结构(c语言)写的。如果要让程序能对int ,long ,char,short,float,double等原子类型快速排序,要重载quickSort()。个人的思路是在重载的quickSort中把原子类型转为对应的包装类型。排完后,再由对包装类型。这时真的要两个数组了:)。但这样时间效率不太高。也可重载quickSort,partition,qSort,这样效率高了,而且不用两个数组了,但代码要写好多。 上面复制程序时,我按了两次ctrl +c。不好意思。 完美解决终极版我不明白楼主的行为,你的目的不就是为了排序吗?干嘛用2个数组,干嘛还要轴,干嘛不用现成的方法我给出我的答案,当然我对你的排序理解为升序,如果要降序,需要重写这个类的equals和compareTo方法我就懒得重写了,实在没意思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)); }} 同意15楼 --------------------------------------------http://www.pkwutai.cn javaGUI的登录窗口怎么连接数据库验证密码平弹出新窗口 java聊天室发送表情问题 关于Collection的一个问题 outputStream怎么实现Mark();Reset(); 菜鸟一问:环境设置问题 大家帮个忙告诉我 !!!!!指点指点我吧!了解snmp通信常识#####确实很急!帮忙一下拉! 要深入学习java应该选什么教程好呢? 如何取得曲线上点座标值 如何得到数据库中数据的位数呢?高手快来,散分了!!!! java小问题:在dos里输入,在一个图形界面窗口输出,怎么弄? 关于switch的问题
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); // 排序右半边
}
}
}
/**
* 划分算法 以枢纽为关键字把数组划分为,小于关键字的一组和大于关键字的一组
*
* @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 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("");
}
}这个程序是根据严蔚敏 吴伟民编著的数据结构(c语言)写的。
如果要让程序能对int ,long ,char,short,float,double等原子类型快速排序,要重载quickSort()。个人的思路是在重载的quickSort中把原子类型转为对应的包装类型。排完后,再由对包装类型。这时真的要两个数组了:)。但这样时间效率不太高。
也可重载quickSort,partition,qSort,这样效率高了,而且不用两个数组了,但代码要写好多。
我不明白楼主的行为,你的目的不就是为了排序吗?干嘛用2个数组,干嘛还要轴,干嘛不用现成的方法
我给出我的答案,当然我对你的排序理解为升序,如果要降序,需要重写这个类的equals和compareTo方法
我就懒得重写了,实在没意思
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));
}}
--------------------------------------------
http://www.pkwutai.cn