Fundamentals 5 min read

Fish Division Puzzle and Classic Sorting Algorithms (Merge, Selection, Bubble) with Python Implementations

This article presents a combinatorial fish‑division puzzle solved by exhaustive search with Python code, followed by clear explanations and Python implementations of three fundamental sorting algorithms—merge sort, selection sort, and bubble sort—illustrating their core ideas and step‑by‑step processes.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Fish Division Puzzle and Classic Sorting Algorithms (Merge, Selection, Bubble) with Python Implementations

Fish Division Puzzle

Five people (A, B, C, D, E) catch fish at night. The next morning each wakes up in turn, discards one extra fish, divides the remaining fish into five equal parts, and takes one part for themselves. The problem asks for the minimum number of fish initially caught.

Solution Idea

Using exhaustive search, assume the initial number of fish is x . The condition (x‑1) % 5 == 0 must hold for the first division; after each person takes their share, the remaining fish become ((x‑1)/5)*4 , which must satisfy the same condition for the next person. The smallest x that meets all five steps is found by iterating.

Python implementation

def fish():
    """ 五人分鱼 """
    fish = 1
    while True:
        total = fish
        enough = True
        for _ in range(5):
            if (total - 1) % 5 == 0:
                total = (total - 1) // 5 * 4
            else:
                enough = False
                break
        if enough:
            print(fish)
            break
        fish += 1

Merge Sort

Merge sort follows a divide‑and‑conquer strategy: split the array repeatedly until each sub‑array contains a single element, then merge the sorted sub‑arrays step by step to produce a fully sorted array.

Python implementation

def merge_sort(arr):
    """ 归并法/分治法排序 """
    if len(arr) < 2:
        return arr[:]
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)

def merge(left, right):
    print(left)
    print(right)
    print('\n')
    result = []
    index_left, index_right = 0, 0
    while index_left < len(left) and index_right < len(right):
        if left[index_left] <= right[index_right]:
            result.append(left[index_left])
            index_left += 1
        else:
            result.append(right[index_right])
            index_right += 1
    result += left[index_left:]
    result += right[index_right:]
    return result

Selection Sort

Selection sort repeatedly selects the smallest remaining element and swaps it into its correct position, moving from the start of the array toward the end.

Python implementation

def select_sort(origin_items):
    """ 选择排序算法 """
    items = origin_items[:]
    for i in range(len(items) - 1):
        min_index = i
        for j in range(i + 1, len(items)):
            if items[j] < items[min_index]:
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]
    return items

Bubble Sort

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order, causing larger elements to “bubble” to the end of the list after each pass.

Python implementation

def bubble_sort(origin_items):
    """ 冒泡排序算法 """
    items = origin_items[:]
    for i in range(len(items) - 1):
        for j in range(0, len(items) - 1 - i):
            if items[j] > items[j + 1]:
                items[j], items[j + 1] = items[j + 1], items[j]
    return items

— END —

algorithmPythonsortingbubble sortmerge sortselection sortpuzzle
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.