美文网首页
InsertionSort插入排序

InsertionSort插入排序

作者: 叫我颜先生 | 来源:发表于2022-03-14 14:29 被阅读0次

    /*

    • @Author: sumBorn
    • @Date: 2022-02-23 21:57:10
    • @Description:

    空间复杂度O(N2) 最好:O(N)完全排序
    时间复杂度O(1)
    稳定排序

    逆序对数量越多,时间复杂度越高

    */

    /**

    • @description:

    • @param {*}

    • @return {*}
      */
      public class Solution
      {
      public int[] InsertionSort(int[] arr)
      {
      for (int i = 1; i < arr.Length; i++)
      {
      for (int j = i; j > 0; j--)
      {
      if (arr[j - 1] > arr[j])
      {
      this.Swap(j - 1, j, arr);
      }
      }
      }

       return arr;
      

      }

      public void Swap(int i, int j, int[] arr)
      {
      var tmp = arr[i];
      arr[i] = arr[j];
      arr[j] = tmp;
      }
      }

    相关文章

      网友评论

          本文标题:InsertionSort插入排序

          本文链接:https://www.haomeiwen.com/subject/gfajdrtx.html