fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void swap(int[] arr, int i, int j) {
  5. int temp = arr[i];
  6. arr[i] = arr[j];
  7. arr[j] = temp;
  8. }
  9.  
  10. public static int partition(int[] arr, int low, int high) {
  11. int pivot = arr[high];
  12. int i = low - 1;
  13.  
  14. for (int j = low; j < high; j++) {
  15. if (arr[j] < pivot) {
  16. i++;
  17. swap(arr, i, j);
  18. }
  19. }
  20. swap(arr, i + 1, high);
  21. return i + 1;
  22. }
  23.  
  24. public static void quickSort(int[] arr, int low, int high) {
  25. if (low < high) {
  26. int pi = partition(arr, low, high);
  27. quickSort(arr, low, pi - 1);
  28. quickSort(arr, pi + 1, high);
  29. }
  30. }
  31.  
  32. public static void main(String[] args) {
  33. Scanner scanner = new Scanner(System.in);
  34.  
  35. int n = scanner.nextInt();
  36. int[] arr = new int[n];
  37.  
  38. for (int i = 0; i < n; i++) {
  39. arr[i] = scanner.nextInt();
  40. }
  41.  
  42. quickSort(arr, 0, n - 1);
  43.  
  44. for (int i = 0; i < n; i++) {
  45. System.out.print(arr[i] + " ");
  46. }
  47. System.out.println();
  48. scanner.close();
  49. }
  50. }
Success #stdin #stdout 0.15s 58964KB
stdin
6
10 5 8 4 3 11
stdout
3 4 5 8 10 11