# c++_两数组取中位数_快速排序

nums1 = [1, 3]
nums2 = [2]

nums1 = [1, 2]
nums2 = [3, 4]

#include <vector>
#include <iostream>
using namespace std;


class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len;
double Median_Num;

nums1.insert(nums1.end(), nums2.begin(), nums2.end());
len = nums1.size();
QKSort(nums1, 0, len-1);

for (int i = 0; i < len; i++)
{
cout << nums1[i];
cout << " ";
}
cout << endl;

if (len % 2 == 1)
{
Median_Num = nums1[len / 2];
return(Median_Num);
}
else
{
Median_Num = ((double)nums1[len / 2 - 1] + (double)nums1[len / 2]) / 2;
return(Median_Num);
}
}

void QKSort(vector<int>& r, int low, int high) {    //冒泡排序
if (low < high) {
int pos = QKPass(r, low, high);
QKSort(r, low, pos-1);
QKSort(r, pos + 1, high);
}
}

int QKPass(vector<int>& r, int left, int right) {
int x = r[left];
int low = left;
int high = right;

while (low < high)
{
while (low < high && r[high] >= x)
{
high--;
}
if (low < high)
{
r[low] = r[high];
low++;
}
while (low < high && r[low] < x)
{
low++;
}
if (low < high)
{
r[high] = r[low];
high--;
}
}
r[low] = x;
return low;
}
};


int main() {
Solution s;
vector<int> a = {1, 3};
vector<int> b = {2, 4};

double result = s.findMedianSortedArrays(a, b);
cout << result << endl;
return 0;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

林zZ

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-15 3万+
03-22 2093

07-08 1万+
05-06 134
10-10 201
04-12 131
03-26 2683
08-06 827
08-04 1889
10-18 9170