(2)Java复习冒泡,选择,插入排序算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26569761/article/details/74089951
import java.io.*;
public class MonkeySort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//定义一个可以装五只猴子的对象数组
		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 (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	
		//让老猴子排序
		Monkey oldMonkey =new Monkey("1000", 1.2f);
		//冒泡排序
		//oldMonkey.bubbleSort(monkeys);
		//选择排序
		//oldMonkey.seclectSort(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 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 seclectSort(Monkey[] monkeys)
	{
		float tempHeight=0.0f;
		String tempNo="";
		for(int i=0;i<monkeys.length;i++)
		{
			//认为下标为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  insertSort(Monkey [] monkeys) 
	{
		//开始排序
		for(int i=1;i<monkeys.length-1;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 show(Monkey [] monkeys)
	{
		for(int i =0;i<monkeys.length;i++)
		{
			System.out.println("猴子编号"+monkeys[i].monkeyId+"身高"+monkeys[i].height);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_26569761/article/details/74089951