mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@@ -8,7 +8,6 @@ For manual testing run:
|
||||
python bogo_sort.py
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
import random
|
||||
|
||||
|
||||
@@ -39,11 +38,6 @@ def bogo_sort(collection):
|
||||
return collection
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(bogo_sort(unsorted))
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def bubble_sort(collection):
|
||||
"""Pure implementation of bubble sort algorithm in Python
|
||||
|
||||
@@ -17,9 +14,12 @@ def bubble_sort(collection):
|
||||
|
||||
>>> bubble_sort([-2, -5, -45])
|
||||
[-45, -5, -2]
|
||||
|
||||
>>> bubble_sort([-23,0,6,-4,34])
|
||||
|
||||
>>> bubble_sort([-23, 0, 6, -4, 34])
|
||||
[-23, -4, 0, 6, 34]
|
||||
|
||||
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
|
||||
True
|
||||
"""
|
||||
length = len(collection)
|
||||
for i in range(length-1):
|
||||
@@ -28,15 +28,12 @@ def bubble_sort(collection):
|
||||
if collection[j] > collection[j+1]:
|
||||
swapped = True
|
||||
collection[j], collection[j+1] = collection[j+1], collection[j]
|
||||
if not swapped: break # Stop iteration if the collection is sorted.
|
||||
if not swapped:
|
||||
break # Stop iteration if the collection is sorted.
|
||||
return collection
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
user_input = raw_input('Enter numbers separated by a comma:').strip()
|
||||
user_input = input('Enter numbers separated by a comma:').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(*bubble_sort(unsorted), sep=',')
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
from __future__ import print_function
|
||||
|
||||
def cocktail_shaker_sort(unsorted):
|
||||
"""
|
||||
Pure implementation of the cocktail shaker sort algorithm in Python.
|
||||
"""
|
||||
for i in range(len(unsorted)-1, 0, -1):
|
||||
swapped = False
|
||||
|
||||
|
||||
for j in range(i, 0, -1):
|
||||
if unsorted[j] < unsorted[j-1]:
|
||||
unsorted[j], unsorted[j-1] = unsorted[j-1], unsorted[j]
|
||||
@@ -16,17 +14,12 @@ def cocktail_shaker_sort(unsorted):
|
||||
if unsorted[j] > unsorted[j+1]:
|
||||
unsorted[j], unsorted[j+1] = unsorted[j+1], unsorted[j]
|
||||
swapped = True
|
||||
|
||||
|
||||
if not swapped:
|
||||
return unsorted
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
cocktail_shaker_sort(unsorted)
|
||||
print(unsorted)
|
||||
|
||||
@@ -48,11 +48,6 @@ def comb_sort(data):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(comb_sort(unsorted))
|
||||
|
||||
@@ -8,8 +8,6 @@ For manual testing run:
|
||||
python counting_sort.py
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def counting_sort(collection):
|
||||
"""Pure implementation of counting sort algorithm in Python
|
||||
@@ -58,6 +56,10 @@ def counting_sort(collection):
|
||||
return ordered
|
||||
|
||||
def counting_sort_string(string):
|
||||
"""
|
||||
>>> counting_sort_string("thisisthestring")
|
||||
'eghhiiinrsssttt'
|
||||
"""
|
||||
return ''.join([chr(i) for i in counting_sort([ord(c) for c in string])])
|
||||
|
||||
|
||||
@@ -65,11 +67,6 @@ if __name__ == '__main__':
|
||||
# Test string sort
|
||||
assert "eghhiiinrsssttt" == counting_sort_string("thisisthestring")
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(counting_sort(unsorted))
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
# Code contributed by Honey Sharma
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def cycle_sort(array):
|
||||
ans = 0
|
||||
|
||||
@@ -45,12 +42,7 @@ def cycle_sort(array):
|
||||
|
||||
# Main Code starts here
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n')
|
||||
user_input = input('Enter numbers separated by a comma:\n')
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
n = len(unsorted)
|
||||
cycle_sort(unsorted)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
"""Gnome Sort Algorithm."""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def gnome_sort(unsorted):
|
||||
"""Pure implementation of the gnome sort algorithm in Python."""
|
||||
@@ -21,12 +19,7 @@ def gnome_sort(unsorted):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
gnome_sort(unsorted)
|
||||
print(unsorted)
|
||||
|
||||
@@ -10,9 +10,6 @@ For manual testing run:
|
||||
python heap_sort.py
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def heapify(unsorted, index, heap_size):
|
||||
largest = index
|
||||
left_index = 2 * index + 1
|
||||
@@ -54,11 +51,6 @@ def heap_sort(unsorted):
|
||||
return unsorted
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(heap_sort(unsorted))
|
||||
|
||||
@@ -9,9 +9,6 @@ python3 -m doctest -v insertion_sort.py
|
||||
For manual testing run:
|
||||
python insertion_sort.py
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def insertion_sort(collection):
|
||||
"""Pure implementation of the insertion sort algorithm in Python
|
||||
|
||||
@@ -40,11 +37,6 @@ def insertion_sort(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(insertion_sort(unsorted))
|
||||
|
||||
@@ -9,9 +9,6 @@ python3 -m doctest -v merge_sort.py
|
||||
For manual testing run:
|
||||
python merge_sort.py
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def merge_sort(collection):
|
||||
"""Pure implementation of the merge sort algorithm in Python
|
||||
|
||||
@@ -46,11 +43,6 @@ def merge_sort(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(*merge_sort(unsorted), sep=',')
|
||||
|
||||
@@ -4,9 +4,6 @@ Takes an average of 0.6 microseconds to sort a list of length 1000 items.
|
||||
Best Case Scenario : O(n)
|
||||
Worst Case Scenario : O(n^2) because native python functions:min, max and remove are already O(n)
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def merge_sort(collection):
|
||||
"""Pure implementation of the fastest merge sort algorithm in Python
|
||||
|
||||
@@ -36,11 +33,6 @@ def merge_sort(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(*merge_sort(unsorted), sep=',')
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
'''
|
||||
This is an implementation of Pigeon Hole Sort.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
def pigeon_sort(array):
|
||||
# Manually finds the minimum and maximum of the array.
|
||||
min = array[0]
|
||||
@@ -38,12 +35,7 @@ def pigeon_sort(array):
|
||||
return array
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by comma:\n')
|
||||
user_input = input('Enter numbers separated by comma:\n')
|
||||
unsorted = [int(x) for x in user_input.split(',')]
|
||||
sorted = pigeon_sort(unsorted)
|
||||
|
||||
|
||||
@@ -9,9 +9,6 @@ python3 -m doctest -v quick_sort.py
|
||||
For manual testing run:
|
||||
python quick_sort.py
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def quick_sort(collection):
|
||||
"""Pure implementation of quick sort algorithm in Python
|
||||
|
||||
@@ -47,11 +44,6 @@ def quick_sort(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [ int(item) for item in user_input.split(',') ]
|
||||
print( quick_sort(unsorted) )
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
def quick_sort_3partition(sorting, left, right):
|
||||
if right <= left:
|
||||
return
|
||||
@@ -20,12 +18,7 @@ def quick_sort_3partition(sorting, left, right):
|
||||
quick_sort_3partition(sorting, b + 1, right)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [ int(item) for item in user_input.split(',') ]
|
||||
quick_sort_3partition(unsorted,0,len(unsorted)-1)
|
||||
print(unsorted)
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
from __future__ import print_function
|
||||
from random import randint
|
||||
from tempfile import TemporaryFile
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
||||
def _inPlaceQuickSort(A,start,end):
|
||||
def _inPlaceQuickSort(A,start,end):
|
||||
count = 0
|
||||
if start<end:
|
||||
pivot=randint(start,end)
|
||||
temp=A[end]
|
||||
A[end]=A[pivot]
|
||||
A[pivot]=temp
|
||||
|
||||
|
||||
p,count= _inPlacePartition(A,start,end)
|
||||
count += _inPlaceQuickSort(A,start,p-1)
|
||||
count += _inPlaceQuickSort(A,p+1,end)
|
||||
return count
|
||||
|
||||
def _inPlacePartition(A,start,end):
|
||||
|
||||
|
||||
count = 0
|
||||
pivot= randint(start,end)
|
||||
temp=A[end]
|
||||
@@ -27,20 +25,20 @@ def _inPlacePartition(A,start,end):
|
||||
A[pivot]=temp
|
||||
newPivotIndex=start-1
|
||||
for index in range(start,end):
|
||||
|
||||
|
||||
count += 1
|
||||
if A[index]<A[end]:#check if current val is less than pivot value
|
||||
newPivotIndex=newPivotIndex+1
|
||||
temp=A[newPivotIndex]
|
||||
A[newPivotIndex]=A[index]
|
||||
A[index]=temp
|
||||
|
||||
|
||||
temp=A[newPivotIndex+1]
|
||||
A[newPivotIndex+1]=A[end]
|
||||
A[end]=temp
|
||||
return newPivotIndex+1,count
|
||||
|
||||
outfile = TemporaryFile()
|
||||
|
||||
outfile = TemporaryFile()
|
||||
p = 100 # 1000 elements are to be sorted
|
||||
|
||||
|
||||
@@ -52,15 +50,15 @@ np.save(outfile, X)
|
||||
print('The array is')
|
||||
print(X)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
outfile.seek(0) # using the same array
|
||||
M = np.load(outfile)
|
||||
r = (len(M)-1)
|
||||
z = _inPlaceQuickSort(M,0,r)
|
||||
z = _inPlaceQuickSort(M,0,r)
|
||||
|
||||
print("No of Comparisons for 100 elements selected from a standard normal distribution is :")
|
||||
print(z)
|
||||
|
||||
@@ -9,9 +9,6 @@ python3 -m doctest -v selection_sort.py
|
||||
For manual testing run:
|
||||
python selection_sort.py
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def selection_sort(collection):
|
||||
"""Pure implementation of the selection sort algorithm in Python
|
||||
:param collection: some mutable ordered collection with heterogeneous
|
||||
@@ -43,11 +40,6 @@ def selection_sort(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(selection_sort(unsorted))
|
||||
|
||||
@@ -9,9 +9,6 @@ python3 -m doctest -v shell_sort.py
|
||||
For manual testing run:
|
||||
python shell_sort.py
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def shell_sort(collection):
|
||||
"""Pure implementation of shell sort algorithm in Python
|
||||
:param collection: Some mutable ordered collection with heterogeneous
|
||||
@@ -44,11 +41,6 @@ def shell_sort(collection):
|
||||
return collection
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||
user_input = input('Enter numbers separated by a comma:\n').strip()
|
||||
unsorted = [int(item) for item in user_input.split(',')]
|
||||
print(shell_sort(unsorted))
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""Topological Sort."""
|
||||
|
||||
from __future__ import print_function
|
||||
# a
|
||||
# / \
|
||||
# b c
|
||||
|
||||
Reference in New Issue
Block a user