Dimitar182

Untitled

Jan 26th, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. //QuickSort Main
  2. package qs;
  3. import java.util.*;
  4.  
  5. public class main {
  6.  
  7. public static void main(String[] args) {
  8. Scanner input = new Scanner(System.in);
  9. System.out.println("Imput num of elements");
  10. int N = input.nextInt();
  11. int[] ARR = new int[N];
  12. for (int i = 0; i < N; i++) {
  13. System.out.printf("Input[%d] elements:" , i);
  14. ARR [i] = input.nextInt();
  15. }
  16. System.out.printf("BEFORE SORT" + Arrays.toString(ARR));
  17. quicksort.QuickSortMethods.QuickSort(ARR,0,n-1);
  18. System.out.println("After sorting : " + Arrays.toString(ARR));
  19. }
  20. }
  21. ---------------------------------------------------------------------------
  22. //QuickSortMethods
  23. package qs;
  24.  
  25. public class methods {
  26.  
  27. public static void QuickSort(int[] ARR,int low, int high ) {
  28. if(low < high) {
  29. int PivotIndex = Partition(ARR, low, high);
  30. QuickSort(ARR, low, PivotIndex-1);
  31. QuickSort(ARR, PivotIndex+1, high);
  32. }
  33. }
  34.  
  35. private static int Partition(int[] aRR, int low, int high) {
  36. //
  37. int Pivot = aRR[high];
  38. int i = low-1;
  39. for(int j = low; j < high; j++) {
  40. if(aRR[j] < Pivot);
  41. i++;
  42. //swap
  43. int temp = aRR[j];
  44. aRR[j] = aRR[i];
  45. aRR[i] = temp;
  46. }
  47. //слагаме pivot елемента на правилното място
  48. int temp = aRR[i+1];
  49. aRR[i+1] = aRR[high];
  50. aRR[high] = temp;
  51. return i + 1;
  52. }
  53. }
  54.  
  55.  
Add Comment
Please, Sign In to add comment