Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class QuickSort {
- static void swap(int[] arr, int i, int j)
- {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- static int partition(int[] arr, int leftEnd, int rightEnd) {
- int left = leftEnd;
- int right = rightEnd;
- int pivot = arr[left];
- while (left < right) {
- while (left < rightEnd && arr[left] <= pivot)
- left++;
- while (right > leftEnd && arr[right] > pivot)
- right--;
- if (left < right)
- swap(arr,left,right);
- else
- swap(arr,leftEnd,right);
- }
- return right;
- }
- static void quickSort(int[] arr, int leftEnd, int rightEnd) {
- if (leftEnd > rightEnd) {
- return;
- }
- else {
- int p = partition(arr, leftEnd, rightEnd);
- quickSort(arr, leftEnd, p - 1);
- quickSort(arr, p + 1, rightEnd);
- }
- }
- public static void main(String[] args) {
- int[] arr = { 10, 7, 8, 9, 1, 5 };
- int N = arr.length;
- // Function call
- quickSort(arr, 0, N - 1);
- System.out.println("Sorted array:");
- for (int j : arr) {
- System.out.println(j);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement