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;
            }
  • 相关阅读:
    html之marquee详解
    CSS盒模型
    基于windows API的手柄/键盘映射编程(一)
    阿超的烦恼来临的始端
    阿超的小目标
    程序员的800字作文
    Link to Coding
    项目经理都干些什么啊
    停不下来的英语课联想
    Markdown
  • 原文地址:https://www.cnblogs.com/rinack/p/4624138.html
Copyright © 2011-2022 走看看