1. mkimura81

    Posted

    mkimura81
Changes in title
+Javaでクイックソート
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,32 @@
+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;
+ }
+}