材料:猴子排序,按照身高来从小到大来排序。
第一关:
老猴子带领小猴子队伍按大小逐一比较,交换,开始高矮排列队伍。(冒泡排序)
第二关:
太慢了,给第一关增加难度,进行选择排序
第三关:
最后,尝试选择用插入排序法,来进行排序。
测试类:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //功能:猴子排序 public class MonkeySelect { public static void main(String[] args) { // TODO Auto-generated method stub // 定义一个可以装5只猴子的对象数组 int size = 5; Monkey[] monkeys = new Monkey[size]; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); // 初始化各个猴子 for (int i = 0; i < monkeys.length; i++) { System.out.println("请输入第" + (i + 1) + "只猴子的高度"); try { String height = br.readLine(); monkeys[i] = new Monkey((i + 1) + "", Float.parseFloat(height)); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 让老猴子排序 Monkey oldMonkey = new Monkey("1000", 1.2f); // 冒泡排序 oldMonkey.bubbleSort(monkeys); // oldMonkey.selectSort(monkeys);选择排序调用 // oldMonkey.insertSort(monkeys);插入排序调用 // 验证 oldMonkey.show(monkeys); } }
猴子类(功能):
// 猴子 class Monkey { private String monkeyId; private float height; public Monkey(String monkeyId, float height) { this.monkeyId = monkeyId; this.height = height; } // 插入排序 public void insertSort(Monkey[] monkeys) { // 开始排序 for (int i = 0; i < monkeys.length; i++) { // 先记录该猴子的身高 float insertHeight = monkeys[i].height; String insertNo = monkeys[i].monkeyId; int insertIndex = i - 1; while (insertIndex >= 0 && monkeys[insertIndex].height > insertHeight) { monkeys[insertIndex + 1].height = monkeys[insertIndex].height; monkeys[insertIndex + 1].monkeyId = monkeys[insertIndex].monkeyId; insertIndex--; } // 插入 monkeys[insertIndex + 1].height = insertHeight; monkeys[insertIndex + 1].monkeyId = insertNo; } } // 选择排序 public void selectSort(Monkey[] monkeys) { float tempHeight = 0.0f; String tempNo = ""; for (int i = 0; i < monkeys.length; i++) { float minHeight = monkeys[i].height; int minIndex = i; // 和后面的比较 for (int j = i + 1; j < monkeys.length; j++) { if (minHeight > monkeys[j].height) { // 修正一下最小值 minHeight = monkeys[j].height; // 修正下标 minIndex = j; } } if (minIndex != i) { // 交换 tempHeight = monkeys[minIndex].height; monkeys[minIndex].height = monkeys[i].height; monkeys[i].height = tempHeight; tempNo = monkeys[minIndex].monkeyId; monkeys[minIndex].monkeyId = monkeys[i].monkeyId; monkeys[i].monkeyId = tempNo; } } } // 冒泡排队 public void bubbleSort(Monkey[] monkeys) { float tempHeight = 0.0f; String tempNo = ""; // 冒泡 for (int i = 0; i < monkeys.length - 1; i++) { for (int j = 0; j < monkeys.length - 1 - i; j++) { // 判断 if (monkeys[j].height > monkeys[j + 1].height) { // 身高 tempHeight = monkeys[j].height; monkeys[j].height = monkeys[j + 1].height; monkeys[j + 1].height = tempHeight; // 编号 tempNo = monkeys[j].monkeyId; monkeys[j].monkeyId = monkeys[j + 1].monkeyId; monkeys[j + 1].monkeyId = tempNo; } } } } // 显示队列 public void show(Monkey[] monkeys) { for (int i = 0; i < monkeys.length; i++) { System.out.println("猴子编号:" + monkeys[i].monkeyId + " 身高:" + monkeys[i].height); } } }