快速排序

爱游戏爱动漫的肥宅 2016-12-15 08:41:23
麻烦各位朋友帮忙看看,下面这个程序的快速排序实现是哪里出了问题。
当数组中有 7 个或以下的数字时,程序正常运行,大于 7 个数时,报下标越界错误。
请大神帮忙修改修改,谢谢
package test;

import java.util.Arrays;

public class SortTest {
public static void main(String[] args) {

// int[] num = {5, 2, 8, 3, 7, 1, 9, 4, 0, 6};
int[] num = {5, 2, 8, 3, 7, 1, 9, 4};

System.out.println("排序前: " + Arrays.toString(num));

num = QuickSort(num, 0, 7);

System.out.println("排序后:" + Arrays.toString(num));

}

// 快速排序
private static int[] QuickSort(int[] num, int low, int high) {
int i = low, j = high;
int temp = num[i];
if (low < high) {
while (i < j) {
while ((num[j] >= temp) && (i < j)) {
j--;
}
num[i] = num[j];
while ((num[i] <= temp) && (i < j)) {
i++;
}
num[j] = num[i];
}
num[i] = temp;

QuickSort(num, low, i );
QuickSort(num, j + 1, high);
}
return num;
}
}
...全文
230 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2016-12-15
  • 打赏
  • 举报
回复
引用 14 楼 yuxiangaaaaa 的回复:
[quote=引用 11 楼 Alias_fa 的回复:] [quote=引用 10 楼 yuxiangaaaaa 的回复:] [quote=引用 9 楼 yuxiangaaaaa 的回复:] [quote=引用 6 楼 Alias_fa 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:]
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。[/quote] 刚看了一下,是因为传值错误了,只要你把那个7减去1就行了,因为数组的位置是从零开始的,最高位的位置是长度减1,改成6就行了,建议改用num = QuickSort(num, 0, num.length-1);[/quote] num的长度是7,即[0]...[6],当出现[7]就是越界[/quote] 可是我代码中的数组里有 8 个数[/quote] QuickSort(num, j + 1, high-1); 这个地方也要-1[/quote] 这样还是不行,只适用一部分数据
自由自在_Yu 2016-12-15
  • 打赏
  • 举报
回复
引用 11 楼 Alias_fa 的回复:
[quote=引用 10 楼 yuxiangaaaaa 的回复:] [quote=引用 9 楼 yuxiangaaaaa 的回复:] [quote=引用 6 楼 Alias_fa 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:]
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。[/quote] 刚看了一下,是因为传值错误了,只要你把那个7减去1就行了,因为数组的位置是从零开始的,最高位的位置是长度减1,改成6就行了,建议改用num = QuickSort(num, 0, num.length-1);[/quote] num的长度是7,即[0]...[6],当出现[7]就是越界[/quote] 可是我代码中的数组里有 8 个数[/quote] QuickSort(num, j + 1, high-1); 这个地方也要-1
  • 打赏
  • 举报
回复
引用 5 楼 qq_24138885 的回复:
public class Test { public static void main(String[] args) { int[] num = { 5, 2, 8, 3, 7, 1, 9, 4, 0, 6 }; System.out.println("排序前: " + Arrays.toString(num)); quickSort(num, 0, num.length - 1); System.out.println("排序后:" + Arrays.toString(num)); } // 交换函数 public static void swap(int a[], int i, int j) { if (i == j) { return; } int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } /** * @param array * 待排序数组 * @param low * 数组下标下界 * @param high * 数组下标上界 * @return temp 临时变量 */ public static int partition(int array[], int low, int high) { // 当前位置为第一个元素所在位置 int p_pos = low; // 采用第一个元素为轴 int temp = array[p_pos]; for (int i = low + 1; i <= high; i++) { if (array[i] < temp) { p_pos++; swap(array, p_pos, i); } } swap(array, low, p_pos); return p_pos; } /** * * 快速排序实现 * * @param array * * @param low * * @param high */ public static void quickSort(int array[], int low, int high) { if (low < high) { int temp = partition(array, low, high); quickSort(array, low, temp - 1); quickSort(array, temp + 1, high); } }
谢谢提供答案
  • 打赏
  • 举报
回复
引用 1 楼 u012934325 的回复:
http://www.cnblogs.com/jym-sunshine/p/5481273.html 你快排写的有问题 不全
谢谢提供答案
  • 打赏
  • 举报
回复
引用 10 楼 yuxiangaaaaa 的回复:
[quote=引用 9 楼 yuxiangaaaaa 的回复:] [quote=引用 6 楼 Alias_fa 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:]
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。[/quote] 刚看了一下,是因为传值错误了,只要你把那个7减去1就行了,因为数组的位置是从零开始的,最高位的位置是长度减1,改成6就行了,建议改用num = QuickSort(num, 0, num.length-1);[/quote] num的长度是7,即[0]...[6],当出现[7]就是越界[/quote] 可是我代码中的数组里有 8 个数
自由自在_Yu 2016-12-15
  • 打赏
  • 举报
回复
引用 9 楼 yuxiangaaaaa 的回复:
[quote=引用 6 楼 Alias_fa 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:]
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。[/quote] 刚看了一下,是因为传值错误了,只要你把那个7减去1就行了,因为数组的位置是从零开始的,最高位的位置是长度减1,改成6就行了,建议改用num = QuickSort(num, 0, num.length-1);[/quote] num的长度是7,即[0]...[6],当出现[7]就是越界
自由自在_Yu 2016-12-15
  • 打赏
  • 举报
回复
引用 6 楼 Alias_fa 的回复:
[quote=引用 2 楼 yuxiangaaaaa 的回复:]
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。[/quote] 刚看了一下,是因为传值错误了,只要你把那个7减去1就行了,因为数组的位置是从零开始的,最高位的位置是长度减1,改成6就行了,建议改用num = QuickSort(num, 0, num.length-1);
  • 打赏
  • 举报
回复
引用 4 楼 m2200 的回复:
给你一段代码参考下:
public class TestPaiXu {
 
    public static void main(String[] args) {
        int[] array = {3,2,6,1,5,9,8,-2};
        zhijie(array);
        maopao(array);
    }
     
    //直接排序
    public static void zhijie(int[] array){
        for(int i=0;i<array.length-1;i++){
            int temp;
            for(int j = i+1;j<array.length;j++){
                if(array[i]>array[j]){
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
         
        for(int a : array){
            System.out.println(a);
        }
    }
     
    //冒泡排序
    public static void maopao(int[] array){
        for(int i=0;i<array.length-1;i++){
            int temp;
            for(int j = 0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
         
        for(int a : array){
            System.out.println(a);
        }
    }
}
然而这里并没有快速排序啊。我现在主要是想弄清楚自己那种写法哪里出了问题。谢谢回答
  • 打赏
  • 举报
回复
引用 3 楼 tzs_1041218129 的回复:
num = QuickSort(num, 0, 7); 你自己写死了7个阿
这不是问题,我运行的时候会根据我数组的个数进行修改的。谢谢回答
  • 打赏
  • 举报
回复
引用 2 楼 yuxiangaaaaa 的回复:
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
我在网上也看到了这段代码,但是我现在想知道自己的那种写法是哪里出错了,谢谢提供答案。
此人似曾相识 2016-12-15
  • 打赏
  • 举报
回复
public class Test { public static void main(String[] args) { int[] num = { 5, 2, 8, 3, 7, 1, 9, 4, 0, 6 }; System.out.println("排序前: " + Arrays.toString(num)); quickSort(num, 0, num.length - 1); System.out.println("排序后:" + Arrays.toString(num)); } // 交换函数 public static void swap(int a[], int i, int j) { if (i == j) { return; } int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } /** * @param array * 待排序数组 * @param low * 数组下标下界 * @param high * 数组下标上界 * @return temp 临时变量 */ public static int partition(int array[], int low, int high) { // 当前位置为第一个元素所在位置 int p_pos = low; // 采用第一个元素为轴 int temp = array[p_pos]; for (int i = low + 1; i <= high; i++) { if (array[i] < temp) { p_pos++; swap(array, p_pos, i); } } swap(array, low, p_pos); return p_pos; } /** * * 快速排序实现 * * @param array * * @param low * * @param high */ public static void quickSort(int array[], int low, int high) { if (low < high) { int temp = partition(array, low, high); quickSort(array, low, temp - 1); quickSort(array, temp + 1, high); } }
爱睡觉的阿狸 2016-12-15
  • 打赏
  • 举报
回复
给你一段代码参考下:
public class TestPaiXu {
 
    public static void main(String[] args) {
        int[] array = {3,2,6,1,5,9,8,-2};
        zhijie(array);
        maopao(array);
    }
     
    //直接排序
    public static void zhijie(int[] array){
        for(int i=0;i<array.length-1;i++){
            int temp;
            for(int j = i+1;j<array.length;j++){
                if(array[i]>array[j]){
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
         
        for(int a : array){
            System.out.println(a);
        }
    }
     
    //冒泡排序
    public static void maopao(int[] array){
        for(int i=0;i<array.length-1;i++){
            int temp;
            for(int j = 0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
         
        for(int a : array){
            System.out.println(a);
        }
    }
}
zhisheng_blog 2016-12-15
  • 打赏
  • 举报
回复
num = QuickSort(num, 0, 7); 你自己写死了7个阿
自由自在_Yu 2016-12-15
  • 打赏
  • 举报
回复
public class quickSort {  
  
  inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  
public quickSort(){  
  
    quick(a);  
  
    for(int i=0;i<a.length;i++)  
  
       System.out.println(a[i]);  
  
}  
  
publicint getMiddle(int[] list, int low, int high) {     
  
            int tmp = list[low];    //数组的第一个作为中轴     
  
            while (low < high) {     
  
                while (low < high && list[high] >= tmp) {     
  
                    high--;     
  
                }     
  
                list[low] = list[high];   //比中轴小的记录移到低端     
  
                while (low < high && list[low] <= tmp) {     
  
                    low++;     
  
                }     
  
                list[high] = list[low];   //比中轴大的记录移到高端     
  
            }     
  
           list[low] = tmp;              //中轴记录到尾     
  
            return low;                   //返回中轴的位置     
  
        }    
  
publicvoid _quickSort(int[] list, int low, int high) {     
  
            if (low < high) {     
  
               int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
  
                _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
  
               _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
  
            }     
  
        }   
  
publicvoid quick(int[] a2) {     
  
            if (a2.length > 0) {    //查看数组是否为空     
  
                _quickSort(a2, 0, a2.length - 1);     
  
        }     
  
       }   
  
}  
墨笙弘一 2016-12-15
  • 打赏
  • 举报
回复
http://www.cnblogs.com/jym-sunshine/p/5481273.html 你快排写的有问题 不全

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧