Skip to content

Commit c7e9fda

Browse files
authored
Merge branch 'master' into master
2 parents 4846796 + c1c0c94 commit c7e9fda

File tree

9 files changed

+181
-4
lines changed

9 files changed

+181
-4
lines changed

allalgorithms/sorting/quicksort.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Quick Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Brian D. Hopper
7+
# Github: @bubbabeans
8+
#
9+
def partition(xs, start, end):
10+
follower = leader = start
11+
while leader < end:
12+
if xs[leader] <= xs[end]:
13+
xs[follower], xs[leader] = xs[leader], xs[follower]
14+
follower += 1
15+
leader += 1
16+
xs[follower], xs[end] = xs[end], xs[follower]
17+
return follower
18+
19+
def _quicksort(xs, start, end):
20+
if start >= end:
21+
return
22+
p = partition(xs, start, end)
23+
_quicksort(xs, start, p-1)
24+
_quicksort(xs, p+1, end)
25+
26+
def quicksort(xs):
27+
_quicksort(xs, 0, len(xs)-1)
28+
29+
# To use: create a list and send it to quicksort: quicksort(list placed here)

allalgorithms/sorting/shell_sort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Shell Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Elias
7+
# Github: @eliasbayona
8+
#
9+
10+
def shell_sort(arr):
11+
n = len(arr)
12+
h = int(n/2)
13+
14+
15+
while h > 0:
16+
for i in range(h,n):
17+
18+
temp = arr[i]
19+
j = i
20+
21+
while j >= h and arr[j-h] >temp:
22+
arr[j] = arr[j-h]
23+
j -= h
24+
25+
arr[j] = temp
26+
27+
h = int(h/2)
28+
29+
return arr
30+

allalgorithms/string/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .palindrome_check import *
2-
from .is_unique import *
2+
from .is_unique import *
3+
from .hamming_dist import *

allalgorithms/string/hamming_dist.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Binary search works for a sorted array.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: ninexball
7+
# Github: @ninexball
8+
#
9+
10+
def hamming_dist(seq1: str, seq2: str) -> int:
11+
"""Compare hamming distance of two strings"""
12+
if len(seq1) != len(seq2):
13+
raise ValueError("length of strings are not the same")
14+
return sum(c1 != c2 for c1, c2 in zip(seq1, seq2))

docs/sorting/quicksort.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Quicksort
2+
3+
A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point.
4+
5+
A pivot point is created in the middle of an array, and all larger items go after the pivot point, and smaller items are placed in front
6+
of the pivot point.
7+
8+
The pivot point is then moved to the middle of either the smaller or larger items, and the sort is run again on that half.
9+
10+
This continues over and over again until everything is in the proper place.
11+
12+
## Install
13+
14+
```
15+
pip install allalgorithms
16+
```
17+
18+
## Usage
19+
20+
```py
21+
from allalgorithms.sorting import quicksort
22+
23+
arr = [77, 2, 10, -2, 1, 7]
24+
25+
print(quicksort(arr))
26+
# -> [-2, 1, 2, 7, 10, 77]
27+
```
28+
29+
## API
30+
31+
```
32+
quicksort(array)
33+
```
34+
35+
> Returns a sorted array
36+
37+
##### Params:
38+
39+
- `array`: Sorted Array

docs/sorting/shell-sort.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Selection Sort
2+
3+
In computer science, shell sort improves upon insertion sort by moving out of order elements more than one position at a time. It has a best case O(n log n) time complexity, and for other cases, it depends on the gap sequence. According to Poonen Theorem, worst case complexity for shell sort is Θ(NlogN)^2/(log logN)^2) or Θ(NlogN)^2/log logN) or Θ(N(logN)^2) or something in between.
4+
## Install
5+
6+
```
7+
pip install allalgorithms
8+
```
9+
10+
## Usage
11+
12+
```py
13+
from allalgorithms.sorting import shell_sort
14+
15+
arr = [77, 2, 10, -2, 1, 7]
16+
17+
print(shell_sort(arr))
18+
# -> [-2, 1, 2, 7, 10, 77]
19+
```
20+
21+
## API
22+
23+
### selection_sort(array)
24+
25+
> Returns a sorted array
26+
27+
##### Params:
28+
29+
- `array`: Unsorted Array

docs/string/hamming-dist.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Hamming Distance
2+
3+
In informatics, Hamming distance is the number of positions where the characters differ between two strings of equal length
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```
14+
>>> from allalgorithms.sorting import hamming_dist
15+
16+
>>> hamming_dist("hello world", "hello wario")
17+
3
18+
```
19+
20+
## API
21+
22+
### hamming_dist(seq1, seq2)
23+
24+
> Returns an integer
25+
26+
> Raises a ValueError if strings are of unequal length
27+
28+
##### Params:
29+
30+
- `seq1`: first string to compare
31+
- `seq2`: second string to compare
32+

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ print(binary_search(arr, 3))
6161
- [Binary Search](https://python.allalgorithms.com/searches/binary-search)
6262
- [Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search)
6363
- [Jump Search](https://python.allalgorithms.com/searches/jump-search)
64-
64+
6565
- ### Sorting
6666
- [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort)
6767
- [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort)
@@ -75,7 +75,7 @@ print(binary_search(arr, 3))
7575

7676
# Related
7777

78-
- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library
78+
- [allalgorithms-js](https://github.com/abranhe/allalgorithms-js): All ▲lgorithms Javascript library
7979

8080
# Maintainers
8181

tests/test_sorting.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
pigeonhole_sort,
99
stooge_sort,
1010
cocktail_shaker_sort,
11-
tree_sort
11+
tree_sort,
12+
shell_sort,
1213
)
1314

1415

@@ -37,6 +38,8 @@ def test_cocktail_shaker_sort(self):
3738
def tree_sort(self):
3839
self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1]))
3940

41+
def test_shell_sort(self):
42+
self.assertEqual([-44, 1, 2, 3, 7, 19], shell_sort([7, 3, 2, 19, -44, 1]))
4043

4144
if __name__ == "__main__":
4245
unittest.main()

0 commit comments

Comments
 (0)