讨论/《排序算法全解析》 - 解析/
《排序算法全解析》 - 解析
共 3 个回复
class Solution {
    public void merge(int[] A, int m, int[] B, int n) {
       System.arraycopy(B,0,A,m,n);
       xuanze(A);     
    }
    public static void xuanze(int arr[]){
        for(int i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    int temp;
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                }
            }
        }
    }
}
/**
 * @param {number[]} A
 * @param {number} m
 * @param {number[]} B
 * @param {number} n
 * @return {void} Do not return anything, modify A in-place instead.
 */
var merge = function(A, m, B, n) {
  // concat
  for (let i = m; i < m + n; i++) {
    A[i] = B[i-m]
  }
  // sort
  let minIndex, maxIndex
  for (let i = 0; i < Math.floor(A.length / 2); i++) {
    minIndex = i
    maxIndex = i
    for (let j = i + 1; j < A.length - i; j++) {
      if (A[minIndex] > A[j]) {
        minIndex = j
      }
      if (A[maxIndex] < A[j]) {
        maxIndex = j
      }
    }
    if (minIndex === maxIndex) break
    let temp = A[minIndex]
    A[minIndex] = A[i]
    A[i] = temp
    if (maxIndex === i) {
      maxIndex = minIndex
    }
    let lastIndex = A.length - i - 1
    temp = A[lastIndex]
    A[lastIndex] = A[maxIndex]
    A[maxIndex] = temp
  }
  return A
};

console.log(merge([1,2,3,0,0,0],
  3,
  [2,5,6],
  3))

python 强行用选择排序算法解题 -_-

class Solution(object):
    def merge(self, A, m, B, n):
        """
        :type A: List[int]
        :type m: int
        :type B: List[int]
        :type n: int
        :rtype: None Do not return anything, modify A in-place instead.
        """
        A[m:] = B
        for i in range(0,m+n-1):
            minIndex = i
            for j in range(i+1,m+n):
                if A[minIndex] > A[j]:
                    minIndex = j
            temp = A[i]
            A[i] = A[minIndex]
            A[minIndex] = temp
        return A