package com.sort;public class SortThread implements Runnable {
private String sortName;
private int[] test;
public SortThread(String sortName,int[] table){
this.sortName = sortName;
test = table;
}
public SortThread(){
super();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//产生10万个随机数
int[] test = new int[100000];
for(int i=0;i<100000;i++){
test[i] = (int) (Math.random()*10000);
}
//创建希尔排序线程
SortThread shell = new SortThread("shellSort",test);
Thread shell_thread = new Thread(shell,"希尔排序");
shell_thread.start();
//创建堆排序线程
SortThread heap = new SortThread("heapSort",test);
Thread heap_thread = new Thread(heap,"堆排序");
heap_thread.start();
SortThread quick = new SortThread("quickSort",test);
Thread quick_thread = new Thread(quick,"快速排序");
quick_thread.start();
}
public void run() {
// TODO Auto-generated method stub
int n = test.length;
if(sortName.equals("shellSort")){
shellSort(test,n);
}else if(sortName.equals("quickSort")){
quickSort(test,n,0,n-1);
}else if(sortName.equals("heapSort")){
heapSort(test,n);
}
} //希尔排序算法
public static synchronized void shellSort(int[] table,int n){
long beginTime = System.currentTimeMillis();
System.out.println("希尔排序:beginTime-->"+beginTime);
for(int delta=n/2;delta>0;delta/=2){
for(int i=delta;i<n;i++){
int temp = table[i];
int j;
for(j=i-delta;j>=0&&temp<table[j];j-=delta){
table[j+delta] = table[j];
}
table[j+delta] = temp;
}
}
long time = System.currentTimeMillis() - beginTime;
System.out.println("希尔排序:endTime-->"+System.currentTimeMillis());
System.out.println("希尔排序:amountTim-->"+time);
}
//快速排序算法
public static synchronized void quickSort(int[] table,int n,int low,int high){
long beginTime1 = System.currentTimeMillis();
//System.out.println("快速排序:beginTime-->"+beginTime1);
if(low>=0&&low<n&&high>=0&&high<n&&low<high){
int i=low,j=high;
int vot = table[i];
while(i!=j){
while(i<j&&vot<=table[j]){
j--;
}
if(i<j){
table[i] = table[j];
i++;
}
while(i<j&&table[i]<vot){
i++;
}
if(i<j){
table[j]=table[i];
j--;
}
}
table[i]=vot;
if(low<i)
quickSort(table,n,low,j-1);
if(i<high)
quickSort(table,n,i+1,high);
}
long time1 = System.currentTimeMillis() - beginTime1;
//System.out.println("快速排序:endTime-->"+System.currentTimeMillis());
System.out.println("快速排序:amountTime-->"+time1);
}
//堆排序算法
//创建最小堆
public static void sift(int[] table,int low,int high){
int i = low;
int j = 2*i+1; //左孩子的下标
int temp = table[i];
while(j<=high){
if(j<high&&table[j]>table[j+1]){ //比较左右孩子的值大小
j++;
}
if(temp>table[j]){ //较小的孩子的值和父结点的值
table[i] = table[j];
i = j;
j = 2*i+1;
}else{
j = high + 1; //退出循环
}
}
table[i] = temp;
}
//堆排序
public static synchronized void heapSort(int[] table,int n){
long beginTime = System.currentTimeMillis();
System.out.println("堆排序:beginTime-->"+beginTime);
int i;
//创建最小堆
for(i=n/2-1;i>=0;i--){
sift(table,i,n-1);
}
//每趟将最小的值交换到后面,并再次调整成堆
for(i=n-1;i>0;i--){
int temp = table[0];
table[0] = table[i];
table[i] = temp;
sift(table,0,i-1);
}
long time = System.currentTimeMillis() - beginTime;
System.out.println("堆排序:endTime-->"+System.currentTimeMillis());
System.out.println("堆排序:amountTime-->"+time);
}
}
private String sortName;
private int[] test;
public SortThread(String sortName,int[] table){
this.sortName = sortName;
test = table;
}
public SortThread(){
super();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//产生10万个随机数
int[] test = new int[100000];
for(int i=0;i<100000;i++){
test[i] = (int) (Math.random()*10000);
}
//创建希尔排序线程
SortThread shell = new SortThread("shellSort",test);
Thread shell_thread = new Thread(shell,"希尔排序");
shell_thread.start();
//创建堆排序线程
SortThread heap = new SortThread("heapSort",test);
Thread heap_thread = new Thread(heap,"堆排序");
heap_thread.start();
SortThread quick = new SortThread("quickSort",test);
Thread quick_thread = new Thread(quick,"快速排序");
quick_thread.start();
}
public void run() {
// TODO Auto-generated method stub
int n = test.length;
if(sortName.equals("shellSort")){
shellSort(test,n);
}else if(sortName.equals("quickSort")){
quickSort(test,n,0,n-1);
}else if(sortName.equals("heapSort")){
heapSort(test,n);
}
} //希尔排序算法
public static synchronized void shellSort(int[] table,int n){
long beginTime = System.currentTimeMillis();
System.out.println("希尔排序:beginTime-->"+beginTime);
for(int delta=n/2;delta>0;delta/=2){
for(int i=delta;i<n;i++){
int temp = table[i];
int j;
for(j=i-delta;j>=0&&temp<table[j];j-=delta){
table[j+delta] = table[j];
}
table[j+delta] = temp;
}
}
long time = System.currentTimeMillis() - beginTime;
System.out.println("希尔排序:endTime-->"+System.currentTimeMillis());
System.out.println("希尔排序:amountTim-->"+time);
}
//快速排序算法
public static synchronized void quickSort(int[] table,int n,int low,int high){
long beginTime1 = System.currentTimeMillis();
//System.out.println("快速排序:beginTime-->"+beginTime1);
if(low>=0&&low<n&&high>=0&&high<n&&low<high){
int i=low,j=high;
int vot = table[i];
while(i!=j){
while(i<j&&vot<=table[j]){
j--;
}
if(i<j){
table[i] = table[j];
i++;
}
while(i<j&&table[i]<vot){
i++;
}
if(i<j){
table[j]=table[i];
j--;
}
}
table[i]=vot;
if(low<i)
quickSort(table,n,low,j-1);
if(i<high)
quickSort(table,n,i+1,high);
}
long time1 = System.currentTimeMillis() - beginTime1;
//System.out.println("快速排序:endTime-->"+System.currentTimeMillis());
System.out.println("快速排序:amountTime-->"+time1);
}
//堆排序算法
//创建最小堆
public static void sift(int[] table,int low,int high){
int i = low;
int j = 2*i+1; //左孩子的下标
int temp = table[i];
while(j<=high){
if(j<high&&table[j]>table[j+1]){ //比较左右孩子的值大小
j++;
}
if(temp>table[j]){ //较小的孩子的值和父结点的值
table[i] = table[j];
i = j;
j = 2*i+1;
}else{
j = high + 1; //退出循环
}
}
table[i] = temp;
}
//堆排序
public static synchronized void heapSort(int[] table,int n){
long beginTime = System.currentTimeMillis();
System.out.println("堆排序:beginTime-->"+beginTime);
int i;
//创建最小堆
for(i=n/2-1;i>=0;i--){
sift(table,i,n-1);
}
//每趟将最小的值交换到后面,并再次调整成堆
for(i=n-1;i>0;i--){
int temp = table[0];
table[0] = table[i];
table[i] = temp;
sift(table,0,i-1);
}
long time = System.currentTimeMillis() - beginTime;
System.out.println("堆排序:endTime-->"+System.currentTimeMillis());
System.out.println("堆排序:amountTime-->"+time);
}
}
堆排序:beginTime-->1288610047855
堆排序:endTime-->1288610047885
堆排序:amountTime-->30
Exception in thread "快速排序" java.lang.StackOverflowError
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:100)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:100)
希尔排序:beginTime-->1288610048465
希尔排序:endTime-->1288610048486
希尔排序:amountTim-->21
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
at com.sort.SortThread.quickSort(SortThread.java:102)
quickSort(table,n,low,j-1);
if(i<high)
quickSort(table,n,i+1,high);
如果把排序的元素改为1000个就不会出错,但改为十万个时就报了上面的错误,哪位大侠能帮改一下呢,实在不知道怎么改!!
谢谢!!