您的位置:首页 > 编程语言 > Java开发

笔记:快速排序的java实现,递归的实现

2014-12-30 09:31 423 查看
这只是这几天学习的笔记,参考了别人的博客,为了表示对大牛的感谢,这里标注了其博客地址:http://blog.csdn.net/pzhtpf/article/details/7560294

这几天在看《算法导论》,看懂了算法,决定自己用java去实现一下,因此对快速排序使用java实现了一下,这是第一次自己写的代码。

package MyPackage;

import java.util.ArrayList;

import com.sun.org.apache.bcel.internal.generic.RETURN;

public class quickSort {
private int[] input;
private ArrayList<Integer> temp = new ArrayList<Integer>();
private int front;
private int back;
printSub ps = new printSub();

public quickSort(int[] input){
this.input = input;
}

public int[] start(int[] scr, int first, int end){
front = 0;
back = 0;
if(!temp.isEmpty()){
temp.clear();
}
temp.add(scr[first]);
for(int i=first+1; i<end-first+1; i++){
if(scr[i]<=scr[first]){
temp.add(0, scr[i]);
front++;
}else {
temp.add(scr[i]);
back++;
}
}
System.out.println("send = "+scr[first]+"; front = "+front+"; back = "+back);
System.out.println("first = "+(end-back+1)+"; end = "+end);
int j = 0;
for(int i=first; i<end-first+1; i++){
scr[i]=temp.get(j);
j++;
}

ps.printS(scr);
if(front>1){
start(scr, first, first+front-1);
}
If(back>1){
start(scr, end-back+1, end);
}
return scr;
}

}
</pre><pre name="code" class="java">
其中input和printSub都是自己写的一个类,input是一个1到100的随机数生成器,printSub是一个int数组的打印函数。其运行结果如下:
<pre name="code" class="plain">5 85 77 13 12 18 70 89 94 82
send = 5; front = 0; back = 9
5 85 77 13 12 18 70 89 94 82
send = 85; front = 5; back = 2
5 70 18 12 13 77 85 89 94 82
send = 70; front = 3; back = 0<pre name="code" class="java">package MyPackage;

import java.util.ArrayList;

public class quickSort1 {
private int[] input;
private ArrayList<Integer> temp = new ArrayList<Integer>();
private int front;
private int back;
public int loc;
printSub ps = new printSub();

public quickSort1(int[] input){
this.input = input;
}

public int[] start(int[] scr, int first, int end){

loc = sort(scr, first, end);
if(loc>2){
start(scr, first, loc-1);				//前
}
if(end-loc>2){
start(scr, loc+1, end);					//后
}

ps.printS(scr);

return scr;
}

public int sort(int[] scr,int first,int end){
int tloc;
front = 0;
back = 0;
if(!temp.isEmpty()){
temp.clear();
}
temp.add(scr[first]);
for(int i=first+1; i<end-first+1; i++){
if(scr[i]<=scr[first]){
temp.add(0, scr[i]);
front++;
}else {
temp.add(scr[i]);
back++;
}
}
//		System.out.println("send = "+scr[first]+"; front = "+front+"; back = "+back);
//		System.out.println("first = "+(end-back+1)+"; end = "+end);
int j = 0;
for(int i=first; i<end-first+1; i++){
scr[i]=temp.get(j);
j++;
}

//		ps.printS(scr);

tloc = front+1;

return tloc;
}


5 13 12 18 70 77 85 89 94 82 send = 13; front = 1; back = 05 12 13 18 70 77 85 89 94 82 5 12 13 18 70 77 85 89 94 82


结果完全不对,原因是:会不断改变front和back的值,使得前面运行后后面的形参first和end输入不对。结果一般只运行了前面的第一个循环。
</pre><pre name="code" class="plain">按自己的思路重新修改了一次代码,结果还是有一些问题:
<pre name="code" class="java">package MyPackage;

import java.util.ArrayList;

public class quickSort1 {
private int[] input;
private ArrayList<Integer> temp = new ArrayList<Integer>();
private int front;
private int back;
public int loc;
printSub ps = new printSub();

public quickSort1(int[] input){
this.input = input;
}

public int[] start(int[] scr, int first, int end){

loc = sort(scr, first, end);
if(loc>2){
start(scr, first, loc-1);				//前
}
if(end-loc>2){
start(scr, loc+1, end);					//后
}

ps.printS(scr);

return scr;
}

public int sort(int[] scr,int first,int end){
int tloc;
front = 0;
back = 0;
if(!temp.isEmpty()){
temp.clear();
}
temp.add(scr[first]);
for(int i=first+1; i<end-first+1; i++){
if(scr[i]<=scr[first]){
temp.add(0, scr[i]);
front++;
}else {
temp.add(scr[i]);
back++;
}
}
//		System.out.println("send = "+scr[first]+"; front = "+front+"; back = "+back);
//		System.out.println("first = "+(end-back+1)+"; end = "+end);
int j = 0;
for(int i=first; i<end-first+1; i++){
scr[i]=temp.get(j);
j++;
}

//		ps.printS(scr);

tloc = front+1;

return tloc;
}

结果如下:

<pre name="code" class="plain"><pre name="code" class="plain">input 34 53 71 93 41 12 28 33 27 56
12 27 33 28 34 53 71 93 41 56
Exception in thread "main" java.lang.StackOverflowError
at MyPackage.quickSort1.sort(quickSort1.java:37)
at MyPackage.quickSort1.start(quickSort1.java:19)
at MyPackage.quickSort1.start(quickSort1.java:24)
at MyPackage.quickSort1.start(quickSort1.java:24)

结果直接报错,初步认为是容器不熟悉,具体情况不明。之后原因找到,是限制条件没弄清楚,主要是变量作用域没弄明白,现在也没弄的很清楚,主要参考别人的代码,递归时要注意,主要是递归的形参都是一样的。所以参考了上面提到的博客。

</pre><pre name="code" class="plain"><span style="white-space:pre">	</span>最终代码:
<pre name="code" class="java">package MyPackage;

public class exQuick {

private int seed,small,big;

public int start(int[] list, int first, int end) {
seed = list[first];
small = first + 1;
big = end;

while (big - small > 0) {
if (list[small] <= seed) {
small++;
} else {
list[small] = list[small] + list[big];
list[big] = list[small] - list[big];
list[small] = list[small] - list[big];
big--;
}
}
//		System.out.println("seed = " + seed + "; small = " + small + "; big = "
//				+ big);
if (list[small] <= list[first]) {
//			list[first] = list[first] + list[small];
//			list[small] = list[first] - list[small];
//			list[first] = list[first] - list[small];
int t = list[first];
list[first] = list[small];
list[small] = t;
seed = small;
} else {
//			list[first] = list[first] + list[small - 1];
//			list[small - 1] = list[first] - list[small - 1];
//			list[first] = list[first] - list[small - 1];
int t = list[first];
list[first] = list[small-1];
list[small-1] = t;
seed = small-1;
}

//		for(int i=0;i<list.length;i++){
//			System.out.print(list[i]+" ");
//		}
//		System.out.println();
//		System.out.println("loc = " + seed);
return seed;
}

}

package MyPackage;

import java.util.ArrayList;

public class quickSort1 {
private int[] input;
private ArrayList<Integer> temp = new ArrayList<Integer>();
exQuick eq;
private int front;
private int back;
public int loc;
printSub ps = new printSub();

public quickSort1(int[] input) {
this.input = input;
this.eq = new exQuick();
}

public int[] start(int[] scr, int first, int end) {

// exQuick eq = new exQuick();
// loc = sort(scr, first, end);
if (first < end) {
loc = eq.start(scr, first, end);
//			if (loc > 2) {
start(scr, first, loc - 1); // 前
//			}
//			if (end - loc > 2) {
start(scr, loc + 1, end); // 后
//			}
}
//		ps.printS(scr);

return scr;
}

// public int sort(int[] scr,int first,int end){
//
// int tloc;
// front = 0;
// back = 0;
// if(!temp.isEmpty()){
// temp.clear();
// }
// temp.add(scr[first]);
// for(int i=first+1; i<end-first+1; i++){
// if(scr[i]<=scr[first]){
// temp.add(0, scr[i]);
// front++;
// }else {
// temp.add(scr[i]);
// back++;
// }
// }
// //
// System.out.println("send = "+scr[first]+"; front = "+front+"; back = "+back);
// // System.out.println("first = "+(end-back+1)+"; end = "+end);
// int j = 0;
// for(int i=first; i<end-first+1; i++){
// scr[i]=temp.get(j);
// j++;
// }
//
// // ps.printS(scr);
//
// tloc = first+front;
//
// return tloc;
// }

// public int[] sort1(int[] scr, int first, int end){
// int temp;
//
// temp = first;
// for(int i=first+1; i<end; i++){
//
// if(scr[temp] > scr[i]){
// scr[temp] = scr[temp]+scr[i];
// scr[i] = scr[temp]-scr[i];
// scr[temp] = scr[temp]-scr[i];
// temp = i;
// }
// }
//
// return scr;
// }

}

终于完成了,主要是没有加入first和end的检查,发现了自己对java中的参数作用域了解没有完全,开始一直以为自己是懂了的。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息