Last active
August 29, 2015 14:03
-
-
Save sankichi92/4ab1d5657f14cac568f0 to your computer and use it in GitHub Desktop.
クイックソート
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| public class QuickSort { | |
| private static final int LEFT = 0; | |
| private static final int RIGHT = 1; | |
| protected static void exchange(int[] a, int i, int j) { | |
| int temp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = temp; | |
| } | |
| protected static int rearrange(int[] a, int i, int j) { | |
| int mover = LEFT; | |
| while (i < j) { | |
| if (mover == LEFT) { | |
| if (a[i] > a[j]) { | |
| exchange(a, i, j); | |
| mover = RIGHT; | |
| } else { | |
| i++; | |
| } | |
| } else { | |
| if (a[i] > a[j]) { | |
| exchange(a, i, j); | |
| mover = LEFT; | |
| } else { | |
| j--; | |
| } | |
| } | |
| } | |
| return i; | |
| } | |
| public static void sort(int[] a, int i, int j) { | |
| if (i < j) { | |
| int p = rearrange(a, i, j); | |
| sort(a, i, p-1); | |
| sort(a, p+1, j); | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| int a[] = new int[10]; | |
| InputStreamReader in = new InputStreamReader(System.in); | |
| BufferedReader br = new BufferedReader(in); | |
| System.out.println("10個の整数を入力してください"); | |
| for (int i = 0; i < 10; i++) { | |
| a[i] = Integer.valueOf(br.readLine()); | |
| } | |
| sort(a, 0, 9); | |
| System.out.println("\nソート結果"); | |
| for (int i : a) { | |
| System.out.println(i); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment