11# Competitive Programming Algorithm Library in Python
22
3- competitivepython is a library of algorithms and data structures implemented in Python. It is designed to be a useful resource for developers who need to implement common algorithms and data structures in their projects .
3+ competitivepython is an open-source library of algorithms and data structures implemented in Python. It offers a collection of frequently used algorithms and data structures that can be directly used in any Python-based project .
44
55## Features
66
7- - Implements a wide range of algorithms and data structures, including :
7+ - Provides implementations for several common algorithms and data structures such as :
88 - Searches: Binary Search, Linear Search, KMP Pattern Search
99 - Graphs: BFS, DFS, Dijkstra
1010 - Sorting: Bubble Sort, Insertion Sort, Shell Sort, Selection Sort, Bucket Sort, Merge Sort, Tim Sort, Quick Sort, Heap Sort, Radix Sort
1111 - Trees: Binary Search Tree
12- - Easy to use and understand, with well-documented code
13- - Portable and compatible with Python 3
12+ - Codebase is easy to use, well-documented, and compatible with Python 3.
1413- Open source and available under the MIT license
1514
1615## Installation
@@ -23,78 +22,259 @@ To install competitivepython library, simply run the following command:
2322
2423## Usage
2524
26- To use PyPy in your project, simply import the desired algorithm or data structure and use it as needed. For example:
25+ To use competitivepython in your project, import the desired algorithm or data structure and use it as needed. Below are some example use cases :
2726
28- - searches implementation example
27+ - Implementing searches:
28+ - Binary Search
2929 ```
3030 from competitivepython import searches
31-
32- result = searches.binary_search([1, 2, 3, 4, 5], 3)
33- result2 = searches.linear_search([5, 7, 9, 2, 4, 10], 4)
31+
32+ arr = [1, 2, 3, 4, 5]
33+ target = 3
34+
35+ result = searches.binary_search(arr, target)
36+
37+ print("Binary Search:",result)
38+
39+ '''Output:
40+ Binary Search: 2
41+ '''
42+ ```
43+ - Linear Search
44+ ```
45+ from competitivepython import searches
46+
47+ arr = [5, 7, 9, 2, 4, 10]
48+ target = 4
49+
50+ result = searches.linear_search(arr, target)
51+
52+ print("Linear Search:",result)
53+
54+ '''Output:
55+ Linear Search: 4
56+ '''
57+ ```
58+ - Knuth–Morris–Pratt string Search
59+ ```
60+ from competitivepython import searches
61+
3462 txt = "ABABDABACDABABCABAB"
3563 pat = "ABABCABAB"
36- result3 = searches.kmp_search(pat,txt)
37- print(result) # Output: 2
38- print(result2) # Output: 4
39- print(result3) # Output: [10]
64+
65+ result = searches.kmp_search(pat,txt)
66+
67+ print("KMP Search:",result)
68+
69+ '''Output:
70+ KMP Search: [10]
71+ '''
72+ ```
73+
74+ - Implementing sorting:
75+ - Bubble Sort
4076 ```
77+ from competitivepython import sorting
4178
42- - sorting implementation example
79+ arr = [112, 6, 7, 12, 15]
80+
81+ result = sorting.bubble_sort(arr)
82+ print('bubble sort:', result)
83+
84+ ''' Output ---
85+ bubble sort: [6, 7, 12, 15, 112]
86+ '''
87+ ```
88+ - Bucket Sort
89+ ```
90+ from competitivepython import sorting
91+
92+ arr = [112, 6, 7, 12, 15]
93+
94+ result = sorting.bucket_sort(arr)
95+ print('bucket sort:', result)
96+
97+ ''' Output ---
98+ bucket sort: [6, 7, 12, 15, 112]
99+ '''
100+ ```
101+ - Heap Sort
43102 ```
44103 from competitivepython import sorting
45104
46105 arr = [112, 6, 7, 12, 15]
106+
107+ result = sorting.heap_sort(arr)
108+
109+ print('heap sort:', result)
47110
48- res = sorting.bubble_sort(arr)
49- res1 = sorting.bucket_sort(arr)
50- res2 = sorting.heap_sort(arr)
51- res3 = sorting.insertion_sort(arr)
52- res4 = sorting.merge_sort(arr)
53- res5 = sorting.quick_sort(arr)
54- res6 = sorting.radix_sort(arr)
55- res7 = sorting.selection_sort(arr)
56- res8 = sorting.shell_sort(arr)
57- res9 = sorting.tim_sort(arr)
111+ ''' Output ---
112+ heap sort: [6, 7, 12, 15, 112]
113+ '''
114+ ```
115+ - Insertion Sort
116+ ```
117+ from competitivepython import sorting
58118
59- print('bubble sort:', res, 'bucket sort:', res1, 'heap sort:', res2, 'insertion sort:', res3, 'merge sort:', res4,
60- 'quick sort:', res5, 'radix sort:', res6, 'selection sort:', res7, 'shell sort:', res8, 'tim sort:', res9)
119+ arr = [112, 6, 7, 12, 15]
120+
121+ result = sorting.insertion_sort(arr)
122+
123+ print('insertion sort:', result)
124+
125+ ''' Output ---
126+ insertion sort: [6, 7, 12, 15, 112]
127+ '''
128+ ```
129+ - Merge Sort
130+ ```
131+ from competitivepython import sorting
132+
133+ arr = [112, 6, 7, 12, 15]
134+
135+ result = sorting.merge_sort(arr)
136+
137+ print('merge sort:', result)
61138
62139 ''' Output ---
63- bubble sort: [6, 7, 12, 15, 112] bucket sort: [6, 7, 12, 15, 112] heap sort: [6, 7, 12, 15, 112]
64- insertion sort: [6, 7, 12, 15, 112] merge sort: [6, 7, 12, 15, 112] quick sort: [6, 7, 12, 15, 112]
65- radix sort: [6, 7, 12, # 15, 112] selection sort: [6, 7, 12, 15, 112] shell sort: [6, 7, 12, 15, 112]
66- tim sort: [6, 7, 12, 15, 112]
140+ merge sort: [6, 7, 12, 15, 112]
141+ '''
142+ ```
143+ - Quick Sort
144+ ```
145+ from competitivepython import sorting
146+
147+ arr = [112, 6, 7, 12, 15]
148+
149+ result = sorting.quick_sort(arr)
150+
151+ print('quick sort:', result)
152+
153+ ''' Output ---
154+ quick sort: [6, 7, 12, 15, 112]
67155 '''
68156 ```
157+ - Radix Sort
158+ ```
159+ from competitivepython import sorting
160+
161+ arr = [112, 6, 7, 12, 15]
162+
163+ result = sorting.radix_sort(arr)
164+
165+ print('radix sort:', result)
69166
70- - graphs implementation example
167+ ''' Output ---
168+ radix sort: [6, 7, 12, # 15, 112]
169+ '''
170+ ```
171+ - Selection Sort
71172 ```
72- from competitivepython import graphs
173+ from competitivepython import sorting
73174
74- graph = {
75- 'A': {'B': 1, 'C': 4},
76- 'B': {'A': 1, 'C': 2, 'D': 5},
77- 'C': {'A': 4, 'B': 2, 'D': 1},
78- 'D': {'B': 5, 'C': 1},
79- }
80- start = 'A'
81- end = 'D'
175+ arr = [112, 6, 7, 12, 15]
176+
177+ result = sorting.selection_sort(arr)
178+
179+ print('selection sort:', result)
82180
83- result = graphs.breadth_first_search(graph, 'C')
84- result2 = graphs.depth_first_search(graph, 'C')
85- result3 = graphs.dijkstra(graph, start, end)
86- print("bfs:",result)
87- print("dfs:",result2)
88- print("dijikstra:",result3)
181+ ''' Output ---
182+ selection sort: [6, 7, 12, 15, 112]
183+ '''
184+ ```
185+ - Shell Sort
186+ ```
187+ from competitivepython import sorting
188+
189+ arr = [112, 6, 7, 12, 15]
190+
191+ result = sorting.shell_sort(arr)
192+
193+ print('shell sort:', result)
89194
90- ''' Output--
91- bfs: {'B', 'D', 'C', 'A'}
92- dfs: {'B', 'D', 'C', 'A'}
93- dijikstra: {'distance': 4, 'path': ['B', 'C', 'D']}
195+ ''' Output ---
196+ shell sort: [6, 7, 12, 15, 112]
94197 '''
95198 ```
199+ - Tim Sort
200+ ```
201+ from competitivepython import sorting
202+
203+ arr = [112, 6, 7, 12, 15]
204+
205+ result = sorting.tim_sort(arr)
206+
207+ print('tim sort:', result)
208+
209+ ''' Output ---
210+ tim sort: [6, 7, 12, 15, 112]
211+ '''
212+ ```
213+
214+ - Implementing graphs:
215+ - Breadth First Search (or Breadth First Traversal)
216+ ```
217+ from competitivepython import graphs
218+
219+ graph = {
220+ 'A': {'B': 1, 'C': 4},
221+ 'B': {'A': 1, 'C': 2, 'D': 5},
222+ 'C': {'A': 4, 'B': 2, 'D': 1},
223+ 'D': {'B': 5, 'C': 1},
224+ }
225+ start = 'A'
226+ end = 'D'
227+
228+ result = graphs.breadth_first_search(graph, 'C')
229+
230+ print("bfs:",result)
231+
232+ ''' Output--
233+ bfs: {'B', 'D', 'C', 'A'}
234+ '''
235+ ```
236+ - Depth First Search(or Depth First Traversal)
237+ ```
238+ from competitivepython import graphs
239+
240+ graph = {
241+ 'A': {'B': 1, 'C': 4},
242+ 'B': {'A': 1, 'C': 2, 'D': 5},
243+ 'C': {'A': 4, 'B': 2, 'D': 1},
244+ 'D': {'B': 5, 'C': 1},
245+ }
246+ start = 'A'
247+ end = 'D'
248+
249+ result = graphs.depth_first_search(graph, 'C')
250+ print("dfs:",result)
251+
252+ ''' Output--
253+ dfs: {'B', 'D', 'C', 'A'}
254+ '''
255+ ```
256+ - Dijkstra’s Shortest Path
257+ ```
258+ from competitivepython import graphs
259+
260+ graph = {
261+ 'A': {'B': 1, 'C': 4},
262+ 'B': {'A': 1, 'C': 2, 'D': 5},
263+ 'C': {'A': 4, 'B': 2, 'D': 1},
264+ 'D': {'B': 5, 'C': 1},
265+ }
266+ start = 'A'
267+ end = 'D'
268+
269+ result = graphs.dijkstra(graph, start, end)
270+ print("dijikstra:",result)
271+
272+ ''' Output--
273+ dijikstra: {'distance': 4, 'path': ['B', 'C', 'D']}
274+ '''
275+ ```
96276
97- - trees implementation example
277+ - Implementing trees:
98278
99279 ```
100280 from competitivepython import trees
0 commit comments