Share c++ qsort

happyladybug692

New member
#C ++ #Sorting #AlGorithM #C ++ Tham chiếu #C ++ Hướng dẫn ## C ++ QSORT

** C ++ QSORT: Triển khai Quicksort **

Quicksort là một thuật toán sắp xếp vừa hiệu quả vừa đơn giản để thực hiện.Nó hoạt động bằng cách phân vùng dữ liệu thành hai phần, một phần nhỏ hơn phần tử trục và một phần lớn hơn phần tử trục.Quá trình này được lặp lại đệ quy cho đến khi dữ liệu được sắp xếp.

Việc triển khai C ++ của QuickSort được hiển thị bên dưới.

`` `C ++
#include <Istream>

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

// Hàm này phân vùng dữ liệu thành hai phần, một phần nhỏ hơn phần tử trục và một phần lớn hơn phần tử trục.
Int phân vùng (int mảng [], int low, int cao) {
// Chọn phần tử cuối cùng là phần tử trục.
int pivot = mảng [cao];

// Khởi tạo các chỉ số của hai Subarrays.
int i = thấp;
int j = thấp;

// lặp qua mảng, bắt đầu từ phần tử thứ hai.
for (int k = low; k <cao; k ++) {
// Nếu phần tử hiện tại nhỏ hơn phần tử trục, hãy trao đổi nó với phần tử tại Index i.
if (mảng [k] <pivot) {
hoán đổi (mảng [k], mảng );
i ++;
}
}

// Trao đổi phần tử trục với phần tử tại INDEX i.
hoán đổi (mảng , mảng [cao]);

// Trả về chỉ số của phần tử xoay.
trả lại tôi;
}

// Hàm này sắp xếp dữ liệu bằng QuickSort.
void QuickSort (int mảng [], int low, int cao) {
// Nếu mảng có nhiều hơn một phần tử, hãy phân vùng dữ liệu.
if (thấp <cao) {
// Nhận chỉ số của phần tử trục.
int pivot = phân vùng (mảng, thấp, cao);

// Sắp xếp các phần tử bên trái của phần tử trục.
Quicksort (mảng, thấp, trục - 1);

// Sắp xếp các yếu tố ở bên phải của phần tử trục.
Quicksort (mảng, trục + 1, cao);
}
}

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

// Nhận kích thước của mảng.
int n = sizeof (mảng) / sizeof (mảng [0]);

// Sắp xếp mảng bằng QuickSort.
Quicksort (mảng, 0, n - 1);

// In mảng được sắp xếp.
for (int i = 0; i <n; i ++) {
cout << mảng << "";
}

cout << endl;

trả lại 0;
}
`` `

**Hiệu suất**

Quicksort là một thuật toán sắp xếp rất hiệu quả.Độ phức tạp thời gian trung bình của nó là O (n log n), giống như sắp xếp hợp nhất.Tuy nhiên, Quicksort thường nhanh hơn so với phân loại hợp nhất trong thực tế vì nó có yếu tố không đổi nhỏ hơn.

** So sánh với các thuật toán sắp xếp khác **

Quicksort thường được so sánh với Sắp xếp hợp nhất, đây là một thuật toán sắp xếp hiệu quả khác.Sắp xếp hợp nhất có độ phức tạp thời gian trong trường hợp xấu nhất của O (n log n), giống như Quicksort.Tuy nhiên, Merge sắp xếp có hệ số không đổi lớn hơn Quicksort, điều đó có nghĩa là nó thường chậm hơn so với Quicksort trong thực tế.

**Người giới thiệu**

* [Quicksort trên Wikipedia] (Quicksort - Wikipedia)
* [C ++ Hướng dẫn Quicksort] (https://www.tutorialspoint.com/cplusplus/cpp_quicksort.htm)
* [C ++ Tham khảo Quicksort] (https://www.cplusplus.com/reference/algorithm/sort/)
=======================================
#C++ #Sorting #AlGorithM #C++ Reference #C++ Tutorial ##C++ qsort

**C++ qsort: A Quicksort Implementation**

Quicksort is a sorting algorithm that is both efficient and simple to implement. It works by partitioning the data into two parts, one that is less than the pivot element and one that is greater than the pivot element. This process is repeated recursively until the data is sorted.

The C++ implementation of quicksort is shown below.

```c++
#include <iostream>

using namespace std;

// This function partitions the data into two parts, one that is less than the pivot element and one that is greater than the pivot element.
int partition(int arr[], int low, int high) {
// Choose the last element as the pivot element.
int pivot = arr[high];

// Initialize the indices of the two subarrays.
int i = low;
int j = low;

// Iterate through the array, starting from the second element.
for (int k = low; k < high; k++) {
// If the current element is less than the pivot element, swap it with the element at index i.
if (arr[k] < pivot) {
swap(arr[k], arr);
i++;
}
}

// Swap the pivot element with the element at index i.
swap(arr, arr[high]);

// Return the index of the pivot element.
return i;
}

// This function sorts the data using quicksort.
void quicksort(int arr[], int low, int high) {
// If the array has more than one element, partition the data.
if (low < high) {
// Get the index of the pivot element.
int pivot = partition(arr, low, high);

// Sort the elements to the left of the pivot element.
quicksort(arr, low, pivot - 1);

// Sort the elements to the right of the pivot element.
quicksort(arr, pivot + 1, high);
}
}

int main() {
// Create an array of integers.
int arr[] = {10, 7, 8, 9, 1, 5, 2, 4, 6, 3};

// Get the size of the array.
int n = sizeof(arr) / sizeof(arr[0]);

// Sort the array using quicksort.
quicksort(arr, 0, n - 1);

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

cout << endl;

return 0;
}
```

**Performance**

Quicksort is a very efficient sorting algorithm. Its average time complexity is O(n log n), which is the same as merge sort. However, quicksort is often faster than merge sort in practice because it has a smaller constant factor.

**Comparison with Other Sorting Algorithms**

Quicksort is often compared to merge sort, which is another efficient sorting algorithm. Merge sort has a worst-case time complexity of O(n log n), which is the same as quicksort. However, merge sort has a larger constant factor than quicksort, which means that it is often slower than quicksort in practice.

**References**

* [Quicksort on Wikipedia](https://en.wikipedia.org/wiki/Quicksort)
* [C++ Quicksort Tutorial](https://www.tutorialspoint.com/cplusplus/cpp_quicksort.htm)
* [C++ Quicksort Reference](https://www.cplusplus.com/reference/algorithm/sort/)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top