有一个题目,将各种排序的代码放入数组代码中排序,并要测算各个排序算法运行的时间,时间问题不是很主要,因为我这个快速排序就无法像其他排序代码一样放入数组代码中,还请各位指教!
public class QuickSort {
private static void quickSort(int[] array, int low, int high) {
if (low >= high) { //请说明为何要使用如此的判断条件
return; //这里返回什么?
}
int i = low, j = high, key = array[i]; // 默认数组的第一个数为基准数据
while (i < j) {
while (i < j && array[j] >= key) { //请说明为何要使用如此的判断条件
j--;
}
if (i < j) {
array[i++] = array[j]; //
}
while (i < j && array[i] < key) {//
i++;
}
if (i < j) {
array[j--] = array[i]; //
}
}
array[i] = key; //
quickSort(array, low, i - 1); // 使用递归的方法,对基准数左边的序列进行快速排序
quickSort(array, i + 1, high); // 使用递归的方法,对基准数左边的序列进行快速排序
}
public static void quickSort(int[] array) {
if (array == null || array.length == 0) {//请说明为何要使用如此的判断条件
return;
}
quickSort(array, 0, array.length - 1); //为什么是array.length - 1?
}
}
public class chap02 {
public static void main(String[]args) {
System.out.println("请输入你要进行排序的数字个数:");
Scanner input1 = new Scanner(System.in);
int num =input1.nextInt();
System.out.println("请选择数组出现的方式 1.手动输入 2.随机生成:");
Scanner in = new Scanner(System.in);
int s=in.nextInt();
int[] a=new int[num];
if(s==1) {
System.out.println("请输入"+num+"个需要排序的整数:");
Scanner inp=new Scanner(System.in);
for(int i=0;i<a.length;i++) {
int temp=inp.nextInt();
a[i]=temp;
while( a[i]>1000|a[i]<1){
System.out.println("请输入1-1000的数");
return;
}
}
inp.close();
}else if(s == 2) {
int k = 0;
while (k <= num - 1) {
int t = (int) (Math.random() * 1000);
boolean b = false;
for (int i = 0; i < a.length; i++) {
if (t == a[i]) {
b = true;
}
}
if (b != true) {
a[k] = t;
k++;
}
}
} else{
System.out.println("请输入1或2!!!");
return ;
}
in.close();
input1.close();
int [] templist1 = a.clone();
System.out.println("初始状态"+Arrays.toString(a)+"下面进行插入排序");
int[] b = InserSort(templist1);
System.out.println("经过插入排序后的最终状态"+Arrays.toString(b));
int [] templist2 = a.clone();
System.out.println("\n\n初始状态"+Arrays.toString(a)+"下面进行冒泡排序");
int []c=BubbleSort(templist2);
System.out.println("经过冒泡排序后最终状态"+Arrays.toString(c));
int [] templist3 = a.clone();
System.out.println("\n\n初始状态"+Arrays.toString(a)+"下面进行选择排序");
int []d=SelectionSort(templist3);
System.out.println("经过选择排序后最终状态"+Arrays.toString(d));
int [] templist4 = a.clone();
System.out.println("\n\n初始状态"+Arrays.toString(a)+"下面进行希尔排序");
int []e=ShellSort(templist4);
System.out.println("经过希尔排序后最终状态"+Arrays.toString(e));
}
public static int[]InserSort(int []a){
int i,j,num=0;
for(i=1;i<a.length;i++) {
for(j=0;j<=i;j++) {
if(a[i]<a[j]) {
int temp=a[i];
for(int k=i;k>j;k--) {
a[k]=a[k-1];
}
a[j]=temp;
}
}
System.out.println("第"+(++num)+"趟排序"+Arrays.toString(a));
}
return a;
}
public static int[]BubbleSort(int []a){
int i,j,num=0;
for (i =a.length -1; i >0 ; i--) { // 比较顺序 第一遍:找出最小值给索引为9的数组
for (j = 0; j < i; j++) {
if(a[j] > a[j+1]){ //当前面的索引小于后面的索引,把两个数据值调换
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
System.out.println("第"+(++num)+"趟排序"+Arrays.toString(a));
}
return a;
}
public static int[]SelectionSort(int[]a){
int i,j,num=0;
for(i=0;i<a.length;i++) {
for(j=i+1;j<a.length;j++) {
if(a[i]>a[j]) {
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.println("第"+(++num)+"趟排序"+Arrays.toString(a));
}
return a;
}
public static int[]ShellSort(int[]a){
int i,j,g,num = 0,temp=0;
for(g=a.length/2;g>0;g/=2) {
for(i=g;i<a.length;i++) {
j=i;
temp=a[j];
if(a[j]<a[j-g]) {
while(j-g>=0&&temp<a[j-g]){
a[j]=a[j-g];
j-=g;
}
a[j]=temp;
}
}
System.out.println("第"+(++num)+"趟排序"+Arrays.toString(a));
}
return a;
}
}
为啥不能放数组里