Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2160506
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2011-10-22 12:47:40

  1. package com.itcast.sort;
  2. import java.io.*;
  3. public class MonkeySort {
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args) {
  8.          int size=5;
  9.          //对象数组
  10.         Monkey []monkeys=new Monkey[size];
  11.         InputStreamReader isr=new InputStreamReader(System.in); //读取的是字符流
  12.         BufferedReader br=new BufferedReader(isr);
  13.         for(int i=0;i<monkeys.length;i++)
  14.         {
  15.              System.out.println("请输入第"+(i+1)+"个猴子的高度:");
  16.              try {
  17.                 String height=br.readLine();
  18.                 monkeys[i]=new Monkey((i+1)+"",Float.parseFloat(height));
  19.             } catch (IOException e) {
  20.                 
  21.                 e.printStackTrace();
  22.             }
  23.         }
  24.         //让老猴子排序
  25.         Monkey oldMonkey=new Monkey("1000",1.2f); //1.2是double类型的,转成float类型
  26.         //排序
  27.         //oldMonkey.bubbleSort(monkeys);
  28.         //oldMonkey.selectSort(monkeys);
  29.         //插入排序发
  30.         oldMonkey.insertSort(monkeys);
  31.         //验证效果 输出
  32.         oldMonkey.show(monkeys);
  33.     }
  34. }

  35. //猴子类
  36. class Monkey
  37. {
  38.      private String monkeyId;
  39.      private float height;
  40.      public Monkey(String monkeyId,float height){
  41.          this.monkeyId=monkeyId;
  42.          this.height=height;
  43.      }
  44.      //排队 冒泡排序 第一关
  45.      public void bubbleSort(Monkey []monkeys){
  46.         
  47.          float tempHeight=0.0f;
  48.          String tempNo="";
  49.          for(int i=0;i<monkeys.length-1;i++){
  50.              for(int j=0;j<monkeys.length-1-i;j++){
  51.                  if(monkeys[j].height>monkeys[j+1].height){
  52.                      //交换身高
  53.                      tempHeight=monkeys[j].height;
  54.                      monkeys[j].height=monkeys[j+1].height;
  55.                      monkeys[j+1].height=tempHeight;
  56.                     
  57.                      //交换编号
  58.                      tempNo=monkeys[j].monkeyId;
  59.                      monkeys[j].monkeyId=monkeys[j+1].monkeyId;
  60.                      monkeys[j+1].monkeyId=tempNo;
  61.                  }
  62.              }
  63.          }
  64.      }
  65.     
  66.      //第二关
  67.      public void selectSort(Monkey []monkeys){
  68.          float tempHeight=0.0f;
  69.          String tempNo="";
  70.          for(int i=0;i<monkeys.length;i++){
  71.              //认为下标是i的猴子身高是最低的
  72.              float minHeight=monkeys[i].height;
  73.              int minIndex=i;
  74.              //和后面的比较
  75.              for(int j=i+1;j<monkeys.length;j++){
  76.                  if(minHeight>monkeys[j].height){
  77.                      //修正一下最小值
  78.                      minHeight=monkeys[j].height;
  79.                      minIndex=j;
  80.                  }
  81.              }
  82.              //交换
  83.              //交换身高
  84.                  if(minIndex!=i){ //一个小小的性能优化
  85.                      tempHeight=monkeys[minIndex].height;
  86.                      monkeys[minIndex].height=monkeys[i].height;
  87.                      monkeys[i].height=tempHeight;
  88.                     
  89.                      //交换编号
  90.                      tempNo=monkeys[minIndex].monkeyId;
  91.                      monkeys[minIndex].monkeyId=monkeys[i].monkeyId;
  92.                      monkeys[i].monkeyId=tempNo;
  93.                  }
  94.          }
  95.      }
  96.     
  97.      //插入排序 第三关
  98.      public void insertSort(Monkey []monkeys){
  99.          //开始排序
  100.          for(int i=1;i<monkeys.length;i++)
  101.          {
  102.              float insertHeight=monkeys[i].height; //先记录该猴子的身高
  103.              String insertNo=monkeys[i].monkeyId;
  104.              int insertIndex=i-1;
  105.              while(insertIndex>=0&&monkeys[insertIndex].height>insertHeight)
  106.              {
  107.                  monkeys[insertIndex+1].height=monkeys[insertIndex].height;
  108.                  monkeys[insertIndex+1].monkeyId=monkeys[insertIndex].monkeyId;
  109.                  insertIndex--;
  110.              }
  111.              //插入
  112.              monkeys[insertIndex+1].height=insertHeight;
  113.              monkeys[insertIndex+1].monkeyId=insertNo;
  114.          }
  115.      }
  116.     
  117.      //显示队列
  118.      public void show(Monkey []monkeys){
  119.          for(int i=0;i<monkeys.length;i++){
  120.          System.out.println("猴子编号:"+monkeys[i].monkeyId+",身高:"+monkeys[i].height);
  121.          }
  122.      }
  123. }
阅读(1069) | 评论(0) | 转发(0) |
0

上一篇:短信猫开发

下一篇:赛迪网笔试题

给主人留下些什么吧!~~