Пример #1
0
 def test_string_sort(self):
     a_list = [
         "this", "is", "some", "sample", "data", "for", "sorting", "test"
     ]
     b_list = a_list.copy()
     insertion_sort(a_list)
     b_list.sort()
     self.assertEqual(a_list, b_list)
Пример #2
0
def shell_sort(a_list: list, h_tuple: tuple = (13, 5, 1)):
    """
    performs inplace shell sort on the given array.
    This is done by calling many insertions sorts with decreasing exchange length
    which is efficient because complexity of insertion sort on partially sorted array is basically linear
    complexity O(N^(3/2)) in practise much faster
    :param a_list: a list to sort
    :param h_tuple: tuple of exchange lengths for insertion sort
    """
    for h in h_tuple:
        insertion_sort(a_list, h)
Пример #3
0
def bucket_sort(nums):
    min, max = nums[0], nums[0]
    for num in nums:
        if num < min:
            min = num
        if num > max:
            max = num
    step = 9
    bucket_num = max // step - min // step + 1
    bucket_list = []
    for i in range(bucket_num):
        bucket_list.append([])
    for i in range(len(nums)):
        index = index_for(nums[i], min, step)
        bucket_list[index].append(nums[i])
    index = 0
    for i in range(bucket_num):
        bucket = bucket_list[i]
        insertion_sort(bucket)
        for k in bucket:
            nums[index] = k
            index += 1
 def test_insertion_sort(self):
     self.assertEqual([1, 5, 23, 57, 65, 1232],
                      insertion_sort([1, 5, 65, 23, 57, 1232]))
Пример #5
0
import copy

from sort.bubble_sort import bubble_sort
from sort.cocktail_sort import cocktail_sort
from sort.comb_sort import comb_sort
from sort.selection_sort import selection_sort
from sort.gnome_sort import gnome_sort
from sort.insertion_sort import insertion_sort

if __name__ == "__main__":
    import random
    nums = [random.randint(0, 1000) for _ in range(100)]

    bubble = bubble_sort(copy.copy(nums))
    comb = comb_sort(copy.copy(nums))
    cocktail = cocktail_sort(copy.copy(nums))
    selection = selection_sort(copy.copy(nums))
    gnome = gnome_sort(copy.copy(nums))
    inserion = insertion_sort(copy.copy(nums))

    print(bubble == comb)
    print(bubble == cocktail)
    print(bubble == selection)
    print(bubble == gnome)
    print(bubble == inserion)
from sort.selection_sort import selection_sort
from sort.insertion_sort import insertion_sort
from sort.merge_sort import merge_sort
from sort.quick_sort import quick_sort
from sort.shell_sort import shell_sort

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
print("Lista original:", data)
bubble_sort(data)
print("Ordenado con burbuja clasica", data)

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
selection_sort(data)
print("Ordenado con selection sort:", data)

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
insertion_sort(data)
print("Ordenado con insertion sort:", data)

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
merge_sort(data)
print("Ordenado con MergeSort:", data)

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
quick_sort(data, 0, len(data) - 1)
print("Ordenando con Quicksort:", data)

data = [2, 3, 1, 9, 6, 4, 5, 7, 8]
shell_sort(data)
print("Ordenando con Shellsort:", data)
Пример #7
0
 def test_integer_sort(self):
     a_list = list(range(100))
     shuffle(a_list)
     insertion_sort(a_list)
     self.assertEqual(a_list, list(range(100)))