Skip to content

Commit affcbac

Browse files
Merge pull request TheAlgorithms#87 from varunu28/master
Updated QuickSort.java
2 parents a009cca + 146a08c commit affcbac

File tree

3 files changed

+166
-184
lines changed

3 files changed

+166
-184
lines changed

Sorts/MergeSort.java

Lines changed: 73 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,100 @@
1-
import java.util.Scanner;
2-
31
/**
4-
* This class implements MergeSort
5-
* @author Unknown
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
64
*
75
*/
8-
public class MergeSort {
9-
/** Array for mergeSort*/
10-
private int[] array;
11-
/** Temp Merge Array*/
12-
private int[] tempMergArr;
13-
/** Length of the array*/
14-
private int length;
156

16-
/**
17-
* Sorts inputArr with merge sort algorithm
18-
*
19-
* @param inputArr Array to be sorted
20-
*/
21-
public final void sort(int inputArr[]) {
22-
this.array = inputArr;
23-
this.length = inputArr.length;
24-
this.tempMergArr = new int[this.length];
25-
this.mergeSort(0, this.length - 1);
26-
}
7+
class MergeSort {
278

289
/**
29-
* Partitions Array into recursively smaller pieces
10+
* This method implements the Generic Merge Sort
3011
*
31-
* @param lowerIndex lower bound to include in the first partition
32-
* @param higherIndex upper bound to include in the third partition
33-
*/
34-
private void mergeSort(int lowerIndex, int higherIndex) {
35-
if (lowerIndex < higherIndex) {
36-
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
37-
// Below step sorts the left side of the array
38-
this.mergeSort(lowerIndex, middle);
39-
// Below step sorts the right side of the array
40-
this.mergeSort(middle + 1, higherIndex);
41-
// Now merge both sides
42-
this.mergeParts(lowerIndex, middle, higherIndex);
12+
* @param arr The array to be sorted
13+
* @param temp The copy of the actual array
14+
* @param left The first index of the array
15+
* @param right The last index of the array
16+
* Recursively sorts the array in increasing order
17+
**/
18+
19+
public static <T extends Comparable<T>> void MS(T[] arr, T[] temp, int left, int right) {
20+
if (left < right) {
21+
int mid = left + (right - left) / 2;
22+
MS(arr, temp, left, mid);
23+
MS(arr, temp,mid + 1, right);
24+
merge(arr, temp, left, mid, right);
4325
}
26+
4427
}
4528

4629
/**
47-
* Merges partitions
30+
* This method implements the merge step of the merge sort
4831
*
49-
* @param lowerIndex The lower index
50-
* @param middle The middle index
51-
* @param higherIndex The higher index
52-
*/
53-
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
54-
for (int i = lowerIndex; i <= higherIndex; i++) {
55-
this.tempMergArr[i] = this.array[i];
32+
* @param arr The array to be sorted
33+
* @param temp The copy of the actual array
34+
* @param left The first index of the array
35+
* @param mid The middle index of the array
36+
* @param right The last index of the array
37+
* merges two parts of an array in increasing order
38+
**/
39+
40+
public static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) {
41+
for (int i=left;i<=right;i++) {
42+
temp[i] = arr[i];
5643
}
57-
int i = lowerIndex;
58-
int j = middle + 1;
59-
int k = lowerIndex;
60-
while (i <= middle && j <= higherIndex) {
61-
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
62-
this.array[k] = this.tempMergArr[i];
44+
45+
int i= left;
46+
int j = mid + 1;
47+
int k = left;
48+
49+
while (i<=mid && j<=right) {
50+
if (temp[i].compareTo(temp[j]) <= 0) {
51+
arr[k] = temp[i];
6352
i++;
64-
} else {
65-
this.array[k] = this.tempMergArr[j];
53+
}
54+
else {
55+
arr[k] = temp[j];
6656
j++;
6757
}
6858
k++;
6959
}
70-
while (i <= middle) {
71-
this.array[k] = this.tempMergArr[i];
72-
k++;
60+
61+
while (i <= mid) {
62+
arr[k] = temp[i];
7363
i++;
64+
k++;
7465
}
75-
7666
}
7767

78-
/**
79-
* Gets input to sort
80-
*
81-
* @return unsorted array of integers to sort
82-
*/
83-
public static int[] getInput() {
84-
final int numElements = 6;
85-
int[] unsorted = new int[numElements];
86-
Scanner input = new Scanner(System.in);
87-
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
88-
for (int i = 0; i < numElements; i++) {
89-
unsorted[i] = input.nextInt();
68+
// Driver program
69+
public static void main(String[] args) {
70+
71+
// Integer Input
72+
int[] arr = {4,23,6,78,1,54,231,9,12};
73+
Integer[] array = new Integer[arr.length];
74+
for (int i=0;i<array.length;i++) {
75+
array[i] = arr[i];
9076
}
91-
input.close();
92-
return unsorted;
93-
}
9477

95-
/**
96-
* Main Method
97-
*
98-
* @param args Command line arguments
99-
*/
100-
public static void main(String args[]) {
101-
int[] inputArr = getInput();
102-
MergeSort mergeSort = new MergeSort();
103-
mergeSort.sort(inputArr);
104-
for (int i : inputArr) {
105-
System.out.print(i);
106-
System.out.print(" ");
78+
// Copy of actual array
79+
Integer[] temp = new Integer[arr.length];
80+
81+
MS(array, temp, 0, arr.length-1);
82+
83+
// Output => 1 4 6 9 12 23 54 78 231
84+
for (int i=0;i<arr.length;i++) {
85+
System.out.print(array[i] + " ");
86+
}
87+
System.out.println();
88+
89+
// String Input
90+
String[] array1 = {"c", "a", "e", "b","d"};
91+
String[] temp1 = new String[array1.length];
92+
MS(array1, temp1, 0, array1.length-1);
93+
94+
//Output => a b c d e
95+
for(int i=0; i<array1.length; i++)
96+
{
97+
System.out.print(array1[i]+"\t");
10798
}
10899
}
109100
}

Sorts/QuickSort.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
4+
*
5+
*/
6+
7+
class QuickSort {
8+
9+
/**
10+
* This method implements the Generic Quick Sort
11+
*
12+
* @param array The array to be sorted
13+
* @param start The first index of an array
14+
* @param end The last index of an array
15+
* Sorts the array in increasing order
16+
**/
17+
18+
public static <T extends Comparable<T>> void QS(T array[], int start, int end) {
19+
if (start < end) {
20+
int PIndex = partition(array, start, end);
21+
QS(array, start, PIndex - 1);
22+
QS(array, PIndex + 1, end);
23+
}
24+
}
25+
26+
/**
27+
* This method finds the partition index for an array
28+
*
29+
* @param array The array to be sorted
30+
* @param start The first index of an array
31+
* @param end The last index of an array
32+
* Finds the partition index of an array
33+
**/
34+
35+
public static <T extends Comparable<T>> int partition(T array[], int start, int end) {
36+
T pivot = array[end];
37+
int PIndex = start;
38+
for (int i=start;i<end;i++) {
39+
if (array[i].compareTo(pivot) <= 0) {
40+
swap(array, i, PIndex);
41+
PIndex++;
42+
}
43+
}
44+
swap(array, PIndex, end);
45+
return PIndex;
46+
}
47+
48+
/**
49+
* This method swaps two elements of an array
50+
*
51+
* @param array The array to be sorted
52+
* @param initial The first element
53+
* @param fin The second element
54+
* Swaps initial and fin element
55+
**/
56+
57+
public static <T extends Comparable<T>> void swap(T[] array, int initial, int fin) {
58+
T temp = array[initial];
59+
array[initial] = array[fin];
60+
array[fin] = temp;
61+
}
62+
63+
// Driver Program
64+
public static void main(String[] args) {
65+
66+
// For integer input
67+
int[] arr = {3,4,1,32,0,2,44,111,5};
68+
Integer[] array = new Integer[arr.length];
69+
for (int i=0;i<arr.length;i++) {
70+
array[i] = arr[i];
71+
}
72+
73+
QS(array, 0, arr.length-1);
74+
75+
//Output => 0 1 2 3 4 5 32 44 111
76+
for (int i=0;i<array.length;i++) {
77+
System.out.print(array[i] + " ");
78+
}
79+
System.out.println();
80+
81+
// String Input
82+
String[] array1 = {"c", "a", "e", "b","d"};
83+
84+
QS(array1, 0,array1.length-1);
85+
86+
//Output => a b c d e
87+
for(int i=0; i<array1.length; i++)
88+
{
89+
System.out.print(array1[i]+"\t");
90+
}
91+
}
92+
}
93+

Sorts/Quicksort.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)