Skip to content

Instantly share code, notes, and snippets.

@ishiis
Last active May 26, 2018 07:01
Show Gist options
  • Save ishiis/183bdff24cf9a1dc5f57e7bd35af8f22 to your computer and use it in GitHub Desktop.
Save ishiis/183bdff24cf9a1dc5f57e7bd35af8f22 to your computer and use it in GitHub Desktop.

ソート

public class Sort {
  public static void main(String... args) {
    int[] array = {8, 6, 1, 10, 4, 7, 2, 5, 3, 9};
    printArray(array);
    quickSort(array, 0, array.length - 1);
    printArray(array);

    System.out.println(fibonacci(10));
  }

  public static void printArray(int[] array) {
    System.out.print("array[");
    for(int i = 0; i < array.length; i++) {
      System.out.print(array[i]);
      if(i != array.length - 1) {
        System.out.print(", ");
      }
    }
    System.out.println("]");
  }

  public static void bubbleSort(int[] array) {
    for(int i = 0; i < array.length - 1; i++) {
      for(int j = array.length - 1; j > i; j--) {
        if(array[j - 1] > array[j]) {
          int temp = array[j - 1];
          array[j - 1] = array[j];
          array[j] = temp;
        }
      }
    }
  }

  public static void quickSort(int[] array, int left, int right) {
    if(left >= right) return;
    int pivot = array[(left + right) / 2];
    int l = left;
    int r = right;
    while(l <= r) {
      while(array[l] < pivot) l++;
      while(array[r] > pivot) r--;
      if(l <= r) {
        int temp = array[l];
        array[l] = array[r];
        array[r] = temp;
        l++;
        r--;
      }
    }
    quickSort(array, left, r);
    quickSort(array, l, right);
  }

  public static int fibonacci(int n) {
    return n <= 0 ? 0 : n == 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
  }
}
@ishiis
Copy link
Author

ishiis commented May 26, 2018

        int outputLine = new Integer(inputLines.get(0));
        inputLines.remove(0);

        inputLines.sort((a, b) -> b.length() - a.length());
        for (int i = 0; i < outputLine; i++) {
            System.out.println(inputLines.get(i));
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment