Java教程

排序算法

本文主要是介绍排序算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

快排

import random
from typing import List


class QuickSortMethod:
    def quick_sort(self, nums):
        self.quick_sort_help(nums, 0, len(nums) - 1)
        return nums

    def quick_sort_help(self, nums, left, right):
        if left >= right:
            return nums
        if left < right:
            idx = random.randint(left, right)
            pivot = nums[idx]
            nums[idx], nums[left] = nums[left], nums[idx]
            i = left
            j = right
            while i < j:
                while i < j and nums[j] > pivot:
                    j -= 1
                nums[i] = nums[j]
                while i < j and nums[i] <= pivot:
                    i += 1
                nums[j] = nums[i]
            nums[i] = pivot
            self.quick_sort_help(nums, left, i - 1)
            self.quick_sort_help(nums, i + 1, right)
        return nums

归并排序

class MergeSolution:
    def merge_sort(self, nums, l, r):
        if l == r:
            return
        mid = (l + r) // 2
        self.merge_sort(nums, l, mid)
        self.merge_sort(nums, mid + 1, r)
        tmp = []
        i, j = l, mid + 1
        while i <= mid or j <= r:
            if i > mid or (j <= r and nums[j] < nums[i]):
                tmp.append(nums[j])
                j += 1
            else:
                tmp.append(nums[i])
                i += 1
        nums[l: r + 1] = tmp

    def sortArray(self, nums: List[int]) -> List[int]:
        self.merge_sort(nums, 0, len(nums) - 1)
        return nums

堆排序

class HeapSort:
    def sortArray(self, nums):
        self.heap_sort(nums)
        return nums

    def heap_sort(self, nums):
        self.heap_init(nums)
        for i in range(len(nums) - 1, -1, -1):
            nums[i], nums[0] = nums[0], nums[i]
            self.adjust_heap_down(nums, i - 1, 0)

    def heap_init(self, nums):
        n = len(nums) - 1
        for i in range(n, -1, -1):
            self.adjust_heap_down(nums, n, i)

    def adjust_heap_down(self, nums, heap_size, idx):
        left = 2 * idx + 1
        while left <= heap_size:
            child_idx = left
            right = left + 1
            if right <= heap_size and nums[right] > nums[left]:
                child_idx = right
            if nums[child_idx] <= nums[idx]:
                break
            nums[idx], nums[child_idx] = nums[child_idx], nums[idx]
            idx = child_idx
            left = 2 * idx + 1

from collections import Counter
class Solution2:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        def site_up(arr, idx):
            parent = (idx - 1) // 2
            while parent >= 0:
                if arr[parent][1] > arr[idx][1]:
                    arr[idx], arr[parent] = arr[parent], arr[idx]
                    idx = parent
                    parent = (idx - 1) // 2
                else:
                    break

        def site_down(arr, idx, size):
            left_child = idx * 2 + 1
            while left_child <= size:
                child = left_child
                right_child = left_child + 1
                if right_child <= size and arr[right_child][1] < arr[left_child][1]:
                    child = right_child
                if arr[child][1] < arr[idx][1]:
                    arr[idx], arr[child] = arr[child], arr[idx]
                    idx = child
                    left_child = 2 * idx + 1
                else:
                    break

        stat = Counter(nums)
        stat = list(stat.items())
        heap = []
        for i in range(k):
            heap.append(stat[i])
            site_up(arr=heap, idx=len(heap) - 1)

        for i in range(k, len(stat)):
            if stat[i][1] > heap[0][1]:
                heap[0] = stat[i]
                site_down(heap, 0, k - 1)

        return [item[0] for item in heap[:k]]


class Solution3:
    def frequencySort(self, s: str) -> str:
        c = Counter(s)
        res = dict(sorted(c.items(), key=lambda x: -x[1]))
        return ''.join(k * v for k, v in res.items())


import collections
import heapq


class Solution4:
    def isPossible(self, nums: List[int]) -> bool:
        mp = collections.defaultdict(list)
        print(mp)
        for x in nums:
            print("mp:", mp.items())
            if mp.get(x - 1):
                queue = mp.get(x - 1)
                prevLength = heapq.heappop(queue)
                heapq.heappush(mp[x], prevLength + 1)
            else:
                heapq.heappush(mp[x], 1)

        return not any(queue and queue[0] < 3 for queue in mp.values())


if __name__ == "__main__":
    s = Solution4()
    # nums = [5, -3, 9, 1, 7, 7, 9, 10, 2, 2, 10, 10, 3, -1, 3, 7, -9, -1, 3, 3]
    # k = 3
    s.isPossible([1, 2, 3, 3, 4, 4, 5, 5])
这篇关于排序算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!