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

Latest commit

 

History

History
History
107 lines (92 loc) · 2.73 KB

File metadata and controls

107 lines (92 loc) · 2.73 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
*
* @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");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.