-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort-WorstCase-Input-Creator.java
More file actions
178 lines (141 loc) · 4.1 KB
/
Copy pathQuickSort-WorstCase-Input-Creator.java
File metadata and controls
178 lines (141 loc) · 4.1 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution {
/*
*
* @Author Yekta Anıl Aksoy
* This code is created on 02/04/2017
* Fast Reader class is used for making fast input / output operation
*
* Main purpose is to get input n to create an array that includes numbers 1 to n and
* Arrange these numbers to make WORST CASE SCENARIO for QuickSort when Pivot is chosen as mid.
* Output count demonstrates a number of comparisons in QuickSort.
*/
static FastReader in;
static PrintWriter out;
static int[] array;
static int count=0;
static void Qsort(int left,int right){
int i,j,key,buf;
key = array[(left+right)/2];
i=left;
j=right;
do{
while(array[i]<key){
i++;
count++;
}
while(key<array[j]){
j--;
count++;
}
count+=2;
if(i<=j){
buf = array[i];
array[i] = array[j];
array[j] = buf;
i++;
j--;
}
}while(i<=j);
if(left<j){
Qsort(left,j);
}
if(i<right)
{
Qsort(i,right);
}
}
public static void Solve(){
System.out.print("Enter the n:");
int n = in.nextInt();
if(n==1){
out.print("1");
}
else if(n==2){
out.print("1 2");
}
else{
array = new int[n];
array[0] = 1;
array[1] = 2;
for(int i =2 ;i<n;i++){
array[i]=i+1;
if(array[i/2]<array[i]){
int tmp = array[i/2];
array[i/2] = array[i];
array[i] = tmp;
}
}
out.print("Created input : ");
for(int number: array){
out.print(number+" ");
}
Qsort(0,n-1);
out.println("\nAbove input given to QuickSort and After Quick sort : "+ Arrays.toString(array));
out.println("Number of comparison in QuickSort : "+count+" ( O(n^2))");
}
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
//long s = System.currentTimeMillis();
Solve();
out.flush();
//System.out.println(System.currentTimeMillis()-s +"ms");
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}