排序:选择排序、冒泡排序、插入排序(day02-13.09.26)

/*选择排序*/

package SelectionSort;

import java.util.Arrays;

public class SelectionSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] m_Num={7,9,0,1,3,2,9,4,9,7,3,8,9,4,3,8,5,6};
		int m_Lenght=m_Num.length;
		int tmpNum=0;
		for(int i =0;i<m_Lenght-1;i++){
			for(int j=i+1;j<m_Lenght;j++){
				if(m_Num[j]>m_Num[i]){
					tmpNum=m_Num[i];
					m_Num[i]=m_Num[j];
					m_Num[j]=tmpNum;
				}
			}
		}
		String tmpNumSort=Arrays.toString(m_Num);
		System.out.println(tmpNumSort);
	}

}

/*冒泡排序*/

package BubbleSort;

import java.util.Arrays;
import java.util.Scanner;

public class BubbleSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner m_NumSort=new Scanner(System.in);
		int[] m_Num={};
		System.out.println("请输入需排列的整数数组");
		do {
			m_Num=Arrays.copyOf(m_Num, m_Num.length+1);
			m_Num[m_Num.length-1]=m_NumSort.nextInt();
//			System.out.println(Arrays.toString(m_Num));
		} while (m_Num[m_Num.length-1]!=99);
		System.out.println("there");
		Sort(m_Num);
		
		m_NumSort.close();
	}
	private static void Sort(int[]aNum){
		
		int tmp;
		for(int i=0;i<aNum.length-1;i++){
			for(int j=0;j<aNum.length-(i+1);j++){
				if(aNum[j]>aNum[j+1]){
					tmp=aNum[j];
					aNum[j]=aNum[j+1];
					aNum[j+1]=tmp;
					System.out.println("here");
				}
			}
		}
		String tmpNumSort=Arrays.toString(aNum);
		System.out.println(tmpNumSort);	
	}

}

/*插入排序*/

package InsertSort;

import java.util.Arrays;
import java.util.Scanner;

public class InsertSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner m_InputNum=new Scanner(System.in);
		int[] m_Num={};
		System.out.println("请输入排序的整数数组:");
		do {
			m_Num=Arrays.copyOf(m_Num, m_Num.length+1);
			m_Num[m_Num.length-1]=m_InputNum.nextInt();
		} while (m_Num[m_Num.length-1]!=99);
		System.out.println(Arrays.toString(m_Num));
		Sort(m_Num);
		m_InputNum.close();
	}
	private static void Sort(int[] aNum){
		int tmp;
		int tmpINum;
		for(int i=1;i<aNum.length;i++){
			for(int j=i-1;j>=0;j--){
				if((aNum[i]>=aNum[j])&&aNum[i]<=aNum[j+1]){
					tmp=aNum[i];
					tmpINum=i;
					while(j!=tmpINum-1){
						aNum[tmpINum]=aNum[tmpINum-1];
						tmpINum--;
					}
					aNum[j+1]=tmp;
					break;
				}
			}
		}
		System.out.println(Arrays.toString(aNum));
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值