Share quick sort c++ code

tinybird673

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

Sắp xếp nhanh là một thuật toán sắp xếp thường được sử dụng trong thực tế vì hiệu quả của nó.Nó là một thuật toán phân chia và chinh phục, có nghĩa là nó chia mảng đầu vào thành các mảng con nhỏ hơn, sắp xếp các mảng con, và sau đó hợp nhất các mép con được sắp xếp để tạo thành mảng đầu ra được sắp xếp.

Mã giả để sắp xếp nhanh như sau:

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

// Đầu vào: Một mảng số nguyên, `arr`
// Đầu ra: Mảng được sắp xếp

chức năng Quicksort (mảng) {
// trường hợp cơ sở: nếu mảng trống hoặc chỉ có một yếu tố,
// Sau đó, nó đã được sắp xếp

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

// chọn phần tử xoay vòng

Đặt pivot = mảng [0];

// phân vùng mảng thành hai mảng con:
// - mảng con bên trái, chứa tất cả các yếu tố ít hơn trục
// - mép con bên phải, chứa tất cả các yếu tố lớn hơn hoặc bằng trục

Đặt bên trái = [];
Để đúng = [];

for (let i = 1; i <arr.length; i ++) {
if (mảng <pivot) {
trái.push (mảng );
} khác {
phải.push (mảng );
}
}

// Sắp xếp đệ quy các khớp phụ trái và phải

trái = Quicksort (trái);
Phải = Quicksort (phải);

// Hợp nhất các mảng con được sắp xếp để tạo ra mảng đầu ra được sắp xếp

để sắp xếp = [];

sort = sorted.concat (trái);
Sắp xếp = Sắp xếp.concat (Pivot);
sort = sorted.concat (phải);

// Trả lại mảng được sắp xếp

trả lại được sắp xếp;
}
`` `

Dưới đây là một ví dụ về cách sắp xếp nhanh hoạt động trên mảng `[5, 3, 1, 2, 4]`.

1. Bước đầu tiên là chọn phần tử trục.Trong trường hợp này, chúng tôi sẽ chọn phần tử đầu tiên, là 5.
2. Sau đó, chúng tôi phân vùng mảng thành hai mảng con:
- mép con bên trái, chứa tất cả các phần tử nhỏ hơn trục (trong trường hợp này, `[3, 1, 2]`)
- mép con bên phải, chứa tất cả các phần tử lớn hơn hoặc bằng trục (trong trường hợp này, `[4]`)
3. Sau đó, chúng tôi sắp xếp đệ quy các khớp phụ trái và phải.
4. Chúng tôi hợp nhất các mép con được sắp xếp để tạo thành mảng đầu ra được sắp xếp.

Mảng đầu ra được sắp xếp là `[1, 2, 3, 4, 5]`.

Sắp xếp nhanh 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, loại nhanh 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.

## hashtags

* #sắp xếp nhanh chóng
* #Sorting
* #C ++
* #algorithms
* #cấu trúc dữ liệu
=======================================
## Quick Sort in C++

Quick sort is a sorting algorithm that is often used in practice because of its efficiency. It is a divide-and-conquer algorithm, which means that it divides the input array into smaller sub-arrays, sorts the sub-arrays, and then merges the sorted sub-arrays to form the sorted output array.

The pseudocode for quick sort is as follows:

```
// Quick sort algorithm

// Input: an array of integers, `arr`
// Output: the sorted array

function quickSort(arr) {
// Base case: if the array is empty or has only one element,
// then it is already sorted

if (arr.length <= 1) {
return arr;
}

// Choose the pivot element

let pivot = arr[0];

// Partition the array into two sub-arrays:
// - the left sub-array, which contains all elements less than the pivot
// - the right sub-array, which contains all elements greater than or equal to the pivot

let left = [];
let right = [];

for (let i = 1; i < arr.length; i++) {
if (arr < pivot) {
left.push(arr);
} else {
right.push(arr);
}
}

// Recursively sort the left and right sub-arrays

left = quickSort(left);
right = quickSort(right);

// Merge the sorted sub-arrays to form the sorted output array

let sorted = [];

sorted = sorted.concat(left);
sorted = sorted.concat(pivot);
sorted = sorted.concat(right);

// Return the sorted array

return sorted;
}
```

Here is an example of how quick sort works on the array `[5, 3, 1, 2, 4]`.

1. The first step is to choose the pivot element. In this case, we will choose the first element, which is 5.
2. We then partition the array into two sub-arrays:
- the left sub-array, which contains all elements less than the pivot (in this case, `[3, 1, 2]`)
- the right sub-array, which contains all elements greater than or equal to the pivot (in this case, `[4]`)
3. We then recursively sort the left and right sub-arrays.
4. We merge the sorted sub-arrays to form the sorted output array.

The sorted output array is `[1, 2, 3, 4, 5]`.

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

## Hashtags

* #quicksort
* #Sorting
* #C++
* #algorithms
* #data-structures
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top