This repository was archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
56 lines (49 loc) · 1.63 KB
/
QuickSort.java
File metadata and controls
56 lines (49 loc) · 1.63 KB
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
package com.ematrix;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class QuickSort {
public void quickSort(Queue<Integer> S){
Integer n=S.size();
Integer pivot=S.peek();
if (n < 2) return;
Queue<Integer> L=new ConcurrentLinkedQueue<>();
Queue<Integer> E=new ConcurrentLinkedQueue<>();
Queue<Integer> G=new ConcurrentLinkedQueue<>();
while(!S.isEmpty()){
int c=S.poll();
if(c<pivot){
L.add(c);
}
else if(c==pivot){
E.add(c);
}else{
G.add(c);
}
}
quickSort(L);
quickSort(G);
while (!L.isEmpty( )){
S.add(L.poll());
}
while (!E.isEmpty( )){
S.add(E.poll());
}
while (!G.isEmpty( )){
S.add(G.poll());
}
}
public static void main(String[] args){
QuickSort newSort=new QuickSort();
Queue<Integer> valuesToSot=new ConcurrentLinkedQueue<>(){};
valuesToSot.add(3);
valuesToSot.add(7);
valuesToSot.add(9);
valuesToSot.add(5);
valuesToSot.add(2);
valuesToSot.add(8);
valuesToSot.add(15);
newSort.quickSort(valuesToSot);
System.out.println(valuesToSot);
}
}