剑指offer-最小的K个数-时间效率-排序-python

python

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

这就是排序题(将结果的最小K值输出)

 

# -*- coding:utf-8 -*-

#快速排序

def quick_sort(lst):

if not lst:

return []

pivot = lst[0]

left = quick_sort([x for x in lst[1:] if x < pivot])

right = quick_sort([x for x in lst[1:] if x >= pivot])

return left + [pivot] + right

#归并排序

def merge_sort(lst):

if len(lst) <= 1:

return lst

mid = len(lst) // 2

left = merge_sort(lst[: mid])

right = merge_sort(lst[mid:])

return merge(left, right)

def merge(left, right):

l, r, res = 0, 0, []

while l < len(left) and r < len(right):

if left[l] <= right[r]:

res.append(left[l])

l += 1

else:

res.append(right[r])

r += 1

res += left[l:]

res += right[r:]

return res

#堆排序

def siftup(lst, temp, begin, end):

if lst == []:

return []

i, j = begin, begin * 2 + 1

while j < end:

if j + 1 < end and lst[j + 1] > lst[j]:

j += 1

elif temp > lst[j]:

break

else:

lst[i] = lst[j]

i, j = j, 2 * j + 1

lst[i] = temp

def heap_sort(lst):

if lst == []:

return []

end = len(lst)

for i in range((end // 2) - 1, -1, -1):

siftup(lst, lst[i], i, end)

for i in range(end - 1, 0, -1):

temp = lst[i]

lst[i] = lst[0]

siftup(lst, temp, 0, i)

return lst

#冒泡排序

def bubble_sort(lst):

if lst == []:

return []

for i in range(len(lst)):

for j in range(1, len(lst) - i):

if lst[j-1] > lst[j]:

lst[j-1], lst[j] = lst[j], lst[j-1]

return lst

#直接选择排序

def select_sort(lst):

if lst == []:

return []

for i in range(len(lst)-1):

smallest = i

for j in range(i, len(lst)):

if lst[j] < lst[smallest]:

smallest = j

lst[i], lst[smallest] = lst[smallest], lst[i]

return lst

#插入排序

def Insert_sort(lst):

if lst == []:

return []

for i in range(1, len(lst)):

temp = lst[i]

j = i

while j > 0 and temp < lst[j - 1]:

lst[j] = lst[j - 1]

j -= 1

lst[j] = temp

return lst

def GetLeastNumbers_Solution(tinput, k):

# write code here

if tinput == [] or k > len(tinput):

return []

# tinput.sort()

tinput = quick_sort(tinput)

return tinput[: k]

tinput,k =[4,5,1,6,2,7,3,8],4

print GetLeastNumbers_Solution(tinput, k)

以上是 剑指offer-最小的K个数-时间效率-排序-python 的全部内容, 来源链接: utcz.com/z/388555.html

回到顶部