LoginSignup
5
4

More than 5 years have passed since last update.

Javaでクイックソート

Last updated at Posted at 2016-02-29
QuickSort.java
import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {4, 5, 1, 6, 9, 2, 8, 3, 7, 0};
        Arrays.stream(quickSort(arr, 0, arr.length-1)).forEach(
                e -> System.out.println(e));
    }
    public static int[] quickSort(int[] arr, int left, int right) {
        if(left<=right) {
            int p = arr[(left+right)/2];
            int l = left;
            int r = right;
            while(l <= r) {
                while(arr[l] < p) l++;
                while(arr[r] > p) r--;

                if(l<=r) {
                    int tmp = arr[l];
                    arr[l] = arr[r];
                    arr[r] = tmp;
                    l++;
                    r--;
                }
            }

            quickSort(arr, left, r);
            quickSort(arr, l, right);
        }
        return arr;
    }
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4