0%

Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

def mergeSort(nums):
if len(nums) <= 1: return nums
# devide
mid = len(nums)//2
left = nums[:mid]
right = nums[mid:]
return mergeFunc(mergeSort(left),mergeSort(right))

def mergeFunc(left,right):
# merge
res = []
while left and right:
if left[0] <= right[0]:
res.append(left.pop(0))
else:
res.append(right.pop(0))
res += left
res += right

return res

if __name__ == "__main__":
nums = [10, 17, 50, 7, 30, 24, 30, 45, 15, 5, 36, 21]
print(mergeSort(nums))

(1) average cost time: T(n) = O(nlogn)
(2) average cost space: S(n) = O(n)
(3) Not in-place sort
(4) stable
(5) best T(n) = O(nlogn), worst T(n) = O(nlogn)

Linked List

lc 148: Given the head of a linked list, return the list after sorting it in ascending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def sortList(head):
if not head or not head.next: return head
# get mid of linked list
slow = fast = head
while fast and fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# devide
right = slow.next
slow.next = None

# merge
def mergeFunc(head,right):
dummy = ListNode()
cur = dummy
while head and right:
if head.val <= right.val:
cur.next = head
head = head.next
# cur = cur.next
# cur.next = None
else:
cur.next = right
right = right.next
cur = cur.next
cur.next = None
if head:
cur.next = head
if right:
cur.next = right
return dummy.next

return mergeFunc(sortList(head),sortList(right))


(1) average cost time: T(n) = O(nlogn)
(2) average cost space: S(n) = O(1)
(3) in-place
(4) stable
(5) best T(n) = O(nlogn), worst T(n) = O(nlogn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def solution(nums):
low, high = 0, len(nums)-1
quickSort(nums,low,high)
return nums

def quickSort(nums,low,high):
if low < high:
pivotpos = partition(nums,low,high)
quickSort(nums,low,pivotpos-1)
quickSort(nums,pivotpos+1,high)

def partition(nums,low,high):
pivot = nums[low]
while low < high:
while low < high and nums[high] >= pivot:
high -= 1
nums[low] = nums[high]
while low < high and nums[low] <= pivot:
low += 1
nums[high] = nums[low]
nums[low] = pivot
return low


if __name__ == "__main__":
nums = [10, 17, 50, 7, 30, 24, 30, 45, 15, 5, 36, 21]
print(solution(nums))

(1) avarage time cost: T(n) = O(nlogn)
(2) avarage time cost: S(n) = O(logn)
(3) in-place sort
(4) unstable
(5) best T(n)= O(nlogn), worst T(n) = O(nlogn)

1
2
3
4
5
6
7
8
9
def bubbleSort(nums):
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]
return nums
if __name__ == '__main__':
nums = [10, 17, 50, 7, 30, 24, 30, 45, 15, 5, 36, 21]
print(bubbleSort(nums))

(1) average time cost T(n): O(n^2)
(2) average space cost S(n): O(1)
(3) in-place sort
(4) stable
(5) best T(n): O(n); Worst T(n): O(n^2)

Problem

bash: hexo: command not found

Solution

Step 1. Add environment variables
Add D:\Blog\node_modules\.bin to path.
If not working, then step 2.

Step 2. install hexo

  • go to D:\Blog\
  • git bash here
  • npm i -g hexo-cli