weixin_42332083 2009-08-07 22:41
浏览 246
已采纳

一段快速排序程序,栈溢出,请高手给查下错

这段程序是我看过别的代码后自己想着写的,可是老出错,小弟水平浅看不出来,请各位帮忙看看,顺便指点一下小弟的排序算法
多谢了!

public class TestSort {

public static void main(String[] args) {

    int[] i_arr = new int[]{ 12, 4, 8, 55, 6, 78, 43, 45, 6, 8 };
    //bubbleSort(i_arr);
    //selectionSort(i_arr);
    //insertSort(i_arr);
    quickSort(i_arr, 0, i_arr.length - 1);
    for(int i=0; i<i_arr.length; i++) {
        System.out.print(i_arr[i] + " ");
    }
}

public static void quickSort(int[] a, int low, int high) {
    if(low >= high)
        return;
    int start = low;
    int end = high;
    int pivot = a[low];

    while(true) {
        while(a[end] >= pivot)
            end --;
        while(a[start] <= pivot)
            start ++;
        if(start >= end)
            break;
        swap(a, start, end);
    }
    quickSort(a, low, start - 1);
    quickSort(a, end + 1, high);
}
public static void swap(int[] a, int b, int c) {
    int temp = a[b];
    a[b] = a[c];
    a[c] = temp;
}

}

  • 写回答

1条回答 默认 最新

  • ghost1000 2009-08-07 22:53
    关注

    [code="java"]
    static void BubbleSort(int a []){
      int temp=0;
      for (int i = 0; i < a.length ; i++) {
      for (int j = 0; j < a.length - i - 1; j++){
      if (a[j]>a[j + 1]){ //把这里改成大于,就是升序了
      temp=a[j];
      a[j]=a[j + 1];
      a[j + 1]=temp;
      }
      }
      }
      }[/code]
    这是比较经典的排序算法,你写的那个我也没怎么看懂,不过看起来有点麻烦。
    不过思路很好。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题