排序演算法之直接插入排序Java實現,直接插入java

來源:互聯網
上載者:User

排序演算法之直接插入排序Java實現,直接插入java

排序演算法之直接插入排序

一、直接插入排序的過程

1、直接插入排序由 N-1 趟排序組成。
2、基本思想:將第一個元素看成一個有序子序列,再依次從第二個記錄起諸葛插入到這個有序的子序列中。
  一般地,將 elem[i] 插入到由 elem[0] ~ elem[i-1] 構成的有序子序列中
  時間複雜度:O(n) ~ O(n^2)

原始數組為:
[74, 27, 85, 59, 41, 66, 37, 92, 4, 93]
--------------------------------------
第 1趟
[27, 74, 85, 59, 41, 66, 37, 92, 4, 93]
第 2趟
[27, 74, 85, 59, 41, 66, 37, 92, 4, 93]
第 3趟
[27, 59, 74, 85, 41, 66, 37, 92, 4, 93]
第 4趟
[27, 41, 59, 74, 85, 66, 37, 92, 4, 93]
第 5趟
[27, 41, 59, 66, 74, 85, 37, 92, 4, 93]
第 6趟
[27, 37, 41, 59, 66, 74, 85, 92, 4, 93]
第 7趟
[27, 37, 41, 59, 66, 74, 85, 92, 4, 93]
第 8趟
[4, 27, 37, 41, 59, 66, 74, 85, 92, 93]
第 9趟
[4, 27, 37, 41, 59, 66, 74, 85, 92, 93]
--------------------------------------
排序後的數組為:
[4, 27, 37, 41, 59, 66, 74, 85, 92, 93]

二、直接插入排序的代碼實現

1、方法一:

import java.util.Arrays;

/**
*
* @title InsertSort
* @describe 直接插入排序
* @author 張富昌
* @date 2016年10月1日下午5:12:41
*/
public class InsertSort_1 {

  public static void main(String[] args) {
    // 聲明整型數組
    int[] array = new int[10];
    // 使用迴圈和隨機數初始化數組
    for (int i = 0; i < array.length; i++) {
      array[i] = (int) Math.round(Math.random() * 100);
    }
    System.out.println("原始數組為:");
    System.out.println(Arrays.toString(array));
    System.out.println("--------------------------------------");
    array = insertSort(array);
    System.out.println("--------------------------------------");
    System.out.println("排序後的數組為:");
    System.out.println(Arrays.toString(array));
  }

 

  /**
  *
  * 功能:插入排序的基本思想是,將元素逐個添加到已經排序好的數組中去,同時要求,插入的元素必須在正確的位置,這樣原來排序好的數組是仍然有序的。
  *
  * 參數:int[] array
  *
  * 傳回型別:int[]
  */
  public static int[] insertSort(int[] array) {
    // 使用臨時數組,替代原始數組
    int[] arr = array;
    // 臨時變數
    int temp;
    for (int i = 1; i < arr.length; i++) {

      System.out.println("第 " + i + "趟");
      for (int j = i; j >= 1; j--) {
        // 較小的數排在前面
        if (arr[j] < arr[j - 1]) {
          temp = arr[j];
          arr[j] = arr[j - 1];
          arr[j - 1] = temp;
        } else {
          break;
        }
      }
      System.out.println(Arrays.toString(arr));
    }
    return arr;
  }
}

2、方法二:

import java.util.Arrays;

/**
*
* @title InsertSort
* @describe 直接插入排序
* @author 張富昌
* @date 2016年10月1日下午5:12:41
*/
public class InsertSort_2 {

  public static void main(String[] args) {
    // 聲明整型數組
    int[] array = new int[10];
    // 使用迴圈和隨機數初始化數組
    for (int i = 0; i < array.length; i++) {
      array[i] = (int) Math.round(Math.random() * 100);
    }
    System.out.println(Arrays.toString(array));
    System.out.println("--------------------------------------");
    array = insertSort(array);
    System.out.println("--------------------------------------");
    System.out.println("排序後的數組為:");
    System.out.println(Arrays.toString(array));
  }

  /**
  *
  * 功能:插入排序的基本思想是,將元素逐個添加到已經排序好的數組中去,同時要求,插入的元素必須在正確的位置,這樣原來排序好的數組是仍然有序的。
  *
  * 參數:int[] array
  *
  * 傳回型別:int[]
  */
  public static int[] insertSort(int[] array) {
    // 使用臨時數組,替代原始數組
    int[] arr = array;
    int j, temp;
    for (int i = 1; i < arr.length; i++) {
      System.out.println("第 " + (i + 1) + "趟");
      if (arr[i] < arr[i - 1]) {
        temp = arr[i];
        for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
          arr[j + 1] = arr[j];
        }
        arr[j + 1] = temp;
      }
      System.out.println(Arrays.toString(arr));
    }
    return arr;
  }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.