Several algorithms interview often test

Foreword

Sorting algorithm is the focus of the interview test center, which is part of the algorithm is not difficult, but the details are highlights basic skills, get to the bottom if it is a lot of people are afraid of the answer is unclear.
By Leetcode today to say something about the title of a template to achieve (Python) various algorithms, as well as some notes, inadequacies please correct me.

Involve algorithms

  • Bubble Sort
  • Quick Sort
  • Merge sort
  • Heapsort
  • Selection Sort
  • Insertion Sort
  • shell sort

Leetcode assessment address - 75 Sort Colors Medium difficulty

text

It is intended to explain the problem:
Given a red, white and blue, a total of n elements of the array, sorts them in situ, such that adjacent elements with the same color and arranged in a red, white, blue order.
Actually very simple, look for the array row sequence on the list, but with their own sort on the boring, this question can be used to precisely measure what the various sorting algorithms.

  1. Bubble Sort
    The idea is to make every element and its adjacent elements of comparison and exchange value, every trip down there an element is bubbling to the top, that is to say it lined up, the next trip to the sort and it would not compare.
    Therefore, the need to compare the number of each pass will be -1, implement, control the number of comparison cycles of the first layer, the second layer loop executes bubbling operation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class : DEF (Self, the nums: List [int]) -> None : "" "         thought bubble sort:         sequentially for each element in the array are sorted, they are discharged to a suitable position up         specific method is the a back element and its contrast element, big (smaller) than the position he exchanged, it has been switched to the right position until the         formation of a blister similar to the process from the bottom to take up         : Note point         after the last element each trip sorting are determined, so the number of comparisons to be - 1, so that the outer control loop is in fact the number of comparisons         "" "         L = len (the nums) for I in Range (L): for J in Range (L - I - . 1 ): IF the nums [J]> the nums [J + . 1 ]:        			the nums [J], the nums [J + . 1 ] = the nums [J + . 1 ], the nums [J]
















DEF sortColors (Self, nums: List [int]) -> None : "" " the Do not return Anything, the Modify nums in-Place INSTEAD. " "" self.bubble_ large column interview often test several algorithms sort (nums)



 
后面就不写全部的类了,只给出方法。
  1. Quicksort
    quick drain of the idea is to select a reference number, it is smaller than the value on the left, which is larger than the value on the right, and then repeat the steps on the left and right sections, respectively, until only a section element.
    Recursive, but not in place of, but this question submitted.

    1
    2
    3
    4
    5
    6
    7
    8
    def quick_sort(self, nums: List[int]) -> None:
    if len(nums) < 2:
    return nums
    pivit_index = 0
    pivit = nums[pivit_index]
    left = [i for i in nums[pivit_index + 1:] if i <= pivit]
    right = [i for i in nums[pivit_index + 1:] if i > pivit]
    return self.quick_sort(left) + [pivit] + self.quick_sort(right)

    Non-recursive version, place the sort, with a stack save interval lower and upper bounds

    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 partition(self, nums, l, r):
    pivit = nums[l]
    while l < r:
    while l < r and nums[r] >= pivit:
    r -= 1
    nums[l] = nums[r]
    while l < r and nums[l] <= pivit:
    l += 1
    nums[r] = nums[l]
    nums[l] = pivit
    return l

    def quick_sort(self, nums: List[int]) -> None:
    stk = []
    stk.append(len(nums) - 1)
    stk.append(0)
    while stk:
    l = stk.pop()
    r = stk.pop()
    mid = self.partition(nums, l, r)
    if l < mid:
    stk.append(mid - 1)
    stk.append(l)
    if r > mid:
    stk.append(r)
    stk.append(mid + 1)

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11961130.html