Updated QuickSort.java
Updated quicksort to its generic version and changed the filename according to convention
This commit is contained in:
parent
a009ccaca9
commit
e9b1e2542b
93
Sorts/QuickSort.java
Normal file
93
Sorts/QuickSort.java
Normal file
@ -0,0 +1,93 @@
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*
|
||||
*/
|
||||
|
||||
class QuickSort {
|
||||
|
||||
/**
|
||||
* This method implements the Generic Quick Sort
|
||||
*
|
||||
* @param array The array to be sorted
|
||||
* @param start The first index of an array
|
||||
* @param end The last index of an array
|
||||
* Sorts the array in increasing order
|
||||
**/
|
||||
|
||||
public static <T extends Comparable<T>> void QS(T array[], int start, int end) {
|
||||
if (start < end) {
|
||||
int PIndex = partition(array, start, end);
|
||||
QS(array, start, PIndex - 1);
|
||||
QS(array, PIndex + 1, end);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds the partition index for an array
|
||||
*
|
||||
* @param array The array to be sorted
|
||||
* @param start The first index of an array
|
||||
* @param end The last index of an array
|
||||
* Finds the partition index of an array
|
||||
**/
|
||||
|
||||
public static <T extends Comparable<T>> int partition(T array[], int start, int end) {
|
||||
T pivot = array[end];
|
||||
int PIndex = start;
|
||||
for (int i=start;i<end;i++) {
|
||||
if (array[i].compareTo(pivot) <= 0) {
|
||||
swap(array, i, PIndex);
|
||||
PIndex++;
|
||||
}
|
||||
}
|
||||
swap(array, PIndex, end);
|
||||
return PIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method swaps two elements of an array
|
||||
*
|
||||
* @param array The array to be sorted
|
||||
* @param initial The first element
|
||||
* @param fin The second element
|
||||
* Swaps initial and fin element
|
||||
**/
|
||||
|
||||
public static <T extends Comparable<T>> void swap(T[] array, int initial, int fin) {
|
||||
T temp = array[initial];
|
||||
array[initial] = array[fin];
|
||||
array[fin] = temp;
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
|
||||
// For integer input
|
||||
int[] arr = {3,4,1,32,0,2,44,111,5};
|
||||
Integer[] array = new Integer[arr.length];
|
||||
for (int i=0;i<arr.length;i++) {
|
||||
array[i] = arr[i];
|
||||
}
|
||||
|
||||
QS(array, 0, arr.length-1);
|
||||
|
||||
//Output => 0 1 2 3 4 5 32 44 111
|
||||
for (int i=0;i<array.length;i++) {
|
||||
System.out.print(array[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// String Input
|
||||
String[] array1 = {"c", "a", "e", "b","d"};
|
||||
|
||||
QS(array1, 0,array1.length-1);
|
||||
|
||||
//Output => a b c d e
|
||||
for(int i=0; i<array1.length; i++)
|
||||
{
|
||||
System.out.print(array1[i]+"\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,102 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Implementation of QuickSort
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
public class Quicksort{
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
int[] array;
|
||||
int size = 0;
|
||||
|
||||
//Prompt user to create array and its elements
|
||||
System.out.print("Enter the size of the array: ");
|
||||
size = input.nextInt();
|
||||
array = new int[size];
|
||||
for (int i = 0; i < size; i++){
|
||||
System.out.print("For index " + i + ", give an integer input: ");
|
||||
array[i] = input.nextInt();
|
||||
}
|
||||
|
||||
//Output inputted array
|
||||
System.out.println("The array is: ");
|
||||
printarray(array);
|
||||
System.out.println();
|
||||
|
||||
//Run quicksort, and output sorted array
|
||||
quicksort(array, 0, array.length - 1);
|
||||
System.out.println("The sorted array is: ");
|
||||
printarray(array);
|
||||
System.out.println();
|
||||
|
||||
input.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* QuickSort method
|
||||
*
|
||||
* @param ar Array to perform QuickSort
|
||||
* @param start Start of the array
|
||||
* @param end End of the array
|
||||
*/
|
||||
public static void quicksort(int[] ar, int start, int end){
|
||||
int[] array;
|
||||
|
||||
int i = start, j = end;
|
||||
if (end-start >= 1){
|
||||
int pivot = ar[end];
|
||||
while (i< j){
|
||||
while (ar[i]<pivot && i<end){
|
||||
i++;
|
||||
}
|
||||
while (ar[j]>=pivot && j>start){
|
||||
j--;
|
||||
}
|
||||
if (i<j){
|
||||
swap(ar, i, j);
|
||||
}
|
||||
} swap(ar, end, i);
|
||||
|
||||
quicksort(ar, start, i-1);
|
||||
quicksort(ar, i+1, end);
|
||||
} else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper Method 1 - Swaps elements of an array
|
||||
*
|
||||
* @param ar Array to be used
|
||||
* @param index1 Index 1 to be switched with Index 2
|
||||
* @param index2 Index 2 to be switched with Index 1
|
||||
*/
|
||||
public static void swap(int[] ar, int index1, int index2){
|
||||
|
||||
int temp = ar[index1];
|
||||
ar[index1] = ar[index2];
|
||||
ar[index2] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper Method 2 - Prints the elements of an array
|
||||
*
|
||||
* @param array Array to be printed
|
||||
*/
|
||||
public static void printarray(int[] array){
|
||||
|
||||
for (int data : array){
|
||||
System.out.print(data + " ");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user