zoukankan      html  css  js  c++  java
  • 【简单算法】快速排序、斐波拉切缓存

    1. 快速排序:

     1         private static void FastSort(int[] data, int low, int high)
     2         {
     3             int i = low;
     4             int j = high;
     5             int mid = data[(low + high) / 2];
     6             do
     7             {
     8                 while (data[i] < mid && i < high)
     9                 {
    10                     i++;
    11                 }
    12                 while (data[j] > mid && j > low)
    13                 {
    14                     j--;
    15                 }
    16                 if (i <= j)
    17                 {
    18                     int temp = data[i];
    19                     data[i] = data[j];
    20                     data[j] = temp;
    21                     i++;
    22                     j--;
    23                 }
    24             } while (i <= j);
    25             if (i < high)
    26             {
    27                 FastSort(data, i, high);
    28             }
    29             if (j > low)
    30             {
    31                 FastSort(data, low, j);
    32             }
    33         } 

    2. 斐波拉切缓存:

     1     class Phabe
     2     {
     3         private static List<long> phabeList;
     4         static Phabe()
     5         {
     6             phabeList = new List<long>();
     7             phabeList.Add(1);
     8             phabeList.Add(1);
     9         }
    10         private static void CalcTo(int to)
    11         {
    12             while (phabeList.Count < to)
    13             {
    14                 int count = phabeList.Count;
    15                 long temp = phabeList[count - 1] + phabeList[count - 2];
    16                 phabeList.Add(temp);//缓存计算结果
    17             }
    18         }
    19         public static long Get(int index)
    20         {
    21             if (index <= 0)
    22             {
    23                 return 0;
    24             }
    25             if (phabeList.Count < index)
    26             {
    27                 CalcTo(index);
    28             }
    29             //Console.WriteLine("Total: " + phabeList.Count);
    30             return phabeList[index-1];
    31         }
    32     }
  • 相关阅读:
    Element + 列表增删改查
    int 型动态数组
    Vue.prototype.$ 和 Vue.use()
    Vue.js生成一个Vue实例
    Element 树形组件
    使用Vuex 实现标签数字增加与减小
    使用指针做形参
    JavaWeb学习之HttpServletRequest
    JavaWeb学习之HttpServletResponse
    JavaWeb学习之Javaweb核心servlet
  • 原文地址:https://www.cnblogs.com/yougmi/p/6589285.html
Copyright © 2011-2022 走看看