void quicksort(int s[],int left,int right){
        if(left<right){
            int temp,i=left,j=right;
            temp=s[right];
            while(i<j){
                //寻找左边第一个大于基准值的下标
                while(s[i]<=temp&&i<j)i++;
                if(i<j)s[j--]=s[i];
                //寻找右边第一个小于基准值的下标
                while(s[j]>=temp&&i<j)j--;
                if(i<j)s[i++]=s[j];
            }
            s[i]=temp;
            quicksort(s,left,i-1);      //递归左边部分数组
            quicksort(s,i+1,right);     //递归右边部分数组
        }
    }

 非递归(使用LinkedHashMap)

void quickSort1(int s[],int left,int right){
        LinkedHashMap<Integer,Integer> lhp=new LinkedHashMap<>();
        //将0,n放入LinkedHashMap
        lhp.put(left,right);
        while(!lhp.isEmpty()){      //只要有需要排序的段
            //读取left,right
            Iterator<Map.Entry<Integer,Integer>> it=lhp.entrySet().iterator();
            left=it.next().getKey();
            right=lhp.get(left);
            //并从LinkedHashMap中删除
            lhp.remove(left,right);
            if(left>=right)continue;
            int i=left,j=right,temp=s[right];
            while(i<j){         //遍历排序一遍
                while(s[i]<=temp&&i<j)i++;
                if(i<j)s[j--]=s[i];
                while(s[j]>=temp&&i<j)j--;
                if(i<j)s[i++]=s[j];
            }
            s[i]=temp;
            lhp.put(left,i-1);
            lhp.put(i+1,right);
        }
    }

 

相关文章:

  • 2022-02-18
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2021-12-29
  • 2021-12-30
  • 2022-12-23
  • 2021-10-08
相关资源
相似解决方案