Skip to content

Commit 23d4e85

Browse files
authored
Added exchange sort (keon#816)
* add_exchange_sort
1 parent 89be41b commit 23d4e85

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ If you want to uninstall algorithms, it is as simple as:
283283
- [comb_sort](algorithms/sort/comb_sort.py)
284284
- [counting_sort](algorithms/sort/counting_sort.py)
285285
- [cycle_sort](algorithms/sort/cycle_sort.py)
286+
- [exchange_sort](algorithms/sort/exchange_sort.py)
286287
- [gnome_sort](algorithms/sort/gnome_sort.py)
287288
- [heap_sort](algorithms/sort/heap_sort.py)
288289
- [insertion_sort](algorithms/sort/insertion_sort.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .comb_sort import *
55
from .counting_sort import *
66
from .cycle_sort import *
7+
from .exchange_sort import *
78
from .heap_sort import *
89
from .insertion_sort import *
910
from .merge_sort import *

algorithms/sort/exchange_sort.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def exchange_sort(arr):
2+
"""
3+
Reference : https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort
4+
Complexity : O(n^2)
5+
"""
6+
arr_len = len(arr)
7+
for i in range(arr_len-1):
8+
for j in range(i+1, arr_len):
9+
if(arr[i] > arr[j]):
10+
arr[i], arr[j] = arr[j], arr[i]
11+
return arr

tests/test_sort.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
comb_sort,
66
counting_sort,
77
cycle_sort,
8+
exchange_sort,
89
max_heap_sort, min_heap_sort,
910
insertion_sort,
1011
merge_sort,
@@ -55,6 +56,9 @@ def test_counting_sort(self):
5556
def test_cycle_sort(self):
5657
self.assertTrue(is_sorted(cycle_sort([1, 3, 2, 5, 65, 23, 57, 1232])))
5758

59+
def test_exchange_sort(self):
60+
self.assertTrue(is_sorted(exchange_sort([1, 3, 2, 5, 65, 23, 57, 1232])))
61+
5862
def test_heap_sort(self):
5963
self.assertTrue(is_sorted(max_heap_sort([1, 3, 2, 5, 65, 23, 57, 1232])))
6064

0 commit comments

Comments
 (0)