Share merge sort c++,

vietthitran

New member
#merge Sort, #C ++, #Sorting, #algorithms, #data Structures ## Merge Sắp xếp trong C ++

Sắp xếp hợp nhất là một thuật toán sắp xếp hoạt động bằng cách tách một nửa danh sách một nửa cho đến khi mỗi người phụ chứa một phần tử duy nhất.Hai người con được sắp xếp sau đó được hợp nhất với nhau để tạo thành một danh sách được sắp xếp duy nhất.Sắp xếp hợp nhất là thuật toán ** o (n log n) **, có nghĩa là độ phức tạp thời gian của nó tỷ lệ thuận với logarit của số lượng các phần tử trong danh sách.

### Pseudocode

Sau đây là mã giả để hợp nhất sắp xếp:

`` `
function merge_sort (mảng) {
if (mảng.length <= 1) {
trả lại mảng;
}

// Chia mảng làm đôi.
mid = mảng.length / 2;
trái = Merge_Sort (mảng.slice (0, mid));
Phải = Merge_Sort (Array.Slice (Mid));

// hợp nhất hai người con được sắp xếp thành một danh sách được sắp xếp.
hợp nhất = [];
while (trái.
if (trái [0] <phải [0]) {
hợp nhất.push (trái.shift ());
} khác {
hợp nhất.push (Right.shift ());
}
}

// Thêm bất kỳ yếu tố còn lại từ những người phụ bên trái hoặc bên phải.
hợp nhất = hợp nhất.concat (trái) .concat (phải);

trở lại sáp nhập;
}
`` `

### Triển khai trong C ++

Sau đây là việc thực hiện Sắp xếp hợp nhất trong C ++:

`` `C ++
#include <Istream>
#include <Vector>

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

// hợp nhất hai người phụ được sắp xếp thành một danh sách được sắp xếp.
Vector <Int> Merge (Vector <Int> Left, Vector <Int> Right) {
// Tạo một vectơ mới để lưu trữ các yếu tố được hợp nhất.
Vector <Int> sáp nhập;

// Lặp lại những người phụ bên trái và bên phải, và thêm phần tử nhỏ hơn vào danh sách hợp nhất.
while (left.size ()> 0 && right.size ()> 0) {
if (trái [0] <phải [0]) {
hợp nhất.push_back (trái.front ());
trái.erase (trái.begin ());
} khác {
hợp nhất.push_back (right.front ());
phải.erase (phải.begin ());
}
}

// Thêm bất kỳ yếu tố còn lại từ những người phụ bên trái hoặc bên phải.
hợp nhất = hợp nhất.concat (trái) .concat (phải);

trở lại sáp nhập;
}

// chia nhỏ một danh sách một nửa cho đến khi mỗi người phụ chứa một phần tử duy nhất.
Vector <Int> Merge_Sort (Vector <Int> mảng) {
// Nếu danh sách trống hoặc chứa một phần tử duy nhất, hãy trả lại danh sách.
if (mảng.size () <= 1) {
trả lại mảng;
}

// chia danh sách làm đôi.
mid = mảng.length / 2;
trái = Merge_Sort (mảng.slice (0, mid));
Phải = Merge_Sort (Array.Slice (Mid));

// hợp nhất hai người con được sắp xếp thành một danh sách được sắp xếp.
trả lại hợp nhất (trái, phải);
}

int main () {
// Tạo một vectơ số nguyên.
vector <int> mảng = {10, 5, 2, 7, 3, 1, 9, 4, 6, 8};

// Sắp xếp vector bằng cách sử dụng Sắp xếp hợp nhất.
mảng = merge_sort (mảng);

// In vector sắp xếp.
for (int i = 0; i <mảng.size (); i ++) {
cout << mảng << "";
}

cout << endl;

trả lại 0;
}
`` `

### Hiệu suất

Sắp xếp hợp nhất là một
=======================================
#merge Sort, #C++, #Sorting, #algorithms, #data Structures ## Merge Sort in C++

Merge sort is a sorting algorithm that works by recursively splitting a list in half until each sublist contains a single element. The two sorted sublists are then merged together to form a single sorted list. Merge sort is a **O(n log n)** algorithm, which means that its time complexity is proportional to the logarithm of the number of elements in the list.

### Pseudocode

The following is the pseudocode for merge sort:

```
function merge_sort(array) {
if (array.length <= 1) {
return array;
}

// Split the array in half.
mid = array.length / 2;
left = merge_sort(array.slice(0, mid));
right = merge_sort(array.slice(mid));

// Merge the two sorted sublists into a single sorted list.
merged = [];
while (left.length > 0 && right.length > 0) {
if (left[0] < right[0]) {
merged.push(left.shift());
} else {
merged.push(right.shift());
}
}

// Add any remaining elements from the left or right sublists.
merged = merged.concat(left).concat(right);

return merged;
}
```

### Implementation in C++

The following is an implementation of merge sort in C++:

```c++
#include <iostream>
#include <vector>

using namespace std;

// Merge two sorted sublists into a single sorted list.
vector<int> merge(vector<int> left, vector<int> right) {
// Create a new vector to store the merged elements.
vector<int> merged;

// Iterate over the left and right sublists, and add the smaller element to the merged list.
while (left.size() > 0 && right.size() > 0) {
if (left[0] < right[0]) {
merged.push_back(left.front());
left.erase(left.begin());
} else {
merged.push_back(right.front());
right.erase(right.begin());
}
}

// Add any remaining elements from the left or right sublists.
merged = merged.concat(left).concat(right);

return merged;
}

// Recursively split a list in half until each sublist contains a single element.
vector<int> merge_sort(vector<int> array) {
// If the list is empty or contains a single element, return the list.
if (array.size() <= 1) {
return array;
}

// Split the list in half.
mid = array.length / 2;
left = merge_sort(array.slice(0, mid));
right = merge_sort(array.slice(mid));

// Merge the two sorted sublists into a single sorted list.
return merge(left, right);
}

int main() {
// Create a vector of integers.
vector<int> array = {10, 5, 2, 7, 3, 1, 9, 4, 6, 8};

// Sort the vector using merge sort.
array = merge_sort(array);

// Print the sorted vector.
for (int i = 0; i < array.size(); i++) {
cout << array << " ";
}

cout << endl;

return 0;
}
```

### Performance

Merge sort is a
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top