• Quick sort C# code(2)


    public class QuickSortNonRecursion
    {
           public int Split(int[] data,int low,int high)
          {
                    if(data == null) throw new ArgumentNullException();
                    if(low<0 || high >= data.length) throw new ArgumentOutOfRangeException();

             int pivot = data[low];
             while(low < high){
                     while(low < high && data[high] >= pivot) high--;
                     data[low] = data[high];
                     while(low < high && data[low]<= pivot) low++;
                     data[high] = data[low];
             }
              data[low] = pivot;
              return low;
    }

    public void QuickSort(int[] data, int low, int high)
    {
            if(low < high)
            {
                  Stack<int> stc = new Stack<int>();
                  int pivot = Split(data,low,high);
                  stc.push(low);
                  stc.push(pivot -1);
                  stc.push(pivot +1);
                  stc.push(high);

                  while(stc.count>0)
                  {
                        high = stc.pop();
                        low = stc.pop();
                        int temp;
                        if(low<high)
                        {
                             pivot = Split(data,low,high);
                             temp = pivot-1;
                             if(low < pivot)
                             {
                                   stc.push(low);
                                   stc.push(temp);
                             }
                             temp = pivot +1;
                             if(high>pivot)
                             {
                                   stc.push(temp);
                                   stc.push(high);
                             }
                         }
                  }
             }
    }

  • 相关阅读:
    Python3 循环语句
    Python3 条件控制
    Python3 字典
    Python3 元组
    Python的字符串函数
    2019/10/24
    JS-字符串方法总结
    maven环境变量配置
    PowerDesigner逆向导入MYSQL数据库并显示中文注释(转载)
    web_custom_request函数详解(转载)
  • 原文地址:https://www.cnblogs.com/stone/p/1232900.html
Copyright © 2020-2023  润新知