-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.txt
343 lines (251 loc) · 6.96 KB
/
read.txt
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Harsh
def bucket_sort(input_str):
buckets = [0] * 26
for char in input_str:
index = ord(char) - ord('a')
buckets[index] += 1
output = ''
for i in range(len(buckets)):
for j in range(buckets[i]):
output += chr(i + ord('a'))
return output
print bucket_sort('cba')
# Ruchir
def bucketSort( sample_string):
sample_list = list(sample_string)
maximum = 26
counter = [0] * ( maximum + 1 )
for ch in sample_list:
counter[ord(ch) - ord('a')] += 1
ndx = 0;
for i in range( len( counter ) ):
while 0 < counter[i]:
sample_list[ndx] = counter[i]
ndx += 1
counter[i] -= 1
return ''.join(sample_list)
Tgma
#santosh
// Bucketsort integers in C
void bucket_sort (int arr[], int n)
{
//Here range is [1,1000]
int m = 1001;
//Create m empty buckets
int buckets[m];
//Intialize all buckets to 0
for (int i = 0; i < m; ++i)
buckets[i] = 0;
//Insert them to buckets
for (int i = 0; i < n; ++i)
++buckets[arr[i]];
//Sort and concatenate
for (int i = 0, j = 0; j < m; ++j)
for (int k = buckets[j]; k > 0; --k)
arr[i++] = j;
}
#Nick:
import sys
def bucketsort(iString):
count = [0]*128
for i in iString:
index = ord(i)
count[index] = count[index] + 1
for j in range(0,128):
for k in range(0,count[j]):
sys.stdout.write(chr(j))
iString = "interview kickstart"
bucketsort(iString)
# tj
#!/usr/bin/env ruby
def bucket_sort(str)
buckets = Array.new(127) { 0 } # ascii table
str.split(//).each { |letter| buckets[letter.ord] += 1 }
buckets.each_with_index.map { |count, ord| ord.chr * count }.join
end
# sorted = bucket_sort('interview kickstart')
# p sorted
# p sorted == " aceeiiikknrrstttvw"
#GURU
static String BucketSort(String input)
{
List<int>[] bucket = new List<int>[26];
for (int i = 0; i < bucket.Length; i++)
{
bucket[i] = new List<int>();
}
for(int j=0; j<input.Length;j++ )
{
bucket[(int)input[j] -'a'].Add(j);
}
String returnStr = "";
for (int i = 0; i < bucket.Length; i++)
{
foreach(int pos in bucket[i])
{
returnStr += input[pos];
}
}
return returnStr;
}
# tj
def merge_sorted_arrays(a, b)
ai, bi, ret = 0, 0, []
loop do
if ai >= a.size && bi >= b.size
break
elsif ai >= a.size || a[ai] > b[bi]
ret << b[bi]; bi += 1
elsif bi >= b.size || a[ai] <= b[bi]
ret << a[ai]; ai += 1
end
end
ret
end
# harsh
def merge(arr1, arr2):
i = 0
j = 0
k = 0
output = [0] * (len(arr1) + len(arr2))
while i < len(arr1) and j < len(arr2):
if arr1[i] <= arr2[j]:
output[k] = arr1[i]
i += 1
else:
output[k] = arr2[j]
j += 1
k += 1
while i < len(arr1):
output[k] = arr1[i]
i += 1
k += 1
while j < len(arr2):
output[k] = arr2[j]
j += 1
k += 1
return output
print merge([4,6], [1,2])
# Ruchir
def combine(data1, data2):
combined = [0 for _ in xrange(len(data1) + len(data2))]
i,j,k = 0,0,0
while (i < len(data1) and j < len(data2)):
if data1[i] < data2[j]:
combined[k] = data1[i]
i += 1
else:
combined[k] = data2[j]
j += 1
k += 1
while i < len(data1):
combined[k] = data1[i]
i += 1
k += 1
while j < len(data2):
combined[k] = data2[j]
j += 1
k += 1
return combined
>>> combine([1,4,7], [2,3,5])
[1, 2, 3, 4, 5, 7]
//Merge Sort // Neera
static int[] merge(int[] a1, int[] a2) {
if (a1.length == 0) return a2;
if (a2.length == 0) return a1;
int[] res = new int[a1.length + a2.length];
int m = 0; //a1 pointer
int n = 0; //a2 pointer
int k = 0; // merged list pointer
while (m < a1.length && n < a2.length) {
if (a1[m] > a2[n]) {
res[k++] = a2[n++];
} else if (a1[m] < a2[n]) {
res[k++] = a1[m++];
} else {
res[k++] = a1[m++];
res[k++] = a2[n++];
}
}
if (m < a1.length) {
while (m < a1.length) {
res[k++] = a1[m++];
}
}
if (n < a2.length) {
while (n < a1.length) {
res[k++] = a1[n++];
}
}
return res;
}
// merge function
static int[] merge(int[] a1, int[] a2) {
int i =0, j = 0, k = 0;
int[] result = new int[a1.length + a2.length];
while(i < a1.length && j < a2.length) {
if (a1[i] <= a2[j])
result[k++] = a1[i++];
else if (a2[j] < a1[i])
result[k++] = a2[j++];
}
while(j < a2.length) {
result[k++] = a2[j++];
}
while(i < a1.length)
result[k++] = a1[i++];
return result;
}
// Just print out the value.. dont return anything
binarySearch(int[] data, int start, int end, int value)
//this returns index of value in data array, -1 if not found
public static int binarySearch(int[] data, int start, int end, int value){
if(start > end)
return -1;
if(start == end){
if(data[start] == value)
return start;
else
return -1;
}
int mid = (start+end) / 2;
if(data[mid] == value)
return mid;
if(data[mid] < value)
return binarySearch(data, start, mid-1, value);
else
return binarySearch(data, mid+1, end , value);
}
# Ruchir
def binarySearch(data, start, end, value):
if start > end:
return False
if len(data) == 0:
return False
else:
midpoint = start + (end - start)//2
if data[midpoint] == value:
return True
else:
if (value < data[midpoint]):
return binarySearch(data, start, midpoint, value)
else:
return binarySearch(data, midpoint+1, end, value)
>>> binarySearch([1,2,3,4,5,6], 0, 5, 3)
True
>>> binarySearch([1,2,3,4,5,6], 0, 5, 7)
False
# Nick
import math
def binarySearch(data, start, end, value):
N_low = int(math.floor((end-start) / 2.0))
N_high = int(math.ceil((end-start) / 2.0))
if value < data[N_low]:
binarySearch(data, start, start+N_low,value)
elif value > data[N_low]:
binarySearch(data, end-N_high,end,value)
elif value == data[N_low]:
print "Found"
print N_low
data = [1,2,3,4,5,6,7,8,9]
binarySearch(data,0,len(data),3)