Guest User

Untitled

a guest
Aug 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Programm {
  4. static int[] numbers = new int[7];
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner keyScanner = new Scanner(System.in);
  9. for (int i = 0; i < 7; i++) {
  10. numbers[i] = keyScanner.nextInt();
  11. }
  12.  
  13. for (int i = 0; i < 7; i++) {
  14. System.out.print(numbers[i] + "; ");
  15. }
  16. QuickSort(0, 6);
  17. for (int i = 0; i < 7; i++) {
  18. System.out.print(numbers[i] + "; ");
  19. }
  20. // numbers = [1, 7, 3, 9, 2, -4, 0];
  21.  
  22. }
  23.  
  24. static void QuickSort(int left, int right) {
  25. System.out.println(left);
  26. System.out.println(right);
  27. if (left < right) {
  28. // int key = (left + right) / 2; #missing numbers before key's index
  29. int key = numbers[(left + right) / 2];
  30. int l = left;
  31. int r = right;
  32. while (l <= r) {
  33.  
  34. while (numbers[l] < key) {
  35. l++;
  36. }
  37. // while (numbers[r] >= key) { #just ">" is enough, "=" causes infinity loop
  38. while (numbers[r] > key) {
  39. r--;
  40. }
  41. if (l <= r) {
  42. int t = numbers[r];
  43. numbers[r] = numbers[l];
  44. numbers[l] = t;
  45. l++;
  46. r--;
  47. }
  48. }
  49.  
  50. if (r > left)
  51. QuickSort(left, r);
  52. if (right > l)
  53. QuickSort(l, right);
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment