Home >Java >javaTutorial >Java implements half sorting algorithm

Java implements half sorting algorithm

高洛峰
高洛峰Original
2017-01-17 13:24:061522browse

Binary insertion sort is an improvement to the insertion sort algorithm. During the sorting algorithm, elements are continuously inserted into the previously sorted sequence. Since the first half is a sorted sequence, we don't have to search for the insertion point in sequence. We can use the half search method to speed up the search for the insertion point.

public static void halfSort(int[] array) {
    int low, high, mid;
    int tmp, j;
    for (int i = 1; i < array.length; i++) {
      tmp = array[i];
      low = 0;
      high = i - 1;
      while (low <= high) {
        mid = low + (high - low) / 2;
        if (array[mid] > tmp)
          high = mid - 1;
        else
          low = mid + 1;
      }
      for (j = i - 1; j > high; j--) {
        array[j + 1] = array[j];
      }
      array[high + 1] = tmp;
    }
  }


Schematic diagram of the half-sorting algorithm:

Java implements half sorting algorithm

##The above is the entire content of this article , I hope it will be helpful for everyone to learn the halving sorting algorithm in Java.

For more articles related to Java implementation of half-way sorting algorithm, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn