fork download
  1. class Main {
  2. public static void main (String[] args) throws java.lang.Exception {
  3. int[] data = {4, 7, 10, 2, 5};
  4. sort(data);
  5. for(int i = 0; i < data.length; i++) {
  6. System.out.print(data[i]+" ");
  7. }
  8. }
  9. public static void sort(int[] data) {
  10. if (data != null) {
  11. quickSort(data, data.length);
  12. }
  13. }
  14. private static void quickSort(int[] data, int count) {
  15. if (count <= 1) {
  16. return;
  17. }
  18. int[] largeData = new int[count];
  19. int[] smallData = new int[count];
  20. int largeCount = 0;
  21. int smallCount = 0;
  22. int pivot = data[0];
  23. for (int index = 1; index < count; index ++) {
  24. if (data[index] > pivot) {
  25. largeData[largeCount] = data[index];
  26. largeCount ++;
  27. }
  28. else {
  29. smallData[smallCount] = data[index];
  30. smallCount ++;
  31. }
  32. }
  33. quickSort(largeData, largeCount);
  34. quickSort(smallData, smallCount);
  35. int sortedIndex = 0;
  36. for (int smallIndex = 0; smallIndex < smallCount; smallIndex ++) {
  37. data[sortedIndex] = smallData[smallIndex];
  38. sortedIndex ++;
  39. }
  40. data[sortedIndex] = pivot;
  41. sortedIndex ++;
  42. for (int largeIndex = 0; largeIndex < largeCount; largeIndex ++) {
  43. data[sortedIndex] = largeData[largeIndex];
  44. sortedIndex ++;
  45. }
  46. }
  47. }
  48.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
2 4 5 7 10