Movatterモバイル変換


[0]ホーム

URL:


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

Commit0217d88

Browse files
authored
Merge pull request#9 from martmists/master
Add Bubble, Insertion and Selection sort
2 parents9896eb0 +fd4c021 commit0217d88

File tree

11 files changed

+179
-10
lines changed

11 files changed

+179
-10
lines changed

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# PyCharm
107+
.idea/

‎allalgorithms/sorting/__init__.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from .merge_sortimport*
1+
from .merge_sortimportmerge_sort
2+
from .insertion_sortimportinsertion_sort
3+
from .selection_sortimportselection_sort
4+
from .bubble_sortimportbubble_sort
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Bubble Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
defbubble_sort(seq):
12+
foriinrange(len(seq)):
13+
forjinrange(len(seq)-i-1):
14+
ifseq[j]>seq[j+1]:
15+
# Swap if both are not in order
16+
seq[j],seq[j+1]=seq[j+1],seq[j]
17+
18+
returnseq
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Insertion Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
definsertion_sort(arr):
12+
iflen(arr)==1:
13+
returnarr
14+
15+
foriinrange(1,len(arr)):
16+
j=i-1
17+
whilej>=0andarr[j]>arr[i]:
18+
j-=1
19+
arr.insert(j+1,arr.pop(i))
20+
21+
returnarr

‎allalgorithms/sorting/merge_sort.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Contributed by: Carlos Abraham Hernandez
77
# Github: @abranhe
88
#
9+
10+
911
defmerge_sort(arr):
1012
iflen(arr)==1:
1113
returnarr
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Bubble Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
defselection_sort(seq):
12+
foriinrange(len(seq)):
13+
min_idx=i
14+
forjinrange(min_idx,len(seq)):
15+
ifseq[j]<seq[min_idx]:
16+
min_idx=j
17+
18+
seq[i],seq[min_idx]=seq[min_idx],seq[i]
19+
returnseq

‎docs/sorting/bubble-sort.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Bubble Sort
2+
3+
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. Bubble sort can be practical if the input is in mostly sorted order with some out-of-order elements nearly in position.
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```py
14+
from allalgorithms.sortingimport bubble_sort
15+
16+
arr= [77,2,10,-2,1,7]
17+
18+
print(bubble_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
##API
23+
24+
###bubble_sort(array)
25+
26+
>Returns a sorted array
27+
28+
#####Params:
29+
30+
-`array`: Unsorted Array

‎docs/sorting/insertion-sort.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Insertion Sort
2+
3+
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```py
14+
from allalgorithms.sortingimport insertion_sort
15+
16+
arr= [77,2,10,-2,1,7]
17+
18+
print(insertion_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
##API
23+
24+
###insertion_sort(array)
25+
26+
>Returns a sorted array
27+
28+
#####Params:
29+
30+
-`array`: Unsorted Array

‎docs/sorting/selection-sort.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Selection Sort
2+
3+
In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n^2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```py
14+
from allalgorithms.sortingimport selection_sort
15+
16+
arr= [77,2,10,-2,1,7]
17+
18+
print(selection_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
##API
23+
24+
###selection_sort(array)
25+
26+
>Returns a sorted array
27+
28+
#####Params:
29+
30+
-`array`: Unsorted Array

‎tests/test_searches.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
fromallalgorithms.searchesimport (
2-
binary_search
3-
)
4-
51
importunittest
62

3+
fromallalgorithms.searchesimportbinary_search
4+
5+
76
classTestSearches(unittest.TestCase):
87

98
deftest_binary_search(self):
@@ -13,5 +12,6 @@ def test_binary_search(self):
1312
self.assertEqual(None,binary_search(arr,8))
1413
self.assertEqual(None,binary_search(arr,-1))
1514

15+
1616
if__name__=='__main__':
1717
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp