Merge pull request #385 from caitlingao/feat-38-divide-and-conquer

feat(geektime_algo): add 38 divide and conquer
This commit is contained in:
wangzheng0822 2019-08-26 08:15:56 +08:00 committed by GitHub
commit 4471160c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,57 @@
fn merge_sort_count(mut nums: Vec<i32>) -> i32 {
let mut count = 0;
let n = nums.len() - 1;
merge_sort(&mut nums, 0, n, &mut count);
count
}
fn merge_sort(nums: &mut Vec<i32>, low: usize, high: usize, count: &mut i32) {
if low >= high { return; }
let middle = low + ((high - low) >> 1);
merge_sort(nums, low, middle, count);
merge_sort(nums, middle+1, high, count);
merge(nums, low, middle, high, count);
}
fn merge(nums: &mut Vec<i32>,
low: usize,
middle: usize,
high: usize,
count: &mut i32) {
let mut i = low;
let mut j = middle + 1;
let mut tmp = vec![];
while i <= middle && j <= high {
if nums[i] <= nums[j] {
tmp.push(nums[i]);
i += 1;
} else {
// count += &(middle - i + 1);
*count += (middle - i + 1) as i32;
tmp.push(nums[j]);
j += 1;
}
}
while i <= middle {
tmp.push(nums[i]);
i += 1;
}
while j <= high {
tmp.push(nums[j]);
j += 1;
}
for i in 0..=(high-low) {
nums[low+1] = tmp[i];
}
}
fn main() {
let nums = vec![1, 5, 6, 2, 3, 4];
let m = merge_sort_count(nums);
println!("{:?}", m);
}