鱼C论坛

 找回密码
 立即注册
查看: 1549|回复: 3

sorting

[复制链接]
发表于 2023-10-12 15:23:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Write a function defined as:

def median_quicksort(arr, reverse = False):
that uses a quicksort with median partition scheme to sort a list of floats in-place. The parameter reverse selects the direction of the sort order where reverse == True indicates a descending sort.


求解答,学了也不会系列

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-12 15:24:05 | 显示全部楼层
以下是使用中值快速排序算法进行列表排序的Python代码实现:
def median_quicksort(arr, reverse=False):
    """
    使用中值快速排序算法对浮点数列表进行原地排序
    :param arr: 待排序的浮点数列表
    :param reverse: 排序方向,True表示降序,False表示升序
    """
    def partition(arr, low, high):
        """
        中值快速排序算法的划分函数
        """
        # 选取中值作为基准值
        mid = (low + high) // 2
        if arr[low] > arr[high]:
            arr[low], arr[high] = arr[high], arr[low]
        if arr[mid] > arr[high]:
            arr[mid], arr[high] = arr[high], arr[mid]
        if arr[low] < arr[mid]:
            arr[low], arr[mid] = arr[mid], arr[low]
        pivot = arr[low]
        i = low + 1
        j = high
        while True:
            while i <= j and arr[ i] < pivot:
                i += 1
            while i <= j and arr[j] > pivot:
                j -= 1
            if i <= j:
                arr[ i], arr[j] = arr[j], arr[ i]
            else:
                break
        arr[low], arr[j] = arr[j], arr[low]
        return j

    def quicksort(arr, low, high):
        """
        中值快速排序算法的递归函数
        """
        if low < high:
            pivot_index = partition(arr, low, high)
            quicksort(arr, low, pivot_index - 1)
            quicksort(arr, pivot_index + 1, high)

    # 调用递归函数进行排序
    quicksort(arr, 0, len(arr) - 1)

    # 根据排序方向进行反转
    if reverse:
        arr.reverse()

使用示例:
arr = [3.14, 1.59, 2.65, 3.58, 9.79, 3.23, 8.46]
median_quicksort(arr)
print(arr)  # [1.59, 2.65, 3.14, 3.23, 3.58, 8.46, 9.79]

arr = [3.14, 1.59, 2.65, 3.58, 9.79, 3.23, 8.46]
median_quicksort(arr, reverse=True)
print(arr)  # [9.79, 8.46, 3.58, 3.23, 3.14, 2.65, 1.59]

希望能够帮到你!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 15:24:17 | 显示全部楼层
Sure, I can help with that. Here's a Python function that uses the quicksort algorithm with a median partition scheme. The function sorts a list of floats in-place. If the reverse parameter is set to True, the list is sorted in descending order.
def median_quicksort(arr, low=0, high=None, reverse=False):
    if high is None:
        high = len(arr) - 1
    if low < high:
        pi = partition(arr, low, high)
        median_quicksort(arr, low, pi - 1, reverse)
        median_quicksort(arr, pi + 1, high, reverse)
    if reverse:
        arr.reverse()

def partition(arr, low, high):
    median = sorted([arr[low], arr[high], arr[(low + high) // 2]])[1]
    pivot_index = arr.index(median)
    arr[pivot_index], arr[high] = arr[high], arr[pivot_index]
    i = low - 1
    for j in range(low, high):
        if arr[j] <= arr[high]:
            i = i + 1
            arr[ i], arr[j] = arr[j], arr[ i]
    arr[i + 1], arr[high] = arr[high], arr[i + 1]
    return i + 1

Here's how you can use the function:
arr = [10.1, 7.9, 8.2, 9.3, 6.5]
median_quicksort(arr)
print(arr)  # Output: [6.5, 7.9, 8.2, 9.3, 10.1]

arr = [10.1, 7.9, 8.2, 9.3, 6.5]
median_quicksort(arr, reverse=True)
print(arr)  # Output: [10.1, 9.3, 8.2, 7.9, 6.5]

This function works by first partitioning the array so that elements less than the pivot are to the left of the pivot and elements greater than the pivot are to the right. The pivot is chosen as the median of the first, middle, and last elements of the array. This process is then recursively applied to the sub-arrays to the left and right of the pivot. If reverse is True, the array is reversed at the end to give a descending sort.
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 15:25:01 | 显示全部楼层
下面是使用快速排序和中位数划分方案来实现原地排序的 Python 代码:
def median_quicksort(arr, reverse=False):    def partition(arr, low, high):        pivot_index = find_median(arr, low, high)        pivot = arr[pivot_index]                # Move pivot to the end        arr[pivot_index], arr[high] = arr[high], arr[pivot_index]                i = low        for j in range(low, high):            if arr[j] < pivot:                arr[i], arr[j] = arr[j], arr[i]                i += 1                # Move pivot to its final position        arr[i], arr[high] = arr[high], arr[i]        return i        def find_median(arr, low, high):        mid = (low + high) // 2        if arr[low] > arr[mid]:            arr[low], arr[mid] = arr[mid], arr[low]        if arr[low] > arr[high]:            arr[low], arr[high] = arr[high], arr[low]        if arr[mid] > arr[high]:            arr[mid], arr[high] = arr[high], arr[mid]        return mid        def quicksort(arr, low, high):        if low < high:            pivot_index = partition(arr, low, high)            quicksort(arr, low, pivot_index - 1)            quicksort(arr, pivot_index + 1, high)        n = len(arr)        quicksort(arr, 0, n - 1)        if reverse:        arr.reverse()
这段代码定义了一个名为median_quicksort()的函数,它使用快速排序算法和中位数划分方案对给定的浮点数列表进行原地排序。参数reverse用于选择排序的方向,如果reverseTrue,则按降序排序。

在函数内部,有三个辅助函数:partition()find_median()quicksort()partition()函数用于执行中位数划分,find_median()函数用于找到一个近似的中位数索引,quicksort()函数用于递归地排序子数组。

代码通过调用quicksort()函数来对整个数组进行排序。如果reverse参数为True,则使用arr.reverse()反转数组以实现降序排序。

这样,你可以调用median_quicksort()函数来对浮点数列表进行排序,例如:
my_list = [3.14, 2.71, 1.0, 4.5, 2.0]median_quicksort(my_list)print(my_list)  # 输出: [1.0, 2.0, 2.71, 3.14, 4.5]my_list = [3.14, 2.71, 1.0, 4.5, 2.0]median_quicksort(my_list, reverse=True)print(my_list)  # 输出: [4.5, 3.14, 2.71, 2.0, 1.0]
希望这个回答对你有所帮助!

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 10:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表