Source : https://www.hackerrank.com/challenges/quicksort2
In the previous challenge, you wrote a partition method to split an array into two sub-arrays, one containing smaller elements and one containing larger elements than a given number. This means you 'sorted' half the array with respect to the other half. Can you repeatedly use partition to sort an entire array?
Guideline
In Insertion Sort, you simply went through each element in order and inserted it into a sorted sub-array. In this challenge, you cannot focus on one element at a time, but instead must deal with whole sub-arrays, with a strategy known as "divide and conquer".
When partition is called on an array, two parts of the array get 'sorted' with respect to each other. If partition is then called on each sub-array, the array will now be split into four parts. This process can be repeated until the sub-arrays are small. Notice that when partition is called on just one of the numbers, they end up being sorted.
Can you repeatedly call partition so that the entire array ends up sorted?
Print Sub-Arrays
In this challenge, print your array every time your partitioning method finishes, i.e. whenever two subarrays, along with the pivot, are merged together.
- The first element in a sub-array should be used as a pivot.
- Partition the left side before partitioning the right side.
- The pivot should be placed between sub-arrays while merging them.
- Array of length or less will be considered sorted, and there is no need to sort or to print them.
Note
Please maintain the original order of the elements in the left and right partitions while partitioning around a pivot element.
For example:Partition about the first element for the array A[]={5, 8, 1, 3, 7, 9, 2} will be {1, 3, 2, 5, 8, 7, 9}
Input Format
There will be two lines of input:
- - the size of the array
- - the n numbers of the array
Output Format
Print every partitioned sub-array on a new line.
Constraints
Each number is unique.
Sample Input
7
5 8 1 3 7 9 2
Sample Output
2 31 2 37 8 91 2 3 5 7 8 9
Explanation
This is a diagram of Quicksort operating on the sample array:
Task
The method quickSort takes in a parameter, , an unsorted array. Use the Quicksort algorithm to sort the entire array.
Source : https://www.hackerrank.com/challenges/quicksort2
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
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) { | |
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); | |
quickSort(array, start, pivotIndex - 1); | |
quickSort(array, pivotIndex + 1, end); | |
printArray(array, start, end); | |
} | |
} | |
/* Partition/Quicksort "Stable Sort" version using O(n) space */ | |
private static int partition(int[] array, int start, int end) { | |
int pivotValue = array[start]; // not a great choice of pivot | |
ArrayList<Integer> leftList = new ArrayList(); | |
ArrayList<Integer> rightList = new ArrayList(); | |
for (int i = start + 1 ; i <= end; i++) { | |
if (array[i] < pivotValue) { | |
leftList.add(array[i]); | |
} else { | |
rightList.add(array[i]); | |
} | |
} | |
/* Recreate array */ | |
copy(leftList, array, start); | |
int newPivotIndex = start + leftList.size(); | |
array[newPivotIndex] = pivotValue; | |
copy(rightList, array, newPivotIndex + 1); | |
return newPivotIndex; | |
} | |
private static void copy(ArrayList<Integer> list, int [] array, int startIndex) { | |
for (int num : list) { | |
array[startIndex++] = num; | |
} | |
} | |
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