mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
The sorting structure was driven to a general java project structure
Fixed the bugs in QuickSort Refactored QuickSort
This commit is contained in:
@ -1,107 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @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(List<T> list, int left, int right) {
|
||||
if (left>=right) { return }
|
||||
else
|
||||
{
|
||||
int pivot = partition(array, left,right);
|
||||
QS(list, left, pivot- 1);
|
||||
QS(list, pivot + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(List<T> list, int left, int right) {
|
||||
int mid=(left+right)/2;
|
||||
T pivot=list.get(mid);
|
||||
swap(list,mid,right);
|
||||
while(left<right)
|
||||
{
|
||||
while(left<right && pivot.compareTo(list.get(left))>=0)
|
||||
{
|
||||
++left;
|
||||
}
|
||||
if(left<right)
|
||||
{
|
||||
swap(list,left,right);
|
||||
--right;
|
||||
}
|
||||
while(left<right && list.get(right).compareTo(pivot)>=0)
|
||||
{
|
||||
--right;
|
||||
}
|
||||
if(left<right)
|
||||
{
|
||||
swap(list,left,right);
|
||||
++left;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(List<E> list, int initial, int fin) {
|
||||
E temp= list.get(initial);
|
||||
list.set(initial,list.get(fin));
|
||||
list.set(fin,temp);
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
|
||||
// For integer input
|
||||
ArrayList<Integer> array = new ArrayList<Integer>(9);
|
||||
array = {3,4,1,32,0,2,44,111,5};
|
||||
|
||||
QS(array, 0, array.size()-1);
|
||||
|
||||
//Output => 0 1 2 3 4 5 32 44 111
|
||||
for (int i=0;i<array.size();i++) {
|
||||
System.out.print(array.get(i) + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
ArrayList<String> array1=new ArrayList<String>(5);
|
||||
array1 = {"c", "a", "e", "b","d"};
|
||||
|
||||
QS(array1, 0,array1.size()-1);
|
||||
|
||||
//Output => a b c d e
|
||||
for(int i=0; i<array1.size(); i++)
|
||||
{
|
||||
System.out.print(array1.get(i)+"\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
94
Sorts/src/sort/QuickSort.java
Normal file
94
Sorts/src/sort/QuickSort.java
Normal file
@ -0,0 +1,94 @@
|
||||
package sort;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||
*
|
||||
*/
|
||||
class QuickSort implements SortAlgorithm {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This method implements the Generic Quick Sort
|
||||
*
|
||||
* @param array The array to be sorted
|
||||
* Sorts the array in increasing order
|
||||
**/
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
doSort(array, 0, array.length - 1);
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The sorting process
|
||||
*
|
||||
* @param left The first index of an array
|
||||
* @param right The last index of an array
|
||||
* @param array The array to be sorted
|
||||
*
|
||||
**/
|
||||
|
||||
private static <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
|
||||
if (left < right) {
|
||||
int pivot = partition(array, left, right);
|
||||
doSort(array, left, pivot - 1);
|
||||
doSort(array, pivot , right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds the partition index for an array
|
||||
*
|
||||
* @param array The array to be sorted
|
||||
* @param left The first index of an array
|
||||
* @param right The last index of an array
|
||||
* Finds the partition index of an array
|
||||
**/
|
||||
|
||||
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
|
||||
int mid = (left + right) / 2;
|
||||
T pivot = array[mid];
|
||||
|
||||
while(left <= right) {
|
||||
while(less(array[left], pivot)){
|
||||
++left;
|
||||
}
|
||||
while(less(pivot, array[right])) {
|
||||
--right;
|
||||
}
|
||||
if(left <= right) {
|
||||
swap(array, left, right);
|
||||
++left;
|
||||
--right;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
|
||||
// For integer input
|
||||
Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12 ,2, 5 ,7 ,8 ,9, 2, 44, 111, 5};
|
||||
|
||||
QuickSort quickSort = new QuickSort();
|
||||
// quickSort.sort(array);
|
||||
|
||||
//Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
|
||||
print(array);
|
||||
|
||||
String[] stringArray = {"c", "a", "e", "b", "d"};
|
||||
quickSort.sort(stringArray);
|
||||
|
||||
//Output => a b c d e
|
||||
print(stringArray);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package Sorts;
|
||||
package sort;
|
||||
|
||||
import static Sorts.SortUtils.*;
|
||||
import static sort.SortUtils.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||
*
|
||||
* @see SortAlgorithm
|
||||
*
|
||||
*/
|
||||
|
@ -1,6 +1,6 @@
|
||||
package Sorts;
|
||||
package sort;
|
||||
|
||||
import static Sorts.SortUtils.*;
|
||||
import static sort.SortUtils.*;
|
||||
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package Sorts;
|
||||
package sort;
|
||||
|
||||
/**
|
||||
* The common interface of most algorithms
|
@ -1,4 +1,4 @@
|
||||
package Sorts;
|
||||
package sort;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
Reference in New Issue
Block a user