Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe78789

Browse filesBrowse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#397 from khongta0932488598/patch-1
QuickSortAlgo.java
2 parents 6959592 + 01d6999 commit fe78789
Copy full SHA for fe78789

File tree

Expand file treeCollapse file tree

1 file changed

+45
-31
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+45
-31
lines changed
Open diff view settings
Collapse file

‎Sorts/QuickSort.java‎

Copy file name to clipboardExpand all lines: Sorts/QuickSort.java
+45-31Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class QuickSort {
1515
* Sorts the array in increasing order
1616
**/
1717

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);
18+
public static <T extends Comparable<T>> void QS(List<T> list, int left, int right) {
19+
if (left>=right) { return }
20+
else
21+
{
22+
int pivot = partition(array, left,right);
23+
QS(list, left, pivot- 1);
24+
QS(list, pivot + 1, right);
2325
}
2426
}
2527

@@ -32,17 +34,32 @@ public static <T extends Comparable<T>> void QS(T array[], int start, int end) {
3234
* Finds the partition index of an array
3335
**/
3436

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++;
37+
public static <T extends Comparable<T>> int partition(List<T> list, int left, int right) {
38+
int mid=(left+right)/2;
39+
T pivot=list.get(mid);
40+
swap(list,mid,right);
41+
while(left<right)
42+
{
43+
while(left<right && pivot.compareTo(list.get(left))>=0)
44+
{
45+
++left;
46+
}
47+
if(left<right)
48+
{
49+
swap(list,left,right);
50+
--right;
51+
}
52+
while(left<right && list.get(right).compareTo(pivot)>=0)
53+
{
54+
--right;
55+
}
56+
if(left<right)
57+
{
58+
swap(list,left,right);
59+
++left;
4260
}
4361
}
44-
swap(array, PIndex, end);
45-
return PIndex;
62+
return left;
4663
}
4764

4865
/**
@@ -54,39 +71,36 @@ public static <T extends Comparable<T>> int partition(T array[], int start, int
5471
* Swaps initial and fin element
5572
**/
5673

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;
74+
public static <T extends Comparable<T>> void swap(List<E> list, int initial, int fin) {
75+
E temp= list.get(initial);
76+
list.set(initial,list.get(fin));
77+
list.set(fin,temp);
6178
}
6279

6380
// Driver Program
6481
public static void main(String[] args) {
6582

6683
// 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-
}
84+
ArrayList<Integer> array = new ArrayList<Integer>(9);
85+
array = {3,4,1,32,0,2,44,111,5};
7286

73-
QS(array, 0, arr.length-1);
87+
QS(array, 0, array.size()-1);
7488

7589
//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] + " ");
90+
for (int i=0;i<array.size();i++) {
91+
System.out.print(array.get(i) + " ");
7892
}
7993
System.out.println();
8094

81-
// String Input
82-
String[] array1 = {"c", "a", "e", "b","d"};
95+
ArrayList<String> array1=new ArrayList<String>(5);
96+
array1 = {"c", "a", "e", "b","d"};
8397

84-
QS(array1, 0,array1.length-1);
98+
QS(array1, 0,array1.size()-1);
8599

86100
//Output => a b c d e
87-
for(int i=0; i<array1.length; i++)
101+
for(int i=0; i<array1.size(); i++)
88102
{
89-
System.out.print(array1[i]+"\t");
103+
System.out.print(array1.get(i)+"\t");
90104
}
91105
}
92106
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.