Share quick sort c++

#Quick Sắp xếp #C ++ #Sorting #algorithms #data Structures

## Sắp xếp nhanh trong C ++

Sắp xếp nhanh là một thuật toán phân chia và chinh phục để sắp xếp.Nó hoạt động bằng cách phân vùng mảng thành hai mảng con, một trong số đó được sắp xếp.Thuật toán sau đó sắp xếp đệ quy hai mảng con.

Sắp xếp nhanh là một thuật toán sắp xếp rất hiệu quả và là một trong những thuật toán sắp xếp được sử dụng phổ biến nhất trong thực tế.Nó là một thuật toán sắp xếp tại chỗ, có nghĩa là nó không yêu cầu thêm không gian để sắp xếp mảng.

Độ phức tạp của thời gian của loại nhanh là O (n log n) trong trường hợp tốt nhất, O (n^2) trong trường hợp xấu nhất và trung bình O (n log n).Độ phức tạp không gian của loại nhanh là O (log n) trong trường hợp tốt nhất và O (n) trong trường hợp xấu nhất.

### Thuật toán sắp xếp nhanh

Thuật toán Sắp xếp nhanh hoạt động bằng cách phân vùng đệ quy mảng thành hai mảng con.Mảng con đầu tiên chứa tất cả các phần tử nhỏ hơn phần tử trục và mép con thứ hai chứa tất cả các phần tử lớn hơn phần tử trục.

Phần tử pivot được chọn làm phần tử đầu tiên trong mảng.Thuật toán sau đó phân vùng mảng thành hai phần phụ bằng cách hoán đổi các phần tử trong mảng saoPhần tử được di chuyển sang phải của phần tử trục.

Quá trình này được lặp lại đệ quy trên hai mảng con cho đến khi mảng được sắp xếp.

### Ví dụ

Sau đây là một ví dụ về cách thuật toán sắp xếp nhanh sẽ sắp xếp mảng `[5, 3, 1, 2, 4]`.

1. Phần tử đầu tiên trong mảng được chọn làm phần tử trục, là `5`.
2. Các phần tử trong mảng được hoán đổi sao cho tất cả các phần tử nhỏ hơn phần tử trục được di chuyển sang bên trái của phần tử trục và tất cả các phần tử lớn hơn phần tử trục được di chuyển sang bên phải của trụcyếu tố.

`` `
[5, 3, 1, 2, 4]
[3, 1, 2, 4, 5]
`` `

3. Thuật toán sau đó được áp dụng đệ quy cho hai mép con `[3, 1, 2]` và `[4, 5]`.

`` `
[3, 1, 2]
[1, 2, 3]

[4, 5]
[4, 5]
`` `

4. Mảng hiện được sắp xếp.

### Thực hiện

Sau đây là việc triển khai thuật toán sắp xếp nhanh trong C ++.

`` `C ++
#include <Istream>

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

// Hàm này phân vùng mảng thành hai mảng con, một trong số đó được sắp xếp.
Int phân vùng (int mảng [], int low, int cao) {
// Chọn phần tử đầu tiên là phần tử trục.
int pivot = mảng [thấp];

// lặp qua mảng, các phần tử hoán đổi để tất cả các phần tử nhỏ hơn phần tử trục được di chuyển sang bên trái của phần tử trục và tất cả các phần tử lớn hơn phần tử trục được di chuyển sang bên phải của trụcyếu tố.
int i = thấp + 1;
for (int j = low+1; j <= high; j ++) {
if (mảng [j] <pivot) {
hoán đổi (mảng , mảng [j]);
i ++;
}
}

// Di chuyển phần tử trục đến vị trí cuối cùng của nó trong mảng được sắp xếp.
hoán đổi (mảng [thấp], mảng [i - 1]);

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

// Hàm này sắp xếp đệ quy các mảng.
void QuickSort (int mảng [], int low, int cao) {
// Nếu mảng trống hoặc chỉ có một yếu tố, nó đã được sắp xếp.
=======================================
#Quick Sort #C++ #Sorting #algorithms #data Structures

## Quick Sort in C++

Quick sort is a divide-and-conquer algorithm for sorting. It works by partitioning the array into two sub-arrays, one of which is sorted. The algorithm then recursively sorts the two sub-arrays.

Quick sort is a very efficient sorting algorithm and is one of the most commonly used sorting algorithms in practice. It is an in-place sorting algorithm, which means that it does not require any extra space to sort the array.

The time complexity of quick sort is O(n log n) in the best case, O(n^2) in the worst case, and O(n log n) on average. The space complexity of quick sort is O(log n) in the best case and O(n) in the worst case.

### Quick Sort Algorithm

The quick sort algorithm works by recursively partitioning the array into two sub-arrays. The first sub-array contains all the elements that are less than the pivot element, and the second sub-array contains all the elements that are greater than the pivot element.

The pivot element is chosen as the first element in the array. The algorithm then partitions the array into two sub-arrays by swapping the elements in the array so that all the elements that are less than the pivot element are moved to the left of the pivot element, and all the elements that are greater than the pivot element are moved to the right of the pivot element.

This process is repeated recursively on the two sub-arrays until the array is sorted.

### Example

The following is an example of how the quick sort algorithm would sort the array `[5, 3, 1, 2, 4]`.

1. The first element in the array is chosen as the pivot element, which is `5`.
2. The elements in the array are swapped so that all the elements that are less than the pivot element are moved to the left of the pivot element, and all the elements that are greater than the pivot element are moved to the right of the pivot element.

```
[5, 3, 1, 2, 4]
[3, 1, 2, 4, 5]
```

3. The algorithm is then recursively applied to the two sub-arrays `[3, 1, 2]` and `[4, 5]`.

```
[3, 1, 2]
[1, 2, 3]

[4, 5]
[4, 5]
```

4. The array is now sorted.

### Implementation

The following is an implementation of the quick sort algorithm in C++.

```c++
#include <iostream>

using namespace std;

// This function partitions the array into two sub-arrays, one of which is sorted.
int partition(int arr[], int low, int high) {
// Choose the first element as the pivot element.
int pivot = arr[low];

// Iterate through the array, swapping elements so that all the elements that are less than the pivot element are moved to the left of the pivot element, and all the elements that are greater than the pivot element are moved to the right of the pivot element.
int i = low + 1;
for (int j = low + 1; j <= high; j++) {
if (arr[j] < pivot) {
swap(arr, arr[j]);
i++;
}
}

// Move the pivot element to its final position in the sorted array.
swap(arr[low], arr[i - 1]);

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

// This function recursively sorts the array.
void quickSort(int arr[], int low, int high) {
// If the array is empty or has only one element, it is already sorted.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top