zoukankan      html  css  js  c++  java
  • 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);
                             }
                         }
                  }
             }
    }

  • 相关阅读:
    手把手教你实现热力图!
    [学习笔记]overthewire bandit 通关秘籍
    施乐3065复印机邮件功能调试
    [IT学习]Greatwall
    [IT学习]从网上获取pdf制作vce文件
    [办公自动化]如何选择投影仪的吊装距离
    [读书笔记]《没人会告诉你的PPT真相》
    英文主日学材料备忘
    win10访问共享文件夹提示:引用的账户当前已锁定,且当前可能无法登陆
    [IT学习]Linux 学习笔记
  • 原文地址:https://www.cnblogs.com/stone/p/1232900.html
Copyright © 2011-2022 走看看