Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4092ff4

Browse files
2 parents 293a5ab + 1515509 commit 4092ff4

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
## BUBBLE SORT - SORTING ALGORITHMS
22

33
- It sorts two consecutive values ​​in a list and continues this process until the whole list becomes sequential.
4-
<img src="https://www.productplan.com/uploads/bubble-sort-1024x683-2.png" />
4+
5+
```python
6+
def bubbleSortAlgorithm(arr):
7+
# for every element (arranged backwards)
8+
for n in range(len(arr)-1, 0, -1): # [20,19,18,17 .... 0]
9+
for k in range(n):
10+
if arr[k] > arr[k+1]:
11+
temp = arr[k]
12+
arr[k] = arr[k+1]
13+
arr[k+1] = temp
14+
15+
return arr
16+
17+
arr = [3,2,13,4,6,5,7,8,20]
18+
print("Output: ", bubbleSortAlgorithm(arr))
19+
```
20+
output:
21+
```
22+
[2, 3, 4, 5, 6, 7, 8, 13, 20]
23+
```
24+
25+
<img src="https://www.productplan.com/uploads/bubble-sort-1024x683-2.png" />

‎Sorting Algorithms/Insertion Sort/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,28 @@
44
- The first value is a sublist element and is sequential because it already has a single value.
55
- The remaining values ​​in this sub list are added by comparison.
66

7-
<img src="https://miro.medium.com/max/3204/1*5t5q_OLP-kGwQyblAN-nog.png" />
7+
```python
8+
def insertion_sort(arr):
9+
# for every index in array
10+
for i in range(1, len(arr)):
11+
# set current values and position
12+
currentvalue = arr[i]
13+
position = i
14+
15+
# sorted sublist
16+
while position > 0 and arr[position-1]>currentvalue:
17+
arr[position] = arr[position-1]
18+
position = position - 1
19+
20+
arr[position] = currentvalue
21+
return arr
22+
23+
arr = [3,2,13,4,6,5,7,8,1,20]
24+
print("Output: ", insertion_sort(arr))
25+
```
26+
output:
27+
```
28+
[1, 2, 3, 4, 5, 6, 7, 8, 13, 20]
29+
```
30+
31+
<img src="https://miro.medium.com/max/3204/1*5t5q_OLP-kGwQyblAN-nog.png" />

‎Sorting Algorithms/Merge Sort/README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,49 @@
33
- Merge sort is a recursive algorithm.
44
- It starts to divide an array until it divides it into sub-arrays with dimensions of 2.
55
- Then combine by starting the order from the smallest subarray.
6-
<img src="https://www.101computing.net/wp/wp-content/uploads/Merge-Sort-Algorithm.png" />
6+
7+
```python
8+
def merge_sort(arr):
9+
if len(arr) > 1:
10+
mid = int(len(arr)/2)
11+
left_half = arr[:mid]
12+
right_half = arr[mid:]
13+
14+
merge_sort(left_half)
15+
merge_sort(right_half)
16+
17+
i = 0 # for left half
18+
j = 0 # for right half
19+
k = 0 # all list
20+
21+
while i < len(left_half) and j < len(right_half):
22+
if left_half[i] < right_half[j]:
23+
arr[k] = left_half[i]
24+
i += 1
25+
else:
26+
arr[k] = right_half[j]
27+
j += 1
28+
k = k + 1
29+
30+
while i < len(left_half):
31+
arr[k] = left_half[i]
32+
i += 1
33+
k += 1
34+
35+
while j < len(right_half):
36+
arr[k] = right_half[j]
37+
j += 1
38+
k += 1
39+
40+
return arr
41+
42+
43+
arr = [3,2,13,4,6,5,7,8,1,20]
44+
print("Output: ", merge_sort(arr) )
45+
```
46+
output:
47+
```
48+
[1, 2, 3, 4, 5, 6, 7, 8, 13, 20]
49+
```
50+
51+
<img src="https://www.101computing.net/wp/wp-content/uploads/Merge-Sort-Algorithm.png" />

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /