⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch8_e8_22.java

📁 《java语言与面向对象程序设计题解及实验指导》源代码
💻 JAVA
字号:
import java.io.*;
import java.util.*; 

public class ch8_e8_22
{
    public static void main(String args[])
    {
        final int NUMBER = 10;
        Vector dataVector = new Vector();
        
        try
        {
            BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
            System.out.println("请输入" + NUMBER +"个整数");
            //插入合适的位置
            for(int i=0; i<NUMBER; i++)
            {
                int temp = Integer.parseInt(br.readLine());
                int low = 0, high = i-1, mid=0;
                while(low<=high)
                {
                    System.out.println(low+","+mid+","+high);
                    mid = (low + high)/2;
                    if(((Integer)dataVector.get(mid)).intValue() == temp)
                    {
                        dataVector.insertElementAt(new Integer(temp),mid);
                        break;
                    }
                    else if(((Integer)dataVector.get(mid)).intValue() > temp)
                    {
                        high = mid - 1;
                    }
                    else
                    {
                        low = mid + 1;
                    }
                }
                if(low>high)
                {
                    dataVector.insertElementAt(new Integer(temp),low);
                }
            }
            //输出
            System.out.println("\n升序的排序结果为:");
            for(int i=0; i<NUMBER; i++)
            {
                System.out.print(dataVector.get(i).toString() + "\t");
            }
        }
        catch(NumberFormatException nfe)
        {
            System.out.println(nfe.toString());
            System.out.println("整数格式输入错误。");
        }
        catch(IOException ioe)
        {
            System.out.println(ioe.toString());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -