Algorithm 4 (data structure) learning-the principle and code of primary sorting algorithm (selection sorting, insertion sorting, hill sorting)

1. Selection sort

A brief description is: first find the smallest element in the array, and exchange it with the first element of the array; then find the smallest element among the remaining elements and exchange the position with the second element of the array; and so on, until the Sort the entire array.
For an array of length N, it takes about (N^2)/2 comparisons and N exchanges to select sort

2. Insertion sort

Insertion sort, insert each element into the appropriate position in other ordered local arrays, in order to make room for the inserted element, we need to move the remaining elements in the appropriate position by one place;
Simply put, first determine an index value i, starting from this i, define j=i, compare a[j] with a[j-1], if a[j-1]>a[j] exchange the two ,J–, if a[j-1]<a[j], it means that you have reached the correct position, exit the small loop, otherwise continue to repeat the operation until j=0; after completing the small loop, put the index i++.

3. Hill sort

To put it simply, first change the swap interval of "insertion sort" from 1 to h, and then h=h/3 after the round is arranged, and then repeat the operation until h>=1.

The following is the code

import java.util.*;

public class Example {
    
    

	//选择排序
	public static void SelectSort(Comparable[] a) {
    
    
		for (int i = 0; i < a.length; i++) {
    
    
			int min = i;
			for (int j = i+1; j < a.length; j++) {
    
    
				if (less(a[j], a[min])) {
    
    
					min = j;
				}
			}
			exch(a, i, min);
		}
	}
	
	/*插入排序,简单来说就是先确定一个索引值i,从这个i开始,定义j=i,a[j]与a[j-1]比较,若a[j-1]>a[j]则交换二者,j--,
	     若a[j-1]<a[j]则表明已经到了正确的位置,退出小循环,否则继续重复操作,直到j=0;完成小循环后把索引i++*/
	public static void InsertSort(Comparable[] a) {
    
    
		for (int i = 1; i < a.length; i++) {
    
    
			for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
    
    
				exch(a, j, j-1);
			}
		}
	}
	
	//希尔排序
	public static void ShellSort(Comparable[] a) {
    
    
		int h = 1;
		while (h < a.length/3) {
    
    
			h = 3*h + 1;
		}
		while (h >= 1) {
    
    
			for (int i = h; i < a.length; i++) {
    
    
				for (int j = i; j >= h && less(a[j], a[j-h]); j-=h) {
    
    
					exch(a, j, j-h);
				}
			}
			h = h / 3;
		}
	}
	
	//做比较判断,返回bool类型
	public static boolean less(Comparable v, Comparable w) {
    
    
		return v.compareTo(w) < 0;
	}
	
	//交换数组元素
	private static void exch(Comparable[] a, int i, int j) {
    
    
		Comparable t = a[i];
		a[i] = a[j];
		a[j] = t;
	}
	
	//打印出数组
	public static void show(Comparable[] a) {
    
    
		for (int i = 0; i < a.length; i++) {
    
    
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		Comparable[] arr = new Comparable[n];
		for (int i = 0; i < arr.length; i++) {
    
    
			arr[i] = in.nextInt();
		}
		SelectSort(arr);
		InsertSort(arr);
		ShellSort(arr);
		show(arr);
	}

}

Guess you like

Origin blog.csdn.net/UCB001/article/details/105010877