首页 > 试题广场 >

(数据结构与算法)列举至少2种排序算法(如快排),并写出实现

[问答题]
数据结构与算法)列举至少2种排序算法(如快排),并写出实现代码

折半排序和冒泡排序
发表于 2023-03-21 10:04:56 回复(0)
def BubbleSort(nums):
    for i in range(0,len(nums)-1):
         for j in range(0,len(nums)-1-i):
             if nums[j+1] < nums[j]:
                    nums[j],nums[j+1] = nums[j+1],nums[j]
    return nums

def QuickSort(nums,lo,hi):
    if hi - lo < 2:
        return
    mi = partition(nums,lo,hi-1)
    QuickSort(nums,lo,mi)
    QuickSort(nums,mi+1,hi)

def patition(nums,lo,hi):
    nums[lo],nums[(lo + hi) // 2] = nums[(lo + hi) // 2],nums[lo]
   p = nums[lo]
    while(lo < hi):
        while(lo<hi and p <= nums[hi]):
            hi -= 1
        nums[lo] = nums[hi]
        while(lo < hi and nums[lo] <= p):
            lo += 1
        nums[hi] = nums[lo]
    nums[lo] = p
    return lo def quicksort(nums):
    QuickSort(nums,0,len(nums)) 

发表于 2019-07-25 21:33:08 回复(0)
王菲菲往往
发表于 2019-03-19 15:46:36 回复(0)
冒泡排序
for i in range(0,len(arr)): for j in range(0,len(arr):
         if arr[i]==arr[j]:
             continue;
          else if arr[i]<arr[j]:
                arr[i],arr[j]=arr[j],list[i]     选择排序
for i in range(0,len(arr)-1):
    t=i
    for j in range(i+1,len(arr)):
        if arr[j]<arr[t]:
            t=j
        arr[i],arr[t]=arr[t],arr[i]

发表于 2019-03-07 19:56:14 回复(0)