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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能