Java implements selection, insertion and Hill sorting

Select sort

Selection sort is to continuously select the smallest of the remaining elements.

For example
(the ones that have been sorted out are crossed out and you don’t need to read them again. The ones in bold are the elements to be sorted (operated) this time).
Array: 48 56 10 94 28 64 37 16
First time: 10 48 56 94 28 64 37 16
Second: 10 16 48 56 94 28 64 37
Third: 10 16 28 48 56 94 64 37
Fourth: 10 16 28 37 48 56 94 64
Fifth: 10 16 28 37 48 56 94 64
Sixth: 10 16 28 37 48 56 94 64
Seventh: 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  

Insertion sort

Insertion sort compares the latter element with the previous element and inserts it into the appropriate position.

Example
Array: 48 56 10 94 28 64 37 16
First time: 48 56 10 94 28 64 37 16
Second time: 10 48 56 94 28 64 37 16
Third time: 10 48 56 94 28 64 37 16
Fourth time : 10 28 48 56 94 64 37 16
Fifth: 10 28 48 56 64 94 37 16
Sixth: 10 28 37 48 56 64 94 16
Seventh: 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 

Hill sort

Hill sorting simply improves insertion sort in order to speed up the speed, swapping non-adjacent elements to sort the part of the array, and finally sorting the locally ordered array with insertion sort.

For example
(the bold elements are inserted and sorted)
first calculate step=length/2;
step (interval)=4;
array: 48 56 10 94 28 64 37 16
first time: 28 56 10 94 48 64 37 16
second time :28 56 10 94 48 64 37 16
Third time: 28 56 10 94 48 64 37 16
Fourth time: 28 56 10 16 48 64 37 94
(Completion of the first round)
step=2;
(Bold insertion sort Insertion sorting is performed without bolding at the same time) The
fifth time: 10 56 28 16 48 64 37 94 The
sixth time: 10 16 28 56 37 64 48 94
(the second round is completed)
step=1; the
seventh time: 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 

If anything is wrong, everyone is welcome to point out and make progress together! ! !

Guess you like

Origin blog.csdn.net/weixin_45734378/article/details/107430915