zoukankan      html  css  js  c++  java
  • 冒泡排序和快速排序

    冒泡排序1:
    public void RisePot(ref int [] array)
            {
                int a = array.Length;
                for (int i = 0; i < a-1; i++)
                {
                    for (int j = a - 1; j > i; j--)
                    {
                        if (array[i] > array[j])
                        {
                            int temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
      
                        }
      
                    }
                }
      
            }
    冒泡排序2:
     public void RisePot(ref int [] array)
            {
                int a = array.Length;
                for (int i = 0; i < a-1; i++)
                {
                    for (int j = i+1; j <a; j++)
                    {
                        if (array[i] > array[j])
                        {
                            int temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
      
                        }
      
                    }
                }
      
            }
    快速排序:
    public void QuickSort(ref int [] arr,int low ,int high)
            {
                if (low < high)
                {
                    int mid = GetMid(arr, low, high);
                    QuickSort(ref arr, low, mid - 1);
                    QuickSort(ref arr, mid + 1, high);
                }
            }
    public int GetMid(int[] array, int low, int high)
            {
                int temp = array[low];
                while (low < high)
                {
                    while (low < high && array[high] >= temp)
                    {
                        high--;
                    }
                    if (low < high)
                    {
                        array[low] = array[high];
                        low++;
                    }
                    while (low < high && array[low] <= temp)
                    {
                        low++;
                    }
                    if (low < high)
                    {
                        array[high] = array[low];
                        high--;
                    }
                }
                array[low] = temp;
                return low;
            }
    
    不使用第三变量交换两个整数的值
    
    static void Main(string[] args)
            {
                int a = 1234, b = 6666;
                Swap(ref a, ref b);
                Console.WriteLine("{0},{1}",a,b);
                Console.Read();
            }
            static void Swap(ref int a, ref int b)
            {
                a = b - a;
                b = b - a;
                a = a + b;
            }
  • 相关阅读:
    部署prerender服务器
    Bzoj4727--Poi2017Turysta
    Bzoj4818--Sdoi2017序列计数
    Heoi2014系列题解
    scoi2017酱油记
    Burnside引理与Pólya定理
    2017省选前北京集训总结
    奥妙重重的随机发生器
    ???--???Insection is not allowed
    反演
  • 原文地址:https://www.cnblogs.com/rinack/p/4624138.html
Copyright © 2011-2022 走看看