首頁  >  文章  >  web前端  >  JavaScript 程式在按行排序的矩陣中尋找中位數

JavaScript 程式在按行排序的矩陣中尋找中位數

WBOY
WBOY轉載
2023-09-16 15:05:02984瀏覽

JavaScript 程序在按行排序的矩阵中查找中位数

我們將描述使用 JavaScript 在按行排序的矩陣中尋找中位數的過程。首先,我們將遍歷矩陣以將所有元素收集到一個陣列中。然後,我們對數組進行排序以找到中間的值,這將是我們的中位數。如果元素個數為偶數,則中位數為中間兩個值的平均值。

方法

給定一個按行排序的矩陣,可以透過以下方法找到中位數 -

  • 將所有行合併到一個排序數組。

  • 找到組合陣列的中間元素,這將是中位數。

  • 如果組合陣列中的元素數量為奇數,則傳回中間元素作為中位數。

  • 如果組合陣列中的元素個數為偶數,則傳回中間兩個元素的平均值作為中位數。

  • 此方法的時間複雜度為 O(m * n log (m * n)),其中 m 是矩陣中的行數,n 是矩陣中的列數。

    李>
  • 空間複雜度為 O(m * n),因為整個矩陣需要組合成一個陣列。

範例

這是一個 JavaScript 函數的完整工作範例,用於尋找按行排序的矩陣中的中位數 -

function findMedian(matrix) {
   
   // Get the total number of elements in the matrix
   const totalElements = matrix.length * matrix[0].length;
   
   // Calculate the middle index of the matrix
   const middleIndex = Math.floor(totalElements / 2);
   
   // Initialize start and end variables to keep track of the search space
   let start = matrix[0][0];
   let end = matrix[matrix.length - 1][matrix[0].length - 1];
    
   while (start <= end) {
   
      // Calculate the mid point
      let mid = Math.floor((start + end) / 2);
      
      // Initialize a counter to keep track of the number of elements less than or equal to the mid value
      let count = 0;
      
      // Initialize a variable to store the row index of the last element less than or equal to the mid value
      let rowIndex = -1;
          
      // Loop through each row in the matrix
      for (let i = 0; i < matrix.length; i++) {
      
         // Use binary search to find the first element greater than the mid value in the current row
         let columnIndex = binarySearch(matrix[i], mid);
         
         // If the current row has no element greater than the mid value, increment the count by the length of the row
         if (columnIndex === -1) {
            count += matrix[i].length;
            rowIndex = i;
         } else {
         
            // Otherwise, increment the count by the column index of the first element greater than the mid value
            count += columnIndex;
            break;
         }
      }
         
      // Check if the count of elements less than or equal to the mid value is greater than or equal to the middle index
      if (count >= middleIndex) {
         end = mid - 1;
      } else {
         start = mid + 1;
         rowIndex++;
      }
         
      // Check if we have reached the middle index
      if (count === middleIndex) {
         return matrix[rowIndex][middleIndex - count];
      }
   }
  
   return start;
}

// Helper function for binary search
function binarySearch(arr, target) {
   let start = 0;
   let end = arr.length - 1;
     
   while (start <= end) {
      let mid = Math.floor((start + end) / 2);
      if (arr[mid] === target) {
         return mid;
      } else if (arr[mid] < target) {
         start = mid + 1;
      } else {
         end = mid - 1;
      }
   }
     
   return start === 0 ? -1 : start - 1;
}
const arr = [
   [1, 2, 3], 
   [4, 5, 6], 
   [7, 8, 9]
];

console.log(findMedian(arr));

說明

  • findMedian函數接受矩陣為參數。它首先分別使用 totalElements middleIndex 計算矩陣中的元素總數和中間索引(中位數)。

  • startend變數分別初始化為矩陣的第一個和最後一個元素,因為它們是矩陣中的最小值和最大值.

以上是JavaScript 程式在按行排序的矩陣中尋找中位數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除