登录
  • #刷题
  • #学java/c#

求一个算法的解答

donnice
1589
12
public class SYX_Quicksort{

public static void quicksort(int[] a){[br]
quicksort(a,0,a.length-1);

}

public static int median3(int[] a, int left, int right){[br]
int center = (left + right)/2;

if(a
<a[left])

SwapReferences(a,left,center);

if(a
<a[left])

SwapReferences(a,left,right);

if(a
<a
)

SwapReferences(a,center,right);

SwapReferences(a,center,right-1);

return a[right-1];

}

public static void quicksort(int[] a, int left, int right){[br]
int pivot = median3(a,left,right);

int i = left;

int j = right - 1;

for(;;){

while(a[​++i] < pivot){}

while(a[--j] > pivot){}

if(i<j)

SwapReferences(a,i,j);

else

break;

}

SwapReferences(a,i,right-1);

quicksort(a,left,i-1);

quicksort(a,i+1,right);

}

public static void SwapReferences(int[] a, int i, int j){[br]
int temp = a;

a = a[j];

a[j] = temp;

}

public static void main(String[] args){[br]
int[] arr = {12,67,32,76,87,54,32,87,34,18,12};[br]
quicksort(arr);

for(int i = 0; i<arr.length-1; i++)

System.out.print(arr);

}

}

很不好意思的来求解答,上面是我所编写的快速排序的程序

已经通过编译器了,但不知为何一直显示下标溢出,我查了半天没发现什么错误……求解答
12条回复
热度排序

发表回复