예제 #1
0
 def test__bubble_sort(self):
     for items in self.generate_cases(self.n_cases):
         ans = sorted(items)
         t1 = time.time_ns()
         sorts.bubble_sort(items)
         self.ts.append(time.time_ns() - t1)
         self.assertEqual(items, ans)
예제 #2
0
    def test_is_sorted(self):
        a = int_sort_list[0].copy()
        bubble_sort(a)
        self.assertEqual(a, int_sort_list[1])

        b = str_sort_list[0].copy()
        bubble_sort(b)
        self.assertEqual(b, str_sort_list[1])
예제 #3
0
 def test_bubble_sort(self):
     a = [5, 8, 1, 9, 6, 3, 10, 7, 2, 4]
     b = a.copy()
     b.sort()
     bubble_sort(a)
     self.assertEqual(a, b)
     c = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
     d = c.copy()
     d.sort()
     bubble_sort(c)
     self.assertEqual(c, d)
예제 #4
0
def data_maker_bubble(liste):
    bubble_sort = open("bubble_sort.txt", "w")  # created the text file here.
    for i in range(
            len(liste)):  # a loop to go over the slices of the original list.
        newList = liste[0:i]
        a = sorts.bubble_sort(newList)
        bubble_sort.write(
            str(a) + "\n")  # writing the counts to the text file line by line
    bubble_sort.close()
    return print("Text file created")
예제 #5
0
def main():
    while True:
        line = input('Введите массив: ')

        try:
            arr = list(map(int, line.split()))

            if len(arr) == 0:
                print('Пустой массив.')
            else:
                print('\nСортировка пузырьком: ')
                print(bubble_sort(arr.copy()))

                print('Сортировка пузырьком с барьером: ')
                print(bubble_barrier_sort(arr.copy()))

                print('Сортировка шейкером: ')
                print(shaker_sort(arr.copy()), '\n')
        except ValueError:
            print('Ошибка ввода.')
예제 #6
0
bookshelf = utils.load_books('books_small.csv')
long_bookshelf = utils.load_books('books_large.csv')


def by_title_ascending(book_a, book_b):
  return book_a['title_lower'] > book_b['title_lower']

def by_author_ascending(book_a, book_b):
  return len(book_a['title_lower']) + len(book_a['author_lower']) > len(book_b['title_lower']) + len(book_b['title_lower'])

def by_total_length(book_a, book_b):
  return book_a['title_lower'] > book_b['title_lower']


sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)

bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()



sort_2 = sorts.bubble_sort(bookshelf_v1, by_title_ascending)

sort_3 = sorts.bubble_sort(bookshelf_v1, by_title_ascending)

#sort_3 = sorts.quicksort(long_bookshelf, 0, len(bookshelf_v2) - 1, by_total_length)
sorts.quicksort(long_bookshelf, 0, len(bookshelf_v2) - 1, by_total_length)

for book in long_bookshelf:
  print(len(book['title_lower']) + len(book['author_lower']))
예제 #7
0
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()

long_bookshelf = utils.load_books('books_large.csv')
long_bookshelf_v1 = long_bookshelf.copy()

#### Sorts on bookshelf

#sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
# for book in sort_1:
# print(book['title'])

print(
    "\n-----------Start of Bubble Sort /w by_author_ascending on bookshelf_v1\n"
)
sort_2 = sorts.bubble_sort(bookshelf_v1, by_author_ascending)
# for book in sort_2:
# print(book['author'])

print(
    "\n-----------Start of Quick Sort /w by_author_ascending on bookshelf_v2\n"
)
sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)
print("Quik sort: There were {0} swaps".format(sorts.quick_swaps))
# for book in bookshelf_v2:
# print(book['author'])

#### Sorts on long_bookshelf
print(
    "\n-----------Start of Bubble Sort /w by_total_length on long_bookshelf\n")
sorts.bubble_sort(long_bookshelf, by_total_length)
예제 #8
0
data_size = [100, 200, 500, 1000, 5000, 10000, 50000, 100000]

for size in data_size:
    data = []
    generator(size)  # создаем файл с данными
    with open(f'./data_{size}.txt', mode='r', encoding='UTF-8') as database:
        for one_data in database:
            data.append(Product(
                one_data.strip()))  # переносим данные из файла в массив

    data_bubble = copy.deepcopy(data)
    data_cocktail = copy.deepcopy(data)
    data_merge = copy.deepcopy(data)

    start1 = datetime.now()
    bubble_sort(data_bubble)  # сортировка пузырьком
    end1 = datetime.now()

    start2 = datetime.now()
    cocktail_sort(data_cocktail)  # шейкер-сортировка
    end2 = datetime.now()

    start3 = datetime.now()
    merge_sort(data_merge)  # сортировка слиянием
    end3 = datetime.now()

    print(str(size) + 'bubble sort:' + str(end1 - start1) + '\n')
    print(str(size) + 'cocktail sort:' + str(end2 - start2) + '\n')
    print(str(size) + 'merge sort:' + str(end3 - start3) + '\n')

    with open('./time.txt', mode='a', encoding='UTF-8') as result:
예제 #9
0
 def test_bubble_sort(self):
     a = [54, 26, 93, 17, 77, 31, 44, 55, 20]
     self.assertEqual(s.bubble_sort(a),
                      [17, 20, 26, 31, 44, 54, 55, 77, 93])
from random import randint
import logging
from copy import copy

import sorts

if __name__ == '__main__':

    N_list = [50000]
    ROUND = 1

    logging.basicConfig(level=logging.INFO)

    for N in N_list:
        randint_list = [[randint(0, 2 ** 31 - 1)
                         for _ in range(N)] for _ in range(ROUND)]

        for i in range(ROUND):
            bubble_list = copy(randint_list[i])
            insertion_list = copy(randint_list[i])
            selection_list = copy(randint_list[i])
            quick_list = copy(randint_list[i])
            heap_list = copy(randint_list[i])

            sorts.bubble_sort(bubble_list)
            sorts.insertion_sort(insertion_list)
            sorts.selection_sort(selection_list)
            sorts.quick_sort(quick_list)
            sorts.heap_sort(heap_list)
예제 #11
0
    else:
        return False


#comparisson function
def by_total_length(book_a, book_b):
    len_a = len(book_a["author"]) + len(book_a["title"])
    len_b = len(book_b["author"]) + len(book_b["title"])
    if len_a > len_b:
        return True
    else:
        return False


#testing algorithmen
ordered_v1_bookshelf = sorts.bubble_sort(bookshelf, by_title_ascending)
# for book in ordered_v1_bookshelf:
# print(book["title"])

ordered_v2_bookshelf = sorts.bubble_sort(bookshelf, by_author_ascending)
# for book in ordered_v2_bookshelf:
# print(book["author"])

ordered_v3_bookshelf = bookshelf[:]
sorts.quicksort(ordered_v3_bookshelf, 0,
                len(ordered_v3_bookshelf) - 1, by_author_ascending)
# for book in ordered_v3_bookshelf:
# print(book["author"])

ordered_v4_bookshelf = sorts.bubble_sort(long_bookshelf, by_total_length)
# for book in ordered_v4_bookshelf:
예제 #12
0
 def do_action(self):
     bubble_sort(self._array)
     self._sorted = '"Bubble sort" used'
예제 #13
0

def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


def by_total_length(book_a, book_b):
    return len(book_a['author_lower']) + len(book_a['title_lower']) > len(
        book_b['author_lower']) + len(book_b['title_lower'])


sort1 = sorts.bubble_sort(bookshelf, by_title_ascending)

# print('sorting by title .........')
# for book in sort1:
#   print(book['title'])

sort2 = sorts.bubble_sort(bookshelf_v1, by_author_ascending)
# print('\nsorting by author name .........')
# for book in sort1:
#   print(book['author'])

#sort3 = sorts.bubble_sort(long_bookshelf, by_total_length)
sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)

sorts.quicksort(long_bookshelf, 0, len(bookshelf_v2) - 1, by_author_ascending)
예제 #14
0

def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a["author_lower"] > book_b["author_lower"]


def by_total_length(book_a, book_b):
    return len(book_a["title_lower"]) + len(book_a["author_lower"]) > len(
        book_b["title_lower"]) + len(book_b["author_lower"])


sort_1 = bubble_sort(bookshelf, by_title_ascending)
sort_2 = bubble_sort(bookshelf_copy_v1, by_author_ascending)
quicksort(bookshelf_copy_v2, 0,
          len(bookshelf_copy_v2) - 1, by_author_ascending)
sort_4 = bubble_sort(bookshelf, by_total_length)
quicksort(bookshelf_copy_v3, 0, len(bookshelf_copy_v3) - 1, by_total_length)

print("\nBubble Sort - by_title_ascending")
for book in sort_1:
    print(book['title_lower'])

print("\nBubble Sort - by_author_ascending")
for book in sort_2:
    print(book['author_lower'])

print("\nQuick Sort - by_author_ascending")
예제 #15
0

def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


def by_total_length(book_a, book_b):
    return len(book_a['author_lower']) + len(book_a['title_lower']) > len(
        book_b['author_lower']) + len(book_b['title_lower'])


sort_1 = bubble_sort(bookshelf, by_title_ascending)
for book in sort_1:
    print(book['title'])

sort_2 = bubble_sort(bookshelf_v1, by_author_ascending)
for book in sort_2:
    print(book['author'])

quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)
book_2 = [book['author_lower'] for book in bookshelf_v2]
print(book_2)

# sort_3 = bubble_sort(long_bookshelf, by_total_length)

quicksort(long_bookshelf, 0, len(long_bookshelf) - 1, by_total_length)
book_3 = [book['author_lower'] for book in long_bookshelf]
예제 #16
0
import sorts

bookshelf = utils.load_books('books_small.csv')
for i in bookshelf:
    print(f'Title: {i["title"]}\nAuthor: {i["author"]}')
print(ord("a"), ord(" "), ord("A"))
for i in bookshelf:
    print(i['title_lower'])
print()


def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


sort1 = sorts.bubble_sort(bookshelf, by_title_ascending)
for i in bookshelf:
    print(f'Title: {i["title"]}\nAuthor: {i["author"]}')
print()


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


bookshelf_v1 = bookshelf[:]
# or bookshelf_v1 = bookshelf.copy()

sort2 = sorts.bubble_sort(bookshelf, by_author_ascending)
for i in bookshelf:
    print(f'Title: {i["title"]}\nAuthor: {i["author"]}')
예제 #17
0

def by_total_length(book_a, book_b):
    return len(book_a['author_lower']) + len(book_a['title_lower']) > len(
        book_b['author_lower']) + len(book_b['title_lower'])


# Comparison functions end

bookshelf = utils.load_books('books_small.csv')
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()

long_bookshelf = utils.load_books('books_large.csv')

sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
sort_2 = sorts.bubble_sort(bookshelf, by_author_ascending)
# sort_3 = sorts.bubble_sort(long_bookshelf, by_total_length)
'''
for book in sort_1:
  print(book['title'])

for book in sort_2:
  print(book['author'])

sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)
for book in bookshelf_v2:
  print(book['author'])

'''
예제 #18
0
bookshelf = utils.load_books(book_small_csv__path)
bookshelf_copy_one = bookshelf.copy()
bookshelf_copy_two = bookshelf.copy()

print()
for book in bookshelf:
    print(book["title_lower"])


# Title comparison function:
def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


bubble_sorted_by_title = sorts.bubble_sort(bookshelf, by_title_ascending)

print()
for book in bubble_sorted_by_title:
    print(book['title'])


# Author comparison function:
def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


bubble_sorted_by_author = sorts.bubble_sort(bookshelf_copy_one,
                                            by_author_ascending)

print()