zoukankan      html  css  js  c++  java
  • 排序算法----快速排序java

    快速排序是对冒泡排序的一种改进,平均时间复杂度是O(nlogn)

    import java.util.Arrays;
    import java.util.Scanner;
    public class test02{
        public static void main(String[] args) {
            int n = 1;
            while (n != 0){
            Scanner scanner = new Scanner(System.in);
            n = scanner.nextInt();
            int s[] = new int[n];
            for (int i = 0; i < n; i++) {
                s[i] = scanner.nextInt();
            }
            sort(s, 0, n - 1);
            System.out.println(Arrays.toString(s));
        }
        }
    
        public static void sort(int[] s, int low, int high){
            int left = low;
            int right = high;
            int temp;
            if(low<high){
                while(left<right){
                    while(left<high&&s[left]<=s[low]){
                        left++;
                    }
                    while(right>low&&s[right]>s[low]){
                        right--;
                    }
                    if(left<right){
                        temp = s[left];
                        s[left]  = s[right];
                        s[right] = temp;
                    }
                }
                temp = s[low];
                s[low] = s[right];
                s[right] = temp;
                sort(s, low, right-1);
                sort(s, right+1, high);
    
            }
        }
    }

  • 相关阅读:
    PAT 1032 (未完成)
    PAT 1031
    PAT 1030
    将爬取到的数据存入数据框并导出
    XPath常见用法
    python 图表
    Protobuf在Unity中的通讯使用
    ProtoBuf在Unity中的使用
    Unity更新资源代码
    匿名函数
  • 原文地址:https://www.cnblogs.com/tk55/p/6622988.html
Copyright © 2011-2022 走看看