fork download
  1. import java.util.Scanner;
  2.  
  3.  
  4. class tsort {
  5.  
  6. private int[] numbers;
  7. private int number;
  8.  
  9. public void sort(int[] values) {
  10.  
  11. // Check for empty or null array
  12. if (values ==null || values.length==0){
  13. return;
  14. }
  15. this.numbers = values;
  16. number = values.length;
  17. quicksort(0, number - 1);
  18.  
  19. for(int k = 0;k<numbers.length;k++)
  20. {
  21. System.out.println(numbers[k]);
  22. }
  23.  
  24. }
  25.  
  26. private void quicksort(int low, int high) {
  27. int i = low, j = high;
  28. // Get the pivot element from the middle of the list
  29. int pivot = numbers[low + (high-low)/2];
  30.  
  31. // Divide into two lists
  32. while (i <= j) {
  33. // If the current value from the left list is smaller then the pivot
  34. // element then get the next element from the left list
  35. while (numbers[i] < pivot) {
  36. i++;
  37. }
  38. // If the current value from the right list is larger then the pivot
  39. // element then get the next element from the right list
  40. while (numbers[j] > pivot) {
  41. j--;
  42. }
  43.  
  44. // If we have found a values in the left list which is larger then
  45. // the pivot element and if we have found a value in the right list
  46. // which is smaller then the pivot element then we exchange the
  47. // values.
  48. // As we are done we can increase i and j
  49. if (i <= j) {
  50. exchange(i, j);
  51. i++;
  52. j--;
  53. }
  54. }
  55. // Recursion
  56. if (low < j)
  57. {
  58. quicksort(low, j);
  59. }
  60. if (i < high)
  61. {
  62. quicksort(i, high);
  63. }
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70. private void exchange(int i, int j) {
  71. int temp = numbers[i];
  72. numbers[i] = numbers[j];
  73. numbers[j] = temp;
  74. }
  75. }
  76. class test3
  77. {
  78. public static void main(String[] args) {
  79. int number[]= null;
  80. int i ;
  81.  
  82. try
  83. {
  84. Scanner sc = new Scanner(System.in);
  85. int l = sc.nextInt();
  86. number=new int[l]; //you had not created!!!!
  87. for( i=0;i<l;i++)
  88. {
  89.  
  90.  
  91.  
  92. number[i]=sc.nextInt();
  93.  
  94.  
  95. }
  96.  
  97. tsort t = new tsort();
  98.  
  99. t.sort(number);
  100.  
  101. }
  102. catch(Exception e)
  103. {
  104.  
  105. }
  106.  
  107. }
  108. }
Success #stdin #stdout 0.1s 381696KB
stdin
5
5
3
6
7
1
stdout
1
3
5
6
7