黑马程序员技术交流社区

标题: 求帮忙看下,弄了一个小时了,总是溢出 [打印本页]

作者: 天山    时间: 2014-4-17 18:08
标题: 求帮忙看下,弄了一个小时了,总是溢出
package com.itheima;import java.io.*;import java.util.*;public class quick_sort {//我是打算做排序,从键盘输入一10个数字,然后用快速排序,最后输出,        //我的 快速排序总是报错说溢出,除了用Scaner 还有什么方法能从键盘读int 数据                        public static void main(String []args)throws IOException        {                System.out.println("请输入你要排序的10个数字,按回车结束");                        int array[]=new int[10];                                        //DataInputStream dis = new DataInputStream(System.in);   //打算用 字符流从控制台输入 数字                //BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));                                                                                                                                /*for(int i=0;i<array.length;i++)                        {                                array[i]=dis.readInt();                        }*/                                                                        Scanner in=new Scanner(System.in);                        for(int i=0;i<10;i++)                        {                                array[i]=in.nextInt();                        }                                                                                //int array[]={99,56,45,5,89,14,25,69,10,45};                        show_(array);                        //sort_(array,0,array.length-1);   //快速排序                        bubble_sort(array);  //冒泡排序                        System.out.println("  ");                        show_(array);                                                }                                                /*for(int i:array)                {                        array[i]=(int)bufr.readLine();                }        }*/        public static void swap(int a[], int i, int j)         {                if (i == j)                        return;
                int temp = a[i];                        a[i] = a[j];                        a[j] = temp;
        }                /*public static int temp_(int arr[],int low,int high)        {                                int temp=low;                int p=arr[temp];                                for(int i=low+1;i<=high;i++)                {                        if(arr[i]<p)                        {                                temp++;                                swap(arr,temp,i);                        }                                        }                                swap(arr,low,temp);                                return temp;        }        */                                        //快速排序        public static int temp_(int arr[],int low,int high)        {                int key=arr[low];                while(low<high)                {                        while(low<high&&arr[high]>=key)                                high--;                                arr[low]=arr[high];                        while(low<high&&arr[low]<=key)                                low++;                                arr[high]=arr[low];                                }                        arr[low]=key;                        return low;        }                                //冒泡排序        public static void bubble_sort(int arr[])        {                for(int i=0;i<arr.length-1;i++){                        for(int j=0;j<arr.length-1;j++){                                if(arr[j]>arr[j+1]){                                        int t=arr[j];                                        arr [j]=arr[j+1];                                        arr[j+1]=t;                                        }                                }                        }        }                                        //递归        public static void sort_(int arr[],int low,int high)        {                if(low<high)                {                        int p=temp_(arr,low,high);                        temp_(arr,low,p-1);                        temp_(arr,p+1,high);                                        }        }                public static void show_(int arr[])        {                for(int i=0;i<arr.length;i++)                {                        System.out.print(arr[i]+"  ");                }        }/*        public static void sort_(int arr[],int low,int high)        {                if(low<high){                 int a=temp_(arr,low,high);                 temp_(arr,low,a-1);                 temp_(arr,a+1,high);                                }                        }*/        }



作者: 天山    时间: 2014-4-17 18:10
package com.itheima;
import java.io.*;
import java.util.*;
public class quick_sort {
       
               
        public static void main(String []args)throws IOException
        {
                System.out.println("请输入你要排序的10个数字,按回车结束");
                        int array[]=new int[10];
               
                        //DataInputStream dis = new DataInputStream(System.in);   //打算用 字符流从控制台输入 数字
                //BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
                               
                                                                               
                /*for(int i=0;i<array.length;i++)
                        {
                                array[i]=dis.readInt();
                        }*/
                       
                       
                        Scanner in=new Scanner(System.in);
                        for(int i=0;i<10;i++)
                        {
                                array[i]=in.nextInt();
                        }
                       
               
                                        //int array[]={99,56,45,5,89,14,25,69,10,45};
                        show_(array);
                        //sort_(array,0,array.length-1);   //快速排序
                        bubble_sort(array);  //冒泡排序
                        System.out.println("  ");
                        show_(array);
                       
       
                }
               
               
                /*for(int i:array)
                {
                        array[i]=(int)bufr.readLine();
                }
        }*/
        public static void swap(int a[], int i, int j)
        {
                if (i == j)
                        return;

                int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;

        }
       
        /*public static int temp_(int arr[],int low,int high)
        {               
                int temp=low;
                int p=arr[temp];
               
                for(int i=low+1;i<=high;i++)
                {
                        if(arr[i]<p)
                        {
                                temp++;
                                swap(arr,temp,i);
                        }
                       
                }
                                swap(arr,low,temp);
               
                return temp;
        }
        */                                        //快速排序
        public static int temp_(int arr[],int low,int high)
        {
                int key=arr[low];
                while(low<high)
                {
                        while(low<high&&arr[high]>=key)
                                high--;
                                arr[low]=arr[high];
                        while(low<high&&arr[low]<=key)
                                low++;
                                arr[high]=arr[low];
                                }
                        arr[low]=key;
                        return low;
        }
                                //冒泡排序
        public static void bubble_sort(int arr[])
        {
                for(int i=0;i<arr.length-1;i++){
                        for(int j=0;j<arr.length-1;j++){
                                if(arr[j]>arr[j+1]){
                                        int t=arr[j];
                                        arr [j]=arr[j+1];
                                        arr[j+1]=t;
                                        }
                                }
                        }
        }
                                        //递归
        public static void sort_(int arr[],int low,int high)
        {
                if(low<high)
                {
                        int p=temp_(arr,low,high);
                        temp_(arr,low,p-1);
                        temp_(arr,p+1,high);
                       
                }
        }
       
        public static void show_(int arr[])
        {
                for(int i=0;i<arr.length;i++)
                {
                        System.out.print(arr[i]+"  ");
                }
        }
/*        public static void sort_(int arr[],int low,int high)
        {
                if(low<high){
                 int a=temp_(arr,low,high);
                 temp_(arr,low,a-1);
                 temp_(arr,a+1,high);
                       
        }
       
               
}*/
        }

作者: 天山    时间: 2014-4-17 18:11
//我是打算做排序,从键盘输入一10个数字,然后用快速排序,最后输出
作者: Kelvinhu    时间: 2014-4-17 18:13
没问题啊。。在哪溢出了?
作者: 天山    时间: 2014-4-17 18:14
Kelvinhu 发表于 2014-4-17 18:13
没问题啊。。在哪溢出了?

show_(array);
                        //sort_(array,0,array.length-1);   //快速排序
                        bubble_sort(array);  //冒泡排序
                        System.out.println("  ");
                        show_(array);
你把快速排序 注释  删除了,把 冒泡排序的注释掉就看得到了
作者: Kelvinhu    时间: 2014-4-17 18:18
天山 发表于 2014-4-17 18:14
show_(array);
                        //sort_(array,0,array.length-1);   //快速排序
                        bubble_sort(array);  //冒泡排序
  1. package Testpackage;

  2. import java.io.*;
  3. import java.util.*;

  4. public class quick_sort {

  5.         public static void main(String[] args) throws IOException {
  6.                 System.out.println("请输入你要排序的10个数字,按回车结束");
  7.                 int array[] = new int[10];

  8.                 // DataInputStream dis = new DataInputStream(System.in); //打算用 字符流从控制台输入
  9.                 // 数字
  10.                 // BufferedReader bufr=new BufferedReader(new
  11.                 // InputStreamReader(System.in));

  12.                 /*
  13.                  * for(int i=0;i<array.length;i++) { array[i]=dis.readInt(); }
  14.                  */

  15.                 Scanner in = new Scanner(System.in);
  16.                 for (int i = 0; i < 10; i++) {
  17.                         array[i] = in.nextInt();
  18.                 }

  19.                 // int array[]={99,56,45,5,89,14,25,69,10,45};
  20.                 show_(array);
  21.                 sort_(array, 0, array.length - 1); // 快速排序
  22.                 // bubble_sort(array); //冒泡排序
  23.                 System.out.println("  ");
  24.                 show_(array);

  25.         }

  26.         /*
  27.          * for(int i:array) { array[i]=(int)bufr.readLine(); } }
  28.          */
  29.         public static void swap(int a[], int i, int j) {
  30.                 if (i == j)
  31.                         return;

  32.                 int temp = a[i];
  33.                 a[i] = a[j];
  34.                 a[j] = temp;

  35.         }

  36.         /*
  37.          * public static int temp_(int arr[],int low,int high) { int temp=low; int
  38.          * p=arr[temp];
  39.          *
  40.          * for(int i=low+1;i<=high;i++) { if(arr[i]<p) { temp++; swap(arr,temp,i); }
  41.          *
  42.          * } swap(arr,low,temp);
  43.          *
  44.          * return temp; }
  45.          */// 快速排序
  46.         public static int temp_(int arr[], int low, int high) {
  47.                 int key = arr[low];
  48.                 while (low < high) {
  49.                         while (low < high && arr[high] >= key)
  50.                                 high--;
  51.                         arr[low] = arr[high];
  52.                         while (low < high && arr[low] <= key)
  53.                                 low++;
  54.                         arr[high] = arr[low];
  55.                 }
  56.                 arr[low] = key;
  57.                 return low;
  58.         }

  59.         // 冒泡排序
  60.         public static void bubble_sort(int arr[]) {
  61.                 for (int i = 0; i < arr.length - 1; i++) {
  62.                         for (int j = 0; j < arr.length - 1; j++) {
  63.                                 if (arr[j] > arr[j + 1]) {
  64.                                         int t = arr[j];
  65.                                         arr[j] = arr[j + 1];
  66.                                         arr[j + 1] = t;
  67.                                 }
  68.                         }
  69.                 }
复制代码
这样?没问题啊。

作者: 天山    时间: 2014-4-17 18:24
Kelvinhu 发表于 2014-4-17 18:18
这样?没问题啊。

奇怪....明明测试好几遍了,我快速排序方法都写了两遍了
作者: Kelvinhu    时间: 2014-4-17 18:25
天山 发表于 2014-4-17 18:24
奇怪....明明测试好几遍了,我快速排序方法都写了两遍了

贴你报错的溢出的Exception看看?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2