zoukankan      html  css  js  c++  java
  • java算法

    public class QuickSort { 
    /**主方法*/ 
    public static void main(String[] args) { 
        //声明数组 
        int[] nums = {27, 8, 57, 9, 23, 41, 65, 19, 0, 1, 2, 4, 5}; 
        //应用快速排序方法 
        quickSort(nums, 0, nums.length-1); 
        //显示排序后的数组 
        for(int i = 0; i < nums.length; ++i) { 
          System.out.print(nums[i] + ","); 
        } 
        System.out.println(""); 


    /**快速排序方法*/ 
    public static void quickSort(int[] a, int lo0, int hi0) { 
        int lo = lo0; 
        int hi = hi0; 

        if (lo >= hi) 
          return; 

        //确定指针方向的逻辑变量 
        boolean transfer=true; 

        while (lo != hi) { 
          if (a[lo] > a[hi]) { 
            //交换数字 
            int temp = a[lo]; 
            a[lo] = a[hi]; 
            a[hi] = temp; 
            //决定下标移动,还是上标移动 
            transfer = (transfer == true) ? false : true; 
          } 

          //将指针向前或者向后移动 
          if(transfer) 
            hi--; 
          else 
            lo++; 

          //显示每一次指针移动的数组数字的变化 
          /*for(int i = 0; i < a.length; ++i) { 
            System.out.print(a[i] + ","); 
          } 
          System.out.print(" (lo,hi) = " + "(" + lo + "," + hi + ")"); 
          System.out.println("");*/ 
        } 

        //将数组分开两半,确定每个数字的正确位置 
        lo--; 
        hi++; 
        quickSort(a, lo0, lo); 
        quickSort(a, hi, hi0); 

    }

  • 相关阅读:
    驰骋工作流引擎-系统变量的引用
    驰骋工作流引擎-表单样本展示
    驰骋工作流引擎CCFLOW下载代码
    初识CSS
    初识HTML标签
    初识JDBC
    通过锁对象解决哲学家就餐问题
    MySQL基本用法
    LRU算法实现,HashMap与LinkedHashMap源码的部分总结
    Java简易实现记事本的打开与保存
  • 原文地址:https://www.cnblogs.com/Rambo635755402/p/3008725.html
Copyright © 2011-2022 走看看