zoukankan      html  css  js  c++  java
  • Expm 1_2 实现快速排序的算法,并尝试采用不同的方法实现线性的划分过程.

     1 package org.xiu68.exp.exp1;
     2 
     3 public class Exp1_2 {
     4     //实现快速排序算法,采用不同的方法实现线性划分的过程
     5     public static void main(String[] args) {
     6         int[] arr=new int[]{8,7,6,5,4,3,2,1,0};
     7         quitSort(arr,0,arr.length-1);
     8         
     9         for(int i=0;i<arr.length;i++)
    10             System.out.print(arr[i]+",");
    11     }
    12     
    13     //快速排序算法
    14         public static void quitSort(int[] r, int i,int j){
    15             if(i<j){
    16                 int middle=partition2(r, i, j);
    17                 quitSort(r, i, middle-1);
    18                 quitSort(r, middle+1, j);
    19             }
    20         }
    21     
    22     //快速排序第一种划分算法
    23         public static int partition1(int[] r,int i,int j){
    24             int temp=r[i];
    25             while(i<j){
    26                 while(i<j && r[j]>=temp)    //从j向前找比temp小的值
    27                     j--;                
    28                 
    29                 if(i<j)
    30                     r[i++]=r[j]; //将j指向的值移到i的位置,i往后移一个位置
    31                 
    32                 while(i<j && r[i]<temp)        //从i向后找比temp大的值
    33                     i++;
    34                 
    35                 if(i<j)
    36                     r[j--]=r[i];
    37             }
    38             
    39             r[i]=temp;
    40             return i;
    41         }
    42     
    43     //快速排序的第二种划分算法
    44     public static int partition2(int[] r,int i,int j){
    45         int temp=r[i];
    46         while(i<j){
    47             //从左往右找比temp大的值
    48             while(i<j && r[i]<temp)
    49                 i++;
    50             //从右往左找比temp小的值
    51             while(i<j && r[j]>temp)
    52                 j--;
    53             
    54             //i和j不是同一个位置
    55             if(i<j){
    56                 int t=r[j];
    57                 r[j]=r[i];
    58                 r[i]=t;
    59             }
    60         }
    61         return i;
    62     }
    63     
    64 }
    View Code
  • 相关阅读:
    WCF ria services完美登陆功能(10)
    利用DYCOM快速建立wcf服务器端
    DYCom简要介绍
    断剑
    生命的价值
    飞翔的蜘蛛
    JSP中如何获取select标签选中的值
    wrapClass
    iconfont 在vue项目中的应用(iconcomponent组件)
    正则表达式
  • 原文地址:https://www.cnblogs.com/xiu68/p/7988702.html
Copyright © 2011-2022 走看看