插入排序(java版)
生活随笔
收集整理的這篇文章主要介紹了
插入排序(java版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 public class InsertSortTest{
2 public static void InsertSort(int[] source) {
3 //默認第一個元素已排序
4 for (int i = 1; i < source.length; i++) {
5 for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {
6 swap(source, j, j - 1);
7 }
8 }
9 }
10 //完成交換功能的子函數 static
11 private static void swap(int[] source, int x, int y) {
12 int temp = source[x];
13 source[x] = source[y];
14 source[y] = temp;
15 }
16 //在main中測試
17 public static void main(String[] args) {
18 int[] a = {4, 2, 1, 6, 3, 6, 0, -5, 1, 1};
19
20 InsertSort(a);
21
22 for (int i = 0; i < a.length; i++) {
23 System.out.printf("%d ", a[i]);
24 }
25 }
26 }
?
轉載于:https://www.cnblogs.com/happyhacking/p/4350616.html
總結
以上是生活随笔為你收集整理的插入排序(java版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery模拟原生态App上拉刷新下拉
- 下一篇: Xamarin.Android开发实践(