Share 4. median of two sorted arrays c++,

homaggie

New member
#C ++, #Median, #Sorted mảng, #algorithms ## 4. Trung bình của hai mảng được sắp xếp trong C ++

Trung bình của một danh sách các số là giá trị trung bình khi danh sách được sắp xếp theo thứ tự tăng dần.Ví dụ, trung bình của danh sách [1, 3, 5, 7, 9] là 5.

Tìm kiếm trung bình của hai mảng được sắp xếp là một vấn đề phổ biến trong khoa học máy tính.Có một vài thuật toán khác nhau có thể được sử dụng để giải quyết vấn đề này, nhưng thuật ngữ hiệu quả nhất là thuật toán chia và chinh phục ** **.

Thuật toán phân chia và chinh phục hoạt động bằng cách chia hai mảng thành các mảng nhỏ hơn và nhỏ hơn cho đến khi mỗi mảng con chỉ chứa một phần tử.Trung bình của hai mảng sau đó là trung bình của các trung vị của hai mảng con.

Dưới đây là việc triển khai thuật toán phân chia và chinh phục trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

// có chức năng tìm trung vị của một mảng được sắp xếp
int findmedian (int mảng [], int n) {
// Nếu mảng có số phần tử lẻ, trung bình là phần tử giữa
if (n % 2 == 1) {
trả lại mảng [n / 2];
}

// Nếu mảng có số lượng phần tử chẵn, trung bình là trung bình của hai yếu tố giữa
khác {
return (mảng [n / 2 - 1] + mảng [n / 2]) / 2;
}
}

// có chức năng tìm trung vị của hai mảng được sắp xếp
int findMedianoftWosortedArrays (int arr1 [], int arr2 [], int n1, int n2) {
// Nếu một trong các mảng trống, trung bình là trung bình của mảng khác
if (n1 == 0) {
Trả lại FindMedian (ARR2, N2);
} if if (n2 == 0) {
Trả lại FindMedian (ARR1, N1);
}

// tìm thấy nhỏ hơn trong hai kích thước mảng
int minsize = min (n1, n2);

// tìm kiếm đệ quy các trung vị của hai mảng con
int median1 = findmedian (ARR1, MINSIZE / 2);
int median2 = findmedian (ARR2, MINSIZE / 2);

// Nếu hai trung vị bằng nhau, trung bình của hai mảng là như nhau
if (median1 == median2) {
trả lại trung bình1;
}

// Nếu trung bình đầu tiên nhỏ hơn so với trung bình thứ hai, trung bình của hai mảng nằm trong nửa sau của ARR1
khác nếu (median1 <median2) {
trả về findMedianoftWosortedArrays (ARR1 + MINSIZE / 2, ARR2, N1 - MINSIZE / 2, N2);
}

// Nếu không, trung bình của hai mảng nằm trong nửa đầu của ARR2
khác {
Trả về findMedianoftWosortedArrays (ARR1, ARR2 + MINSIZE / 2, N1, N2 - MINSIZE / 2);
}
}

int main () {
// Xác định hai mảng được sắp xếp
int arr1 [] = {1, 3, 5, 7, 9};
int Arr2 [] = {2, 4, 6, 8, 10};

// Tìm trung bình của hai mảng
int median = findMedianoftWosortedArrays (ARR1, ARR2, sizeof (ARR1) / sizeof (ARR1 [0]), sizeof (ARR2) / sizeof (ARR2 [0]));

// In trung bình
cout << "Trung bình của hai mảng là" << Median << endl;

trả lại 0;
}
`` `

## 5 hashtags

* #C ++
* #phương tiện truyền thông
=======================================
#C++, #Median, #Sorted Arrays, #algorithms ## 4. Median of Two Sorted Arrays in C++

The median of a list of numbers is the middle value when the list is sorted in ascending order. For example, the median of the list [1, 3, 5, 7, 9] is 5.

Finding the median of two sorted arrays is a common problem in computer science. There are a few different algorithms that can be used to solve this problem, but the most efficient one is the **divide and conquer** algorithm.

The divide and conquer algorithm works by recursively splitting the two arrays into smaller and smaller sub-arrays until each sub-array contains only one element. The median of the two arrays is then the average of the medians of the two sub-arrays.

Here is an implementation of the divide and conquer algorithm in C++:

```c++
#include <iostream>

using namespace std;

// Function to find the median of a sorted array
int findMedian(int arr[], int n) {
// If the array has an odd number of elements, the median is the middle element
if (n % 2 == 1) {
return arr[n / 2];
}

// If the array has an even number of elements, the median is the average of the two middle elements
else {
return (arr[n / 2 - 1] + arr[n / 2]) / 2;
}
}

// Function to find the median of two sorted arrays
int findMedianOfTwoSortedArrays(int arr1[], int arr2[], int n1, int n2) {
// If one of the arrays is empty, the median is the other array's median
if (n1 == 0) {
return findMedian(arr2, n2);
} else if (n2 == 0) {
return findMedian(arr1, n1);
}

// Find the smaller of the two array sizes
int minSize = min(n1, n2);

// Recursively find the medians of the two sub-arrays
int median1 = findMedian(arr1, minSize / 2);
int median2 = findMedian(arr2, minSize / 2);

// If the two medians are equal, the median of the two arrays is the same
if (median1 == median2) {
return median1;
}

// If the first median is less than the second median, the median of the two arrays is in the second half of arr1
else if (median1 < median2) {
return findMedianOfTwoSortedArrays(arr1 + minSize / 2, arr2, n1 - minSize / 2, n2);
}

// Otherwise, the median of the two arrays is in the first half of arr2
else {
return findMedianOfTwoSortedArrays(arr1, arr2 + minSize / 2, n1, n2 - minSize / 2);
}
}

int main() {
// Define two sorted arrays
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {2, 4, 6, 8, 10};

// Find the median of the two arrays
int median = findMedianOfTwoSortedArrays(arr1, arr2, sizeof(arr1) / sizeof(arr1[0]), sizeof(arr2) / sizeof(arr2[0]));

// Print the median
cout << "The median of the two arrays is " << median << endl;

return 0;
}
```

## 5 Hashtags

* #C++
* #Media
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top