Binary Insert sort Insert sort of hill sort

Source: Internet
Author: User
Insert Sort
public static int[] Sort (int[] a) {
		int length = a.length;
		for (int i = 1; i < length; i++) {
			int key = A[i];
			int j = i-1;
			Why is j>=0 here? Because when j=0, if also conforms to the a[j]>key, also needs to carry on the shift operation. While
			(J >= 0 && A[j] > key) {
				A[j + 1] = A[j];
				j--;
			}
			Why is j+1 here, because the last statement in the while loop, J--。
			a[j + 1] = key;
		}
		return A;
	}
Hill Sort

public class Shellsort {

	static public int[] Sort (int[] a) {
		int len = a.length;
		int DK = LEN/2;
		while (DK >= 1) {
			//system.out.println (DK);
			Shellinsert (A, DK);
			DK/= 2;
		}

		return A;
	}
	/**
	 * DK = 1, simple insertion sort
	/private static void Shellinsert (int[] A, int dk) {
		if (DK = 0)
			throw new IllegalArgumentException ("DK can ' t eqauls 0!");
		
		int len = a.length;

		for (int i = DK; i < Len; i++) {
			int key = A[i];
			int j = I-DK;
			while (J >= 0 && key < A[j]) {
				a[j + DK] = a[j];
				J-=DK;
			}
			A[j + DK] = key;
		}
	}


The discussion of the DK in the order of Hill is more likely to refer to related books. binary Insertion Sort

public class Binaryinsertionsort {public

	static int[] Sort (int[] a) {
		int length = a.length;

		for (int i = 1; i < length; i++) {

			int key = A[i];

			int low = 0;
			int high = i-1;

			High = Getinsertindex (A, a[i], low, high);
			for (int j = i-1, J >= High; j--)
				a[j + 1] = A[j];
			A[high] = key;
		}
		return A;
	}

	private static int Getinsertindex (int[] A, int key, int low, int high) {while
		(low <= high) {
			int mid = + high)/2;
			if (Key > A[mid]) {Low
				= mid + 1;
			} else {high
				= mid-1;
		}} return high + 1;
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.