java简单排序

插入排序:

package com.test.linked;



public class InsertSort {
	public class Array{
		private int[] Myarray;
		private int size;
		public Array(int max){
			Myarray=new int[max];
			size=0;
		}
		/**
		 * 插入数据
		 * @param value
		 */
		public void insert(int value){
			Myarray[size]=value;
			size++;
		}
		public void sort(){
			int out,in;
			for (out = 1; out <size; out++) {
			int	temp=Myarray[out];
			in=out;
			while(in>0&&Myarray[in-1]>=temp){
				Myarray[in]=Myarray[in-1];
				--in;
			}
			Myarray[in]=temp;
			}
			
			}
		/**
		 * 显示数组
		 */
		public void display(){
			System.out.println("array begin");
			for (int i = 0; i < size; i++) {
				System.out.println(Myarray[i]);
			}
		}
	}
	public static void main(String[] args){
		int max=10;
		InsertSort tt=new InsertSort();
		Array test=tt.new Array(max);
		for (int i = 0; i <max ; i++) {
			
			test.insert((int) (Math.random()*87));
		}
		test.display();
		test.sort();
		test.display();
	}
}
数据:
array begin
63
78
43
16
45
84
62
81
57
65
array begin
16
43
45
57
62
63
65
78
81
84
冒泡排序:

package com.test.linked;


public class BoubleSort {
	
	public class Array{
		private int[] Myarray;
		private int size;
		public Array(int max){
			Myarray=new int[max];
			size=0;
		}
		/**
		 * 插入数据
		 * @param value
		 */
		public void insert(int value){
			Myarray[size]=value;
			size++;
		}
		public void sort(){
			for (int i = 0; i < size-1; i++) {
				for (int j = 0; j <size-i-1; j++) {
					if(Myarray[j]>Myarray[j+1]){
						swap(j, j+1);
					}
				}
			}
			
			}
		
		
		/**
		 * 交换数据
		 * @param leftpart
		 * @param rightpart
		 */
		public void swap(int index1,int index2){
			int temp=Myarray[index1];
			Myarray[index1]=Myarray[index2];
			Myarray[index2]=temp;
		}
		/**
		 * 显示数组
		 */
		public void display(){
			System.out.println("array begin");
			for (int i = 0; i < size; i++) {
				System.out.println(Myarray[i]);
			}
		}
	}
	public static void main(String[] args){
		int max=10;
		BoubleSort tt=new BoubleSort();
		Array test=tt.new Array(max);
		for (int i = 0; i <max ; i++) {
			
			test.insert((int) (Math.random()*87));
		}
		test.display();
		test.sort();
		test.display();
	}
}
数据:

array begin
83
41
17
61
53
15
69
26
0
20
array begin
0
15
17
20
26
41
53
61
69
83

选择排序:

package com.test.linked;

public class SelectSort {

	public class Array{
		private int[] Myarray;
		private int size;
		public Array(int max){
			Myarray=new int[max];
			size=0;
		}
		/**
		 * 插入数据
		 * @param value
		 */
		public void insert(int value){
			Myarray[size]=value;
			size++;
		}
		public void sort(){
			int min,out,inner;
			for ( out = 0; out <size-1; out++) {
				min=out;
				for (inner = out+1;  inner<size; inner++) {
					if(Myarray[inner]<Myarray[min]){
						min=inner;
					}
				}
				swap(out, min);
			}
		}
		
		/**
		 * 交换数据
		 * @param leftpart
		 * @param rightpart
		 */
		public void swap(int index1,int index2){
			int temp=Myarray[index1];
			Myarray[index1]=Myarray[index2];
			Myarray[index2]=temp;
		}
		/**
		 * 显示数组
		 */
		public void display(){
			System.out.println("array begin");
			for (int i = 0; i < size; i++) {
				System.out.println(Myarray[i]);
			}
		}
	}
	public static void main(String[] args){
		int max=10;
		SelectSort sort=new SelectSort();
		Array test=sort.new Array(max);
		for (int i = 0; i <max ; i++) {
			
			test.insert((int) (Math.random()*87));
		}
		test.display();
		test.sort();
		test.display();
	}
}
数据:

array begin
42
20
61
39
62
36
42
51
17
52
array begin
17
20
36
39
42
42
51
52
61
62





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值