递归排序法

一列数字的规则如下;1,1,2,3,5,8,13,21,34........ 求第30位数字是多少,用递规和非递归两种方法算法实现
斐波纳契数列(Fibonacci Sequence),又称黄金分割数列。在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,现在我从算法的角度,利用递归和非递归两种方式来进行实现:

  1. for循环
            Console.Write("for算法:");
            int a = 0; int b = 1; int sun = 0;
            for (int i = 0; i <=28; i++)
            {               
                sun = a + b;
                a = b;
                b = sun;
               
            }
            Console.WriteLine(sun);
  1. 递归算法:调用函数赋值为30
 public static int digui(int x)
        {
            if (x <= 0) return 0;
            else if (x > 0 && x <= 2)     return 1;
            else return digui(x - 1) + digui(x - 2);
        }

冒泡排序

           Console.WriteLine("冒泡排序 23,44,66,76,98,11,3,9,7");
             int temp = 0;
            int[] arr = {23,44,66,76,98,11,3,9,7};
     
            for (int i = 0; i < arr.Length - 1; i++)
            {   
                for (int j = 0; j < arr.Length - 1 - i; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }
       
            }
            Console.WriteLine("排序后的数组:");
            foreach (int item in arr)
            {
                Console.Write(item+"  |  ");
            }

插入排序法

        public static void InsertionSort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }
                list[j] = t;
            }

        }

选择排序法

        public static void SelectionSort(int[] list)
        {
            int min;
            for (int i = 0; i < list.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[j] < list[min])
                        min = j;
                }
                int t = list[min];
                list[min] = list[i];
                list[i] = t;
            }

        }

希尔排序法

        public static void ShellSort(int[] list)
        {
            int inc;
            for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= list.Length; i += inc)
                {
                    int t = list[i - 1];
                    int j = i;
                    while ((j > inc) && (list[j - inc - 1] > t))
                    {
                        list[j - 1] = list[j - inc - 1];
                        j -= inc;
                    }
                    list[j - 1] = t;
                }
            }
        }

        private static void Swap(ref int l, ref int r)
        {
            int s;
            s = l;
            l = r;
            r = s;
        }

快速排序法

        public static void QuickSort(int[] list, int low, int high)
        {
            int pivot;
            int l, r;
            int mid;
            if (high <= low)
            {
                return;
            }
            else if (high == low + 1)
            {
                if (list[low] > list[high])
                    Swap(ref list[low], ref list[high]);
                return;
            }
            mid = (low + high) >> 1;
            pivot = list[mid];
            Swap(ref list[low], ref list[mid]);
            l = low + 1;
            r = high;
            do
            {
                while (l <= r && list[l] < pivot)
                    l++;
                while (list[r] >= pivot)
                    r--;
                if (l < r)
                    Swap(ref list[l], ref list[r]);
            } while (l < r);
            list[low] = list[r];
            list[r] = pivot;
            if (low + 1 < r)
            {
                QuickSort(list, low, r - 1);
            }
            if (r + 1 < high)
            {
                QuickSort(list, r + 1, high);
            }
        }

JS十大经典算法排序总结对比图
JS十大经典算法排序总结对比图

Last modification:November 29th, 2019 at 06:11 pm
如果觉得我的文章对你有用,请随意赞赏