java实现选择、插入以及希尔排序

选择排序

选择排序是不断的选择剩余元素中最小的。

举例
划掉的是已经排好顺序的不用再看了,加粗的是这次选择下次要进行排序(操作)的元素)
数组:48 56 10 94 28 64 37 16
第一次: 10 48 56 94 28 64 37 16
第二次: 10 16 48 56 94 28 64 37
第三次: 10 16 28 48 56 94 64 37
第四次: 10 16 28 37 48 56 94 64
第五次: 10 16 28 37 48 56 94 64
第六次: 10 16 28 37 48 56 94 64
第七次: 10 16 28 37 48 56 64 94

public class Selection {
    
    
	public static void sort(int[]a) {
    
    
		int N = a.length;
		for (int i = 0; i < N - 1; i++) {
    
    
			int min = i;
			for (int j = i + 1; j < N; j++) {
    
    
				if(a[j] < a[min]) min = j;
			}
			if(min != i) {
    
    //进行交换
				int temp = a[i];
				a[i] = a[min];
				a[min] = temp;
			}
		}
	}
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub

		Scanner in = new Scanner(System.in);
		System.out.println("输入排序的个数:");
		int N = in.nextInt();
		int[] a = new int[N];
		System.out.println("排序前:");
		for (int i = 0; i < a.length; i++) {
    
    
			a[i] = in.nextInt();
		}
		sort(a);
		System.out.println("选择排序后:");
		for (int i : a) {
    
    
			System.out.print(i + " ");
		}
	}
}

输入排序的个数:
10
排序前:
45 12 23 95 10 75 94 60 31 25
选择排序后:
10 12 23 25 31 45 60 75 94 95  

插入排序

插入排序将后一个元素和前面的元素进行比较,插入到合适的位置中。

举例
数组:48 56 10 94 28 64 37 16
第一次:48 56 10 94 28 64 37 16
第二次:10 48 56 94 28 64 37 16
第三次:10 48 56 94 28 64 37 16
第四次:10 28 48 56 94 64 37 16
第五次:10 28 48 56 64 94 37 16
第六次:10 28 37 48 56 64 94 16
第七次:10 16 28 37 48 56 64 94

public class Insertion {
    
    

	public static void sort(int[] a) {
    
    
		for (int i = 1; i < a.length; i++) {
    
    
			for (int j = i; j > 0; j--) {
    
    
				if(a[j] < a[j - 1]) {
    
    
					int temp = a[j - 1];
					a[j - 1] = a[j];
					a[j] = temp;
				}
			}
		}
	}
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		System.out.println("输入排序的个数:");
		int N = in.nextInt();
		int[] a = new int[N];
		System.out.println("排序前:");
		for (int i = 0; i < a.length; i++) {
    
    
			a[i] = in.nextInt();
		}
		sort(a);
		System.out.println("插入排序后:");
		for (int i : a) {
    
    
			System.out.print(i + " ");
		}
	}
}

输入排序的个数:
10
排序前:
45 95 62 20 13 84 90 55 61 24
插入排序后:
13 20 24 45 55 61 62 84 90 95 

希尔排序

希尔排序为了加快速度简单地改进了插入排序,交换不相邻的元素以对数组的局部进行排序,并最终用插入排序将局部有序的数组排序。

举例
(加粗的元素进行插入排序)
先计算step=length/2;
step(间隔)=4;
数组:48 56 10 94 28 64 37 16
第一次:28 56 10 94 48 64 37 16
第二次:28 56 10 94 48 64 37 16
第三次:28 56 10 94 48 64 37 16
第四次:28 56 10 16 48 64 37 94
(第一轮完成)
step=2;
(加粗的进行插入排序的同时不加粗的也进行插入排序)
第五次:10 56 28 16 48 64 37 94
第六次:10 16 28 56 37 64 48 94
(第二轮完成)
step=1;
第七次:10 16 28 37 48 56 64 94

public class Shell {
    
    

	public static void sort(int[] a){
    
    
		int N = a.length;
		for (int step = N / 2; step > 0; step /= 2) {
    
    
			for (int i = 0; i < N; i++) {
    
    
				for (int j = i; j >= 0 && (j + step) < N; j--) {
    
    
					if(a[j] > a[j + step]) {
    
    
					int temp = a[j + step];
					a[j + step] = a[j];
					a[j] = temp;
					}		
				}
			}
		}
	}
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub

		Scanner in = new Scanner(System.in);
		System.out.println("输入排序的个数:");
		int N = in.nextInt();
		int[] a = new int[N];
		System.out.println("排序前:");
		for (int i = 0; i < N; i++) {
    
    
			a[i] = in.nextInt();
		}
		sort(a);
		System.out.println("希尔排序后:");
		for (int i : a) {
    
    
			System.out.print(i + " ");
		}
	}
}


输入排序的个数:
10
排序前:
48 56 10 23 94 82 50 31 70 55
希尔排序后:
10 23 31 48 50 55 56 70 82 94 

如有不对的地方欢迎大家指出,共同进步!!!

猜你喜欢

转载自blog.csdn.net/weixin_45734378/article/details/107430915