插入排序:将一部分看成有序,一部分无序,无序的第一个数字与前面的交换,需要临时变量temp来保存无序的第一个

xiaoxiao2025-04-26  5

package com.interview.datastructure; public class SelectSort { public static void sort(int[] a) { if (a != null) { for (int i = 1; i < a.length; i++) { int tmp = a[i], j = i; if (a[j - 1] > tmp) { while (j >= 1 && a[j - 1] > tmp) { a[j] = a[j - 1]; j--; } } a[j] = tmp; } } } public static void main(String[] args) { int[] array = {1, 6, 8, 2, 5, 3}; sort(array); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); } }

转载请注明原文地址: https://www.6miu.com/read-5029175.html

最新回复(0)