Skip to content

Commit 40b9fc4

Browse files
authored
Merge branch 'master' into add-heap-sort
2 parents f60068d + 4cb0741 commit 40b9fc4

File tree

10 files changed

+174
-4
lines changed

10 files changed

+174
-4
lines changed

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-
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))

allalgorithms/string/is_unique.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Check if a string has all unique characters.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: José E. Andrade Jr.
7+
# Github: @andradejunior
8+
#
9+
10+
def is_unique(string_to_check):
11+
character_set = set()
12+
for character in string_to_check:
13+
if character in character_set:
14+
return False
15+
character_set.add(character)
16+
return True

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+

docs/string/is-unique.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Is Unique
2+
3+
This algorithms checks if a string has all unique characters in O(n) time.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.string import is_unique
15+
16+
str = "abcdefg"
17+
18+
print(is_unique(str)
19+
# -> True
20+
21+
str = "test"
22+
23+
print(is_unique(str)
24+
# -> False
25+
```
26+
27+
## API
28+
29+
### is_unique(string)
30+
31+
> Return True if string has all unique characters, False otherwise
32+
33+
##### Params:
34+
35+
- `string`: Input String
36+

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
cocktail_shaker_sort,
1111
tree_sort,
1212
heap_sort
13+
shell_sort,
1314
)
1415

1516

@@ -43,6 +44,8 @@ def test_heap_sort(self):
4344
heap_sort(array)
4445
self.assertEqual([-44, 1, 2, 3, 7, 19], array)
4546

47+
def test_shell_sort(self):
48+
self.assertEqual([-44, 1, 2, 3, 7, 19], shell_sort([7, 3, 2, 19, -44, 1]))
4649

4750
if __name__ == "__main__":
4851
unittest.main()

tests/test_string.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from allalgorithms.string import palindrome_check
3+
from allalgorithms.string import palindrome_check, is_unique
44

55

66
class TestSorting(unittest.TestCase):
@@ -13,5 +13,14 @@ def test_palindrome_check(self):
1313
self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?"))
1414
self.assertEqual(False, palindrome_check("How are you?"))
1515

16+
def test_is_unique(self):
17+
self.assertEqual(True, is_unique("abcdefg"))
18+
self.assertEqual(True, is_unique("1234567"))
19+
self.assertEqual(True, is_unique("algorithms"))
20+
self.assertEqual(False, is_unique("abcdefa"))
21+
self.assertEqual(False, is_unique("abddefg"))
22+
self.assertEqual(False, is_unique("12345567"))
23+
24+
1625
if __name__ == "__main__":
1726
unittest.main()

0 commit comments

Comments
 (0)