Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //QuickSort Main
- package qs;
- import java.util.*;
- public class main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Imput num of elements");
- int N = input.nextInt();
- int[] ARR = new int[N];
- for (int i = 0; i < N; i++) {
- System.out.printf("Input[%d] elements:" , i);
- ARR [i] = input.nextInt();
- }
- System.out.printf("BEFORE SORT" + Arrays.toString(ARR));
- quicksort.QuickSortMethods.QuickSort(ARR,0,n-1);
- System.out.println("After sorting : " + Arrays.toString(ARR));
- }
- }
- ---------------------------------------------------------------------------
- //QuickSortMethods
- package qs;
- public class methods {
- public static void QuickSort(int[] ARR,int low, int high ) {
- if(low < high) {
- int PivotIndex = Partition(ARR, low, high);
- QuickSort(ARR, low, PivotIndex-1);
- QuickSort(ARR, PivotIndex+1, high);
- }
- }
- private static int Partition(int[] aRR, int low, int high) {
- //
- int Pivot = aRR[high];
- int i = low-1;
- for(int j = low; j < high; j++) {
- if(aRR[j] < Pivot);
- i++;
- //swap
- int temp = aRR[j];
- aRR[j] = aRR[i];
- aRR[i] = temp;
- }
- //слагаме pivot елемента на правилното място
- int temp = aRR[i+1];
- aRR[i+1] = aRR[high];
- aRR[high] = temp;
- return i + 1;
- }
- }
Add Comment
Please, Sign In to add comment