forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingo_sort.py
More file actions
300 lines (231 loc) · 7.6 KB
/
Copy pathbingo_sort.py
File metadata and controls
300 lines (231 loc) · 7.6 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
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
"""
Author: ADWAITA JADHAV
Created On: 4th October 2025
Bingo Sort Algorithm
Time Complexity: O(n + k) where k is the range of input, O(n^2) worst case
Space Complexity: O(1)
Bingo Sort is a variation of selection sort that is particularly efficient
when there are many duplicate elements in the array. It processes all
elements with the same value in a single pass.
"""
import inspect
def sort(_list):
"""
Sort a list using Bingo Sort algorithm
:param _list: list of values to sort
:return: sorted list
"""
if not _list or len(_list) <= 1:
return _list[:]
# Make a copy to avoid modifying the original list
arr = _list[:]
n = len(arr)
# Find the minimum and maximum values
min_val = max_val = arr[0]
for i in range(1, n):
if arr[i] < min_val:
min_val = arr[i]
if arr[i] > max_val:
max_val = arr[i]
# If all elements are the same, return the array
if min_val == max_val:
return arr
# Bingo sort main algorithm
bingo = min_val
next_pos = 0
while next_pos < n:
# Find next bingo value
next_bingo = max_val
for i in range(next_pos, n):
if arr[i] > bingo and arr[i] < next_bingo:
next_bingo = arr[i]
# Place all instances of current bingo value at correct positions
for i in range(next_pos, n):
if arr[i] == bingo:
arr[i], arr[next_pos] = arr[next_pos], arr[i]
next_pos += 1
bingo = next_bingo
# If no next bingo found, we're done
if next_bingo == max_val:
break
return arr
def bingo_sort_optimized(_list):
"""
Optimized version of Bingo Sort
:param _list: list of values to sort
:return: sorted list
"""
if not _list or len(_list) <= 1:
return _list[:]
arr = _list[:]
n = len(arr)
# Find min and max
min_val = max_val = arr[0]
for val in arr:
if val < min_val:
min_val = val
if val > max_val:
max_val = val
if min_val == max_val:
return arr
# Bingo sort main algorithm
bingo = min_val
next_bingo = max_val
largest_pos = n - 1
next_pos = 0
while bingo < next_bingo:
# Find next bingo value and place current bingo values
start_pos = next_pos
for i in range(start_pos, largest_pos + 1):
if arr[i] == bingo:
arr[i], arr[next_pos] = arr[next_pos], arr[i]
next_pos += 1
elif arr[i] < next_bingo:
next_bingo = arr[i]
bingo = next_bingo
next_bingo = max_val
return arr
def bingo_sort_with_duplicates(_list):
"""
Bingo sort that efficiently handles many duplicates
:param _list: list of values to sort
:return: sorted list
"""
if not _list or len(_list) <= 1:
return _list[:]
arr = _list[:]
n = len(arr)
# Count duplicates while finding min/max
value_count = {}
min_val = max_val = arr[0]
for val in arr:
value_count[val] = value_count.get(val, 0) + 1
if val < min_val:
min_val = val
if val > max_val:
max_val = val
# If only one unique value
if min_val == max_val:
return arr
# Reconstruct array using counts
result = []
current_val = min_val
while current_val <= max_val:
if current_val in value_count:
result.extend([current_val] * value_count[current_val])
# Find next value
next_val = max_val + 1
for val in value_count:
if val > current_val and val < next_val:
next_val = val
current_val = next_val
return result
def is_suitable_for_bingo_sort(_list):
"""
Check if the list is suitable for bingo sort (has many duplicates)
:param _list: list to check
:return: True if suitable, False otherwise
"""
if not _list or len(_list) <= 1:
return False
unique_count = len(set(_list))
total_count = len(_list)
# If less than 50% unique elements, bingo sort is beneficial
return unique_count / total_count < 0.5
def count_duplicates(_list):
"""
Count the number of duplicate elements in the list
:param _list: list to analyze
:return: dictionary with element counts
"""
if not _list:
return {}
counts = {}
for item in _list:
counts[item] = counts.get(item, 0) + 1
return counts
def bingo_sort_stable(_list):
"""
Stable version of bingo sort (maintains relative order of equal elements)
:param _list: list of values to sort
:return: sorted list maintaining stability
"""
if not _list or len(_list) <= 1:
return _list[:]
# Create list of (value, original_index) pairs
indexed_list = [(val, i) for i, val in enumerate(_list)]
# Sort by value, then by original index for stability
indexed_list.sort(key=lambda x: (x[0], x[1]))
# Extract just the values
return [val for val, _ in indexed_list]
def analyze_efficiency(_list):
"""
Analyze if bingo sort would be more efficient than other sorting algorithms
:param _list: list to analyze
:return: dictionary with analysis results
"""
if not _list:
return {"suitable": False, "reason": "Empty list"}
n = len(_list)
unique_count = len(set(_list))
duplicate_ratio = 1 - (unique_count / n)
analysis = {
"total_elements": n,
"unique_elements": unique_count,
"duplicate_ratio": duplicate_ratio,
"suitable": duplicate_ratio > 0.3,
"efficiency_gain": max(0, duplicate_ratio * 100)
}
if duplicate_ratio > 0.5:
analysis["recommendation"] = "Highly recommended - many duplicates"
elif duplicate_ratio > 0.3:
analysis["recommendation"] = "Recommended - moderate duplicates"
else:
analysis["recommendation"] = "Not recommended - few duplicates"
return analysis
def compare_with_other_sorts(_list):
"""
Compare bingo sort performance characteristics with other algorithms
:param _list: list to analyze
:return: performance comparison
"""
analysis = analyze_efficiency(_list)
n = len(_list) if _list else 0
comparison = {
"bingo_sort": {
"best_case": "O(n + k)" if analysis.get("suitable", False) else "O(n^2)",
"average_case": "O(n + k)" if analysis.get("suitable", False) else "O(n^2)",
"worst_case": "O(n^2)",
"space": "O(1)",
"stable": "No (unless using stable variant)"
},
"quick_sort": {
"best_case": "O(n log n)",
"average_case": "O(n log n)",
"worst_case": "O(n^2)",
"space": "O(log n)",
"stable": "No"
},
"merge_sort": {
"best_case": "O(n log n)",
"average_case": "O(n log n)",
"worst_case": "O(n log n)",
"space": "O(n)",
"stable": "Yes"
}
}
return comparison
def time_complexities():
"""
Return information on time complexity
:return: string
"""
return ("Best Case: O(n + k) where k is range of input, "
"Average Case: O(n + k) with many duplicates or O(n^2), "
"Worst Case: O(n^2)")
def get_code():
"""
Easily retrieve the source code of the sort function
:return: source code
"""
return inspect.getsource(sort)