-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackqs.java
More file actions
77 lines (70 loc) · 1.99 KB
/
Copy pathStackqs.java
File metadata and controls
77 lines (70 loc) · 1.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.*;
public class Stackqs{
public static String [] getL() {//using input, creates list
int index = 0; //index to help copy input into a list
final int max = 1000; //maximum size of list
try (Scanner a = new Scanner (System.in)) {
String [] list = new String [max]; //array which will take input, size 1000
System.out.println();
while (a.hasNextLine() && index<list.length){//copies input into array
list[index]=a.nextLine();
index++;
}
System.out.println();
int size = index;//when a.hasNextLine() returns false,
//the size of index is the size of the final array
index=0; //resets index for further loop
String [] finalList= new String [size];
while(index<size) {//copies elements from first array to final array
finalList[index] = list[index];
index++;
}
return finalList;
}
}
public static void swap(String[] s, int i, int j){
String swap = s[i];
s[i]=s[j];
s[j]=swap;
}
public static void qsStack(String[] s){
System.out.println();
Mystack stack = new Mystack(s.length);
stack.push(0);
stack.push(s.length-1);
while(!(stack.isEmpty())){
int high = stack.pop();
int low=stack.pop();
if(high>low){
String pivot = s[low];
int i=low-1;
int j=low;
while(j<=high){
if(s[j].compareTo(pivot)<0){
i++;
swap(s, i, j);
}
j++;
}
swap(s, i, low);
int partition = i;
stack.push(low);
stack.push(i);
stack.push(partition);
}
}
}
public static void printlist (String [] a) {//this prints a given array
int print = 0;
while(print<a.length){
System.out.println(a[print]);
print=print+1;
}
}
public static void main(String [] a){//calls input array, quicksort method, prints list
System.out.println();
a = getL();
qsStack(a);
printlist(a);
}
}