標籤:edr ++ log insert auto sort generated temp 技術分享
材料:猴子排序,按照身高來從小到大來排序。
第一關:
老猴子帶領小猴子隊伍按大小逐一比較,交換,開始高矮排列隊伍。(冒泡排序)
第二關:
太慢了,給第一關增加難度,進行選擇排序
第三關:
最後,嘗試選擇用插入排序法,來進行排序。
測試類別:
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); } }}
過三關 Java冒泡排序選擇排序插入排序小練習