Source : https://www.hackerrank.com/challenges/quicksort3
The previous version of Quicksort was easy to understand, but it was not optimal. It required copying the numbers into other arrays, which takes up space and time. To make things faster, one can create an "in-place" version of Quicksort, where the numbers are all sorted within the array itself.
Challenge
Create an in-place version of Quicksort. You need to follow Lomuto Partitioning method.
Guideline
Instead of copying the array into multiple sub-arrays, use indices to keep track of the different sub-arrays. You can pass the indices to a modified partition method. The partition method should partition the sub-array and then return the index location where the pivot gets placed, so you can then call partition on each side of the pivot.
- Always select the last element in the 'sub-array' as a pivot.
- Partition the left side and then the right side of the sub-array.
- Print out the whole array at the end of every partitioning method.
- An array of length or less will be considered sorted, and there is no need to sort or to print them.
Since you cannot just create new sub-arrays for the elements, partition method will need to use another trick to keep track of which elements are greater and which are lower than the pivot.
The In-place Trick
- If an element is lower than the pivot, you should swap it with a larger element on the left-side of the sub-array.
- Greater elements should remain where they are.
- At the end of the partitioning method, the pivot should be swapped with the first element of the right partition, consisting of all larger elements, of the sub-array.
- To ensure that you don't swap a small element with another small element, use an index to keep track of the small elements that have already been swapped into their "place".
Input Format
There will be two lines of input:
- - the size of the array
- - the numbers of the array
Output Format
Print the entire array on a new line at the end of every partitioning method.
Constraints
There are no duplicate numbers.
Sample Input
7
1 3 9 8 2 7 5
Sample Output
1 3 2 5 9 7 81 2 3 5 9 7 81 2 3 5 7 8 9
Explanation
5 is initally selected as the pivot, and the array is partitioned as shown in the diagram. The left side is partitioned next with 2 as the pivot. Finally, the right side is partitioned with 8 as the pivot. The entire array is now sorted.
Source : https://www.hackerrank.com/challenges/quicksort3
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int s = scan.nextInt(); | |
int[] array = new int[s]; | |
for (int i = 0; i < s; i++) { | |
array[i] = scan.nextInt(); | |
} | |
scan.close(); | |
quickSort(array); | |
} | |
public static void quickSort(int [] array) { | |
if (array != null) { | |
quickSort(array, 0, array.length - 1); | |
} | |
} | |
private static void quickSort(int [] array, int start, int end) { | |
if (start < end) { | |
int pivotIndex = partition(array, start, end); | |
printArray(array, 0, array.length - 1); | |
quickSort(array, start, pivotIndex - 1); | |
quickSort(array, pivotIndex + 1, end); | |
} | |
} | |
/* Must use "Lomuto Partition" scheme (which isn't very efficient) from Wikipedia: | |
https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme */ | |
private static int partition(int[] A, int lo, int hi) { | |
int pivot = A[hi]; | |
int i = lo - 1; | |
for (int j = lo; j <= hi - 1; j++) { | |
if (A[j] <= pivot) { | |
i++; | |
swap(A, i , j); | |
} | |
} | |
swap(A, i + 1, hi); | |
return i + 1; | |
} | |
private static void swap(int [] array, int i, int j) { | |
int temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
private static void printArray(int[] array, int start, int end) { | |
for (int i = start; i <= end; i++) { | |
System.out.print(array[i] + " "); | |
} | |
System.out.println(); | |
} | |
} |
No comments:
Post a Comment