-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
王洋
committed
Jul 26, 2024
1 parent
18d97e7
commit adbe5df
Showing
3 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 从utilsPython/array.py导入quick_sort | ||
# from utilsPython.array import quick_sort | ||
from typing import List | ||
|
||
def quick_sort(arr: List[int]) -> List[int]: | ||
if len(arr) <= 1: | ||
return arr | ||
pivot = arr[len(arr) // 2] | ||
left = [x for x in arr if x < pivot] | ||
middle = [x for x in arr if x == pivot] | ||
right = [x for x in arr if x > pivot] | ||
return quick_sort(left) + middle + quick_sort(right) | ||
|
||
class Solution: | ||
def findValueOfPartition(self, nums: List[int]) -> int: | ||
sortedNums = quick_sort(nums) | ||
n = len(sortedNums) | ||
res = -1 | ||
for i in range(0, n-1): | ||
x = sortedNums[i + 1] - sortedNums[i] | ||
if res == -1 or x < res: | ||
res = x | ||
return res | ||
|
||
s = Solution() | ||
print(s.findValueOfPartition([1,3,2,4])) # 1 | ||
print(s.findValueOfPartition([100,1,10])) # 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# array.py | ||
|
||
from typing import List | ||
|
||
def quick_sort(arr: List[int]) -> List[int]: | ||
if len(arr) <= 1: | ||
return arr | ||
pivot = arr[len(arr) // 2] | ||
left = [x for x in arr if x < pivot] | ||
middle = [x for x in arr if x == pivot] | ||
right = [x for x in arr if x > pivot] | ||
return quick_sort(left) + middle + quick_sort(right) | ||
|
||
def merge_sort(arr: List[int]) -> List[int]: | ||
if len(arr) <= 1: | ||
return arr | ||
middle = len(arr) // 2 | ||
left = merge_sort(arr[:middle]) | ||
right = merge_sort(arr[middle:]) | ||
return merge(left, right) | ||
|
||
def merge(left: List[int], right: List[int]) -> List[int]: | ||
result = [] | ||
while left and right: | ||
if left[0] < right[0]: | ||
result.append(left.pop(0)) | ||
else: | ||
result.append(right.pop(0)) | ||
result.extend(left if left else right) | ||
return result | ||
|
||
def heap_sort(arr: List[int]) -> List[int]: | ||
def heapify(arr: List[int], n: int, i: int): | ||
largest = i | ||
left = 2 * i + 1 | ||
right = 2 * i + 2 | ||
|
||
if left < n and arr[left] > arr[largest]: | ||
largest = left | ||
|
||
if right < n and arr[right] > arr[largest]: | ||
largest = right | ||
|
||
if largest != i: | ||
arr[i], arr[largest] = arr[largest], arr[i] | ||
heapify(arr, n, largest) | ||
|
||
n = len(arr) | ||
for i in range(n // 2 - 1, -1, -1): | ||
heapify(arr, n, i) | ||
|
||
for i in range(n - 1, 0, -1): | ||
arr[i], arr[0] = arr[0], arr[i] | ||
heapify(arr, i, 0) | ||
|
||
return arr | ||
|
||
def shell_sort(arr: List[int]) -> List[int]: | ||
n = len(arr) | ||
gap = n // 2 | ||
while gap > 0: | ||
for i in range(gap, n): | ||
temp = arr[i] | ||
j = i | ||
while j >= gap and arr[j - gap] > temp: | ||
arr[j] = arr[j - gap] | ||
j -= gap | ||
arr[j] = temp | ||
gap //= 2 | ||
return arr | ||
|
||
def insertion_sort(arr: List[int]) -> List[int]: | ||
for i in range(1, len(arr)): | ||
key = arr[i] | ||
j = i - 1 | ||
while j >= 0 and key < arr[j]: | ||
arr[j + 1] = arr[j] | ||
j -= 1 | ||
arr[j + 1] = key | ||
return arr | ||
|
||
def selection_sort(arr: List[int]) -> List[int]: | ||
for i in range(len(arr)): | ||
min_idx = i | ||
for j in range(i + 1, len(arr)): | ||
if arr[j] < arr[min_idx]: | ||
min_idx = j | ||
arr[i], arr[min_idx] = arr[min_idx], arr[i] | ||
return arr | ||
|
||
def bubble_sort(arr: List[int]) -> List[int]: | ||
n = len(arr) | ||
for i in range(n): | ||
swapped = False | ||
for j in range(0, n - i - 1): | ||
if arr[j] > arr[j + 1]: | ||
arr[j], arr[j + 1] = arr[j + 1], arr[j] | ||
swapped = True | ||
if not swapped: | ||
break | ||
return arr |