• 算法复习1(冒泡、快排、折半)


    冒泡

     1  static void BubbleSort()
     2         {
     3             int[] array = new int[] { 7, 3, 5, 8, 2 };
     4 
     5             for (int i = 0; i < array.Length; i++)
     6             {
     7                 for (int j = 0; j < array.Length - i - 1; j++)
     8                 {
     9                     if (array[j] > array[j + 1])
    10                     {
    11                         int temp = array[j];
    12                         array[j] = array[j + 1];
    13                         array[j + 1] = temp;
    14                     }
    15                 }
    16             }
    17             Array.ForEach(array, p => Console.Write(p));
    18         }
    View Code

    快排

            static int QuickSort(int[] array, int low, int high)
            {
                if (low >= high) return -1;
    
                int keyIndex = low;
                int key = array[low];
    
    
                while (low < high)
                {
                    while (low <= high)
                    {
                        if (key > array[high])
                        {
                            Swap(array, keyIndex, high);
                            keyIndex = high;
                            break;
                        }
                        high--;
                    }
    
                    while (low < high)
                    {
                        if (key < array[low])
                        {
                            Swap(array, low, keyIndex);
                            keyIndex = low;
                            break;
                        }
                        low++;
                    }
                }
    
                return keyIndex;
            }
    
            static void QuickSortRecursion(int[] array, int i, int j)
            {
                int index = QuickSort(array, i, j);
    
                if (index == -1) return;
    
                QuickSortRecursion(array, i, index - 1);
                QuickSortRecursion(array, index + 1, j);
            }
    View Code

    二分

     1 static int DichoFind(int[] array, int num)
     2         {
     3             int i = 0;
     4             int j = array.Length - 1;
     5 
     6             while (i <= j)
     7             {
     8                 int mid = (i + j) / 2;
     9 
    10                 if (array[mid] > num)
    11                 {
    12                     j = mid - 1;
    13                 }
    14                 else if (array[mid] < num)
    15                 {
    16                     i = mid + 1;
    17                 }
    18                 else
    19                 {
    20                     return mid;
    21                 }
    22             }
    23 
    24             return -1;
    25         }
    View Code
  • 相关阅读:
    amfphp1.9 class mapping初探
    C#程序打包.exe应用程序
    数据库备份方案
    ListView 控件使用
    在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库
    .NET
    转载:MATLAB 符号函数作图
    整理雷达相关
    Python 程序 运行过程
    struts2 文件上传显示进度条
  • 原文地址:https://www.cnblogs.com/dalas/p/3106142.html
Copyright © 2020-2023  润新知