java中插入排序_Java中的插入排序

java中插入排序

Today we will look into the Insertion Sort Java program. Insertion sort is similar to Bubble sort and in this post, we will go through the insertion sort algorithm, example and then write insertion sort java code to sort the integer array.

今天,我们将研究插入排序Java程序。 插入排序与Bubble排序类似,在本文中,我们将通过插入排序算法(示例),然后编写插入排序Java代码对整数数组进行排序。

Java中的插入排序 (Insertion Sort in Java)

  1. In java insertion sort, we compare the value at any index from all the prior elements until all the lesser values are found.

    在Java插入排序中,我们将比较所有先前元素中任何索引处的值,直到找到所有较小的值。
  2. Then we place the value at the index before which there are no lesser values.

    然后,将值放在没有较小值的索引处。
  3. Above two steps are done iteratively to the last index, finally we have a sorted array of integers.

    以上两个步骤迭代完成到最后一个索引,最后我们得到一个排序的整数数组。

插入排序算法示例 (Insertion Sort Algorithm Example)

Let’s understand insertion sort algorithm with an example. Let’s say we have an unsorted array [5, 4, 14, 2, 8].

让我们通过一个例子来理解插入排序算法。 假设我们有一个未排序的数组[5, 4, 14, 2, 8] 5,4,14,2,2,8 [5, 4, 14, 2, 8]

  1. 1st index iteration: value at 1st index = 4, which is less than 5, so array becomes [5, 5, 14, 2, 8], as we reached the start we place the value at 0th index and array becomes [4, 5, 14, 2, 8]

    第一个索引迭代 :第一个索引= 4的值,小于5,所以数组变成[5,5,14,2,2,8],当我们到达起点时,将值放在第0个索引,数组变成[4, 5,14,2,8]
  2. 2nd index iteration: value at 2nd index = 14 which is greater than 5, so leave the array as it is. Now array = [4, 5, 14, 2, 8]

    第二索引迭代 :第二索引= 14处的值大于5,因此将数组保持原样。 现在数组= [4、5、14、2、8]
  3. 3rd index iteration: value at 3rd index = 2 which is smaller than 14, so array becomes [4, 5, 14, 14, 8], again 2 is smaller than 5, so array becomes [4, 5, 5, 14, 8]. Again 2 is smaller than 4, so array becomes [4, 4, 5, 14, 8]. As we reached the start of array, we place 2 at 0th index and array becomes [2, 4, 5, 14, 8]

    第三索引迭代 :第三索引= 2处的值小于14,因此数组变为[4、5、14、14、8],再次2小于5,因此数组变为[4、5、5、14, 8]。 同样,2小于4,因此数组变为[4、4、5、14、8]。 当我们到达数组的开头时,我们将2放置在第0个索引处,并且数组变为[2,4,5,14,14,8]
  4. 4th index iteration: value at 4th index = 8, so array becomes [2, 4, 5, 14, 14], then 8 is greater than 5, so place 8 at the 14th place and array becomes [2, 4, 5, 8, 14]. And our array is sorted now.

    第四个索引迭代第四个索引的值= 8,因此数组变为[2,4,5,5,14,14],然后8大于5,因此将8放在第14位,数组变为[2,4,5, 8、14]。 现在,我们的数组已排序。

Java插入排序 (Java Insertion Sort)

Key points to remember when writing insertion sort implementation in java program:

在Java程序中编写插入排序实现时要记住的要点:

  • Start with 2nd element to the last element of the array, so use a for loop.

    从第二个元素开始到数组的最后一个元素,因此使用for循环。
  • Store the value into another variable to avoid it being lost when we change the index value in between.

    将值存储到另一个变量中,以避免在我们之间更改索引值时丢失它。
  • We need to keep changing the values until we are at 0th index or we found prior value to be greater, so we can use a while loop for this.

    我们需要不断更改值,直到我们在第0个索引处,或者发现先前值更大为止,因此可以为此使用while循环。

Here is the implementation of the insertion sort in java based on the above example and key points.

这是基于上述示例和关键点的java中插入排序的实现。

package com.journaldev.test;

import java.util.Arrays;

public class InsertionSort {

	public static void main(String[] args) {
		int A[] = new int[10];
		populateArray(A);
		System.out.println("Before Sorting: ");
		printArray(A);
		// sort the array
		insertionSort(A);
		System.out.println("\nAfter Sorting: ");
		printArray(A);
	}

	/**
	 * This method will sort the integer array using insertion sort in java algorithm
	 * 
	 * @param arr
	 */
	private static void insertionSort(int[] arr) {
		for (int i = 1; i < arr.length; i++) {
			int valueToSort = arr[i];
			int j = i;
			while (j > 0 && arr[j - 1] > valueToSort) {
				arr[j] = arr[j - 1];
				j--;
			}
			arr[j] = valueToSort;
		}
	}

	public static void printArray(int[] B) {
		System.out.println(Arrays.toString(B));
	}

	public static void populateArray(int[] B) {
		for (int i = 0; i < B.length; i++) {
			B[i] = (int) (Math.random() * 100);
		}
	}
}

I am using random number generator code and the output for me for above program is:

我正在使用随机数生成器代码,上述程序的输出为:

Before Sorting: 
[57, 90, 80, 48, 35, 91, 1, 83, 32, 53]

After Sorting: 
[1, 32, 35, 48, 53, 57, 80, 83, 90, 91]

插入排序复杂度 (Insertion Sort Complexity)

The complexity of insertion sorting is O(n) for best case (already sorted array) and O(n2) for worst case (sorted in reverse order).

对于最佳情况(已排序的数组),插入排序的复杂度为O(n),对于最坏情况(以相反顺序排序),插入复杂度为O(n2)。

Insertion sort in Java is a good choice for the array of small sizes and when you know that values will be mostly sorted. For example, sorting an array of size 100 where you know that all the values will be in between 1 to 10. That’s all for insertion sort in java.

Java中的插入排序是小尺寸数组的很好选择,并且当您知道大多数值将被排序时。 例如,对大小为100的数组进行排序时,您知道所有值都将在1到10之间。这全部用于Java中的插入排序。

Reference: Wikipedia

参考: 维基百科

翻译自: https://www.journaldev.com/585/insertion-sort-java

java中插入排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值