快速排序如何实现?

yeyuqiudui 2009-06-05 10:37:38
1:用两个数组,在确定轴值之后,来回交换移动,最终实现快速排序(能搜到的都是在一个数组内来回交换移动):
2:要求详细源代码;
3:java实现;
4:万分感谢!!!
...全文
476 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddyouyue 2009-06-06
  • 打赏
  • 举报
回复
以前学数据结构的时候用C做过...
光宇广贞 2009-06-06
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yeyuqiudui 的回复:]
对一个数组内的内容进行排序,通过两个数组(一个辅助数组存放移动的数值)
[/Quote]

同学,快排是本地排序,不需要另一个数组,说出你的需求。这是哪个缺德的给你出的题,抑或是你自己想出来的?
bigbug9002 2009-06-06
  • 打赏
  • 举报
回复
上面复制程序时,我按了两次ctrl +c。不好意思。
bigbug9002 2009-06-06
  • 打赏
  • 举报
回复
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,这样效率高了,而且不用两个数组了,但代码要写好多。
yeyuqiudui 2009-06-06
  • 打赏
  • 举报
回复
对一个数组内的内容进行排序,通过两个数组(一个辅助数组存放移动的数值)
sachmx1234 2009-06-06
  • 打赏
  • 举报
回复
scliuqiang 2009-06-06
  • 打赏
  • 举报
回复

/**
* 划分算法 以枢纽为关键字把数组划分为,小于关键字的一组和大于关键字的一组
*
* @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
bigbug9002 2009-06-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xzy88 的回复:]
引用 4 楼 bigbug9002 的回复:
为什么要用两个数组呢?


我也费解,可能楼主有两个数组并对两个数组中的内容排完序,最后生成一个排完序的数组吧,这样的话,方法就多了,最好理解的是先把两个
数组的内容都放在一个临时数组中去,再对这个数组排序就行了.
[/Quote]

那用归并排序好了,何必用快速排序?

快速排序本来就是用一个数组就完成的。
lulu0126 2009-06-06
  • 打赏
  • 举报
回复
收藏9L代码!
goodmrning 2009-06-06
  • 打赏
  • 举报
回复
我这里有快速排序的算法演示,楼主可以参考下!
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); // 排序右半边
}
}


}
L502650 2009-06-06
  • 打赏
  • 举报
回复
抵制问作业...
seaman_xh 2009-06-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bigbug9002 的回复:]
为什么要用两个数组呢?
[/Quote]

快排只需要一个数组,归并排序才需要第二个数组
三仙半 2009-06-06
  • 打赏
  • 举报
回复
快排是可以做的,但是,楼主要怎么快排啊?要求确实不是很清楚啊。
网络科技 2009-06-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bigbug9002 的回复:]
为什么要用两个数组呢?
[/Quote]
我也费解,可能楼主有两个数组并对两个数组中的内容排完序,最后生成一个排完序的数组吧,这样的话,方法就多了,最好理解的是先把两个
数组的内容都放在一个临时数组中去,再对这个数组排序就行了.
linshaoang 2009-06-06
  • 打赏
  • 举报
回复
同意15楼


--------------------------------------------
http://www.pkwutai.cn
bocai17 2009-06-06
  • 打赏
  • 举报
回复
完美解决终极版
我不明白楼主的行为,你的目的不就是为了排序吗?干嘛用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));
}

}
bigbug9002 2009-06-06
  • 打赏
  • 举报
回复
为什么要用两个数组呢?
zxcv_168 2009-06-05
  • 打赏
  • 举报
回复
...................
knightzhuwei 2009-06-05
  • 打赏
  • 举报
回复
那么对什么排序呢?两个数组还是一个数组?

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧