from randomList import randomList from typing import List def re(iList): max_num = max(iList) # 最大值 min_num = min(iList) # 最小值 much_list = len(iList) # 桶个数 bad = max_num - min_num # 极差 all_list = [] # 总桶 for count in range(much_list): all_list.append([]) # 总桶里面的桶个数 for num in iList: extent = int((num - min_num)*(much_list - 1)/bad) # 分桶 all_list[extent].append(num) for j in range(len(all_list)): all_list[j].sort() arr = [] for first in all_list: for second in first: arr.append(second) return arr if __name__ =='__main__': iList = randomList.randomList(20) iList1 = [0.45,0.87,1.2,3.425,0.137,0.365,0.278,0.359,0.67] print(re(iList)) print(re(iList1))
if list_list[i] not in dict1: dict1[list_list[i]] = 1 else: dict1[list_list[i]] += 1 print(dict1) print(f"去重后: {result1}") list_copy = copy.deepcopy(result1) for i in range(len(result1)): min_value = min(list_copy) for j in range(dict1[min_value]): min_value = min(list_copy) z_result.append(min_value) list_copy.remove(min_value) return z_result if __name__ == '__main__': list1 = randomList.randomList(10) print(list1) quick_sort(list1, 0, len(list1) - 1) print(list1) list1 = randomList.randomList(10) print(list1) quick_sort2(list1, 0, len(list1) - 1) print(list1) print() list1 = randomList.randomList(10) print(f"未排序: {list1}") result11 = count_sort(list1) print(f"排序后: {result11}")
from typing import List from randomList import randomList def mergeSort(List): if len(List) <= 1: return List mid = len(List) // 2 left, right = List[0:mid], List[mid:] return merge(mergeSort(left), mergeSort(right)) def merge(left, right): mList = [] while left and right: if left[0] > right[0]: mList.append(right.pop(0)) else: mList.append(left.pop(0)) if left: mList.extend(left) if right: mList.extend(right) return mList if __name__ == '__main__': randomList.randomList(10) print(mergeSort(randomList.randomList(10)))
return nums for i in range(len(nums) - 1): f = 1 for j in range(len(nums) - 1 - i): if nums[j] > nums[j + 1]: nums[j], nums[j + 1] \ = nums[j + 1], nums[j] f = 0 if f == 1: break print(f"第{i + 1}轮排序结果: {nums}") return nums if __name__ == '__main__': list = randomList.randomList(10) print(f"冒泡原始数据: {list}") result = sort(list) print(f"冒泡排序数据: {result}") print() def sort1(nums: List[int]) -> List[int]: if len(nums) <= 1: return nums for i in range(len(nums) - 1): maxindex = i f = 1 for j in range(i + 1, len(nums)): if nums[maxindex] > nums[j]: maxindex = j
from randomList import randomList from typing import List def bubbleSort(List): size = len(List) if size <=1: return List for i in range(len(List)-1): for j in range(len(List) - 1 - i): if List[j] > List[j+1]: List[j],List[j + 1] = List[j+1],List[j] print('第{}几轮排序结果'.format(i)) print(List) return List if __name__ == '__main__': randomList.randomList(10) print(bubbleSort(randomList.randomList(10)))
from typing import List from randomList import randomList def sort(nums: List[int]) -> List[int]: if len(nums) <= 1: return nums for i in range(len(nums) - 1): f = 1 for j in range(len(nums) - 1 - i): if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] f = 0 if f == 1: break print(f"第{i + 1}论排序结果: {nums}") return nums if __name__ == '__main__': # a = [10, 20, 40, 5, 7, 90, 23, 34, 45, 56, 45, 56, 67, 34, 23] a = randomList.randomList(10) print(f"原始数组: {a}") res = sort(a) print(f"完成数组: {res}")
#====从小到大========== #=======冒泡排序=========== def bubble(nums: List): for i in range(len(nums) - 1): #每一次循环将最大值排到最后 for j in range(len(nums) - i - 1): #之前排过的最大值不需再次排列 if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] # print("第%s轮排序结果:"%(i+1),end=" ") # print(nums) return nums # nums1 = [4,6,7,5,3,2,10,8,7,4,5,11,14,15] a = randomList.randomList(10) # print(bubble(a)) #=======选择排序============ #选择排序 (将列表中的每一个元素和后面所有的元素比较,如果前面的元素比后面的元素大,则交换两个元素位置) def selectSort(nums: List): for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] > nums[j]: nums[i], nums[j] = nums[j], nums[i] return nums b = randomList.randomList(9)
from typing import List from randomList import randomList class Solution: def insert_sort(self, nums: List[int]) -> List[int]: if len(nums) <= 1: return nums for right in range(1, len(nums)): target = nums[right] for left in range(0, right): if nums[left] > target: nums[left + 1: right + 1] = \ nums[left: right] nums[left] = target break print(f"第{right}轮排序结果: {nums}") return nums if __name__ == '__main__': s = Solution() nums = randomList.randomList(10) print(f"插入原始数据: {nums}") result = s.insert_sort(nums) print(f"插入排序完成: {result}")
from randomList import randomList for num in range(1,20): nums = randomList.randomList(20) # 不优化 for i in range(len(nums)-1): for j in range(len(nums)-1-i): if nums[j] > nums[j+1]: nums[j],nums[j+1] = nums[j+1],nums[j] print(f'第{nums}次结果为',nums) # 优化 for i in range(len(nums)-1): flag = True for j in range(len(nums)-1-i): if nums[j] > nums[j+1]: nums[j],nums[j+1] = nums[j+1],nums[j] flag = False if flag: break print(f'第{nums}次结果为',nums)
from randomList import randomList from typing import List iList = randomList.randomList(5) class Node: def __init__(self, data): self.data = data self.next = None def __repr__(self): return f"Node({self.data})" class LinkList: def __init__(self): self.head = None def __repr__(self): current = self.head resualt = "" while current: resualt += f"{current} --> " current = current.next return resualt + "END" def linklist(self, obj: List): new_node = Node(obj[0]) self.head = new_node current = self.head
# 2.数组反转 def reverse_list(nums): l = 0 r = len(nums) - 1 while l < r: nums[l], nums[r] = nums[r], nums[l] l += 1 r -= 1 return nums if __name__ == '__main__': list = randomList.randomList(3) print(list) print(reverse_list(list)) # 3.两两交换链表的结点 # 4.三数之和 def three_sum(nums): nums.sort() result = [] for i in range(len(nums) - 2): l = i + 1 r = len(nums) - 1 while l < r: sum = nums[i] + nums[l] + nums[r]
from randomList import randomList from typing import List def selectSort(List): size = len(List) if size <= 1: return List for i in range(len(List) - 1): #外层循环控制第几轮 for j in range(i + 1, len(List)): #内存循环负责找最小值 if List[i] > List[j]: List[i], List[j] = List[j], List[i] print('第{}轮排序结果'.format(i)) print(List) return List if __name__ == '__main__': randomList.randomList(10) print(selectSort(randomList.randomList(10)))
from typing import List from randomList import randomList def insertSort(List): if len(List) <= 1: return List for right in range(1, len(List)): targrt = List[right] for left in range(0, right): if List[left] > targrt: List[left + 1:right + 1] = List[left:right] List[left] = targrt break print('第{}轮排序结果'.format(right)) print(List) return List if __name__ == '__main__': randomList.randomList(10) print(insertSort(randomList.randomList(10)))
def buckerSort(List): if len(List) <= 1: return List max_value = max(List) min_value = min(List) d = max_value - min_value buckers = len(List) # 桶的个数 count_list = [] # 空桶 for i in range(buckers): # 造桶 count_list.append([]) for i in range(len(List)): # 定位元素属于哪个桶子 num = int((List[i] - min_value) * (buckers - 1) / d) bucker = count_list[num] bucker.append(List[i]) for i in range(len(count_list)): # 每个桶子内部排序 count_list[i].sort() sort_List = [] #按顺序输出ng for i in count_list: for j in i: sort_List.append(j) return sort_List if __name__ == '__main__': randomList.randomList(10) print(buckerSort(randomList.randomList(10)))
# l[j], l[j + 1] = l[j + 1], l[j] # print("第{}轮排序结果".format(i), end=" ") # print(l) # return l # # # if __name__ == '__main__': # print("原始数据: ", iList) # print(bubblesort(iList)) # from typing import List from randomList import randomList from typing import * iList = randomList.randomList(10) def mp(l: List[int]): if len(l) <= 1: return l for i in range(1, len(l)): for j in range(len(l) - 1): if l[j] >= l[j + 1]: l[j], l[j + 1] = l[j + 1], l[j] print("第%s轮排序结果: " % i, end=" ") print(l) return l if __name__ == '__main__':