Share quick sort c#

tranan24682468

New member
#Quick sort #c ##sorting #algorithms #data Cấu trúc ## 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 khoa học máy tính.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 khớp con, và sau đó hợp nhất các máng con được sắp xếp lại với nhau để tạo thành một mảng được sắp xếp.

Sắp xếp nhanh là một thuật toán sắp xếp rất hiệu quả và nó thường nhanh hơn các thuật toán sắp xếp khác, chẳng hạn như phân loại hợp nhất và sắp xếp đống.Tuy nhiên, sắp xếp nhanh có thể không hiệu quả trên các mảng đã được sắp xếp hoặc gần như được sắp xếp.

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

`` `
chức năng QuickSort (mảng, trái, phải) {
if (trái <phải) {
// Chọn một yếu tố xoay vòng.
pivotindex = phân vùng (mảng, trái, phải);

// Sắp xếp các mảng phụ bên trái.
Quicksort (mảng, trái, pivotindex - 1);

// Sắp xếp đúng mảng con.
QuickSort (mảng, pivotindex + 1, phải);
}
}

Phân vùng chức năng (mảng, trái, phải) {
// Chọn phần tử Pivot là phần tử cuối cùng trong mảng.
Pivot = mảng [phải];

// Khởi tạo các con trỏ trái và phải.
LeftPulum = trái;
RightPulum = Right - 1;

// Trong khi con trỏ bên trái nhỏ hơn con trỏ bên phải,
// So sánh các phần tử ở con trỏ bên trái và con trỏ bên phải.
// Nếu phần tử ở con trỏ bên trái nhỏ hơn phần tử trục,
// Tăng con trỏ bên trái.
// Nếu phần tử ở con trỏ bên phải lớn hơn phần tử trục,
// Giảm con trỏ bên phải.
// Nếu các phần tử ở con trỏ bên trái và con trỏ bên phải bằng nhau,
// không làm gì cả.
WHER
if (mảng [retlegpulum] <pivot) {
LeftPulum ++;
} if if (mảng [mảng
RightPulum--;
} khác {
// Các phần tử ở con trỏ bên trái và con trỏ bên phải bằng nhau.
// Không làm gì cả.
}
}

// Trao đổi các phần tử ở con trỏ bên trái và con trỏ bên phải.
mảng [leftPulum] = mảng [roothpulum];
Mảng [RightPulum] = Pivot;

// Trả về chỉ số của phần tử xoay.
Trả về LeftPulum;
}
`` `

Dưới đây là một ví dụ về cách sắp xếp nhanh hoạt động trên mảng sau:

`` `
[8, 4, 7, 9, 2, 6, 1, 5]
`` `

1. Bước đầu tiên là chọn một phần tử trục.Trong trường hợp này, chúng tôi sẽ chọn phần tử cuối cùng trong mảng, là 9.
2. Sau đó, chúng tôi khởi tạo các con trỏ trái và phải vào các phần tử đầu tiên và cuối cùng trong mảng, tương ứng.
3. Sau đó, chúng tôi so sánh các phần tử ở con trỏ bên trái và con trỏ bên phải.Trong trường hợp này, 8 là ít hơn 9, vì vậy chúng tôi tăng con trỏ bên trái.
4. Sau đó, chúng tôi so sánh các phần tử ở con trỏ bên phải và con trỏ bên trái.Trong trường hợp này, 9 là lớn hơn 2, vì vậy chúng tôi giảm đúng con trỏ.
5. Sau đó, chúng tôi lặp lại các bước 3 và 4 cho đến khi con trỏ bên trái bằng con trỏ bên phải.
6. Tại thời điểm này, con trỏ bên trái đang chỉ vào phần tử 6, nhỏ hơn phần tử trục.Chúng tôi trao đổi các phần tử ở con trỏ bên trái và phần tử trục.
7. Phần tử trục hiện đang ở vị trí chính xác và chúng ta có thể sắp xếp đệ quy các nòng phụ trái và phải.

Sau đây là mảng được sắp xếp:

`` `
[1, 2, 4, 5, 6, 7, 8, 9]
`` `

## hashtags

* #Sắp xếp nhanh chóng
* #C#
* #Sorting
* #algorithms
*
=======================================
#Quick sort #C# #Sorting #algorithms #data structures ##Quick Sort in C#

Quick sort is a sorting algorithm that is often used in computer science. 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 back together to form a sorted array.

Quick sort is a very efficient sorting algorithm, and it is often faster than other sorting algorithms, such as merge sort and heap sort. However, quick sort can be inefficient on arrays that are already sorted or nearly sorted.

The pseudocode for quick sort is as follows:

```
function quickSort(array, left, right) {
if (left < right) {
// Choose a pivot element.
pivotIndex = partition(array, left, right);

// Sort the left sub-array.
quickSort(array, left, pivotIndex - 1);

// Sort the right sub-array.
quickSort(array, pivotIndex + 1, right);
}
}

function partition(array, left, right) {
// Choose the pivot element to be the last element in the array.
pivot = array
;

// Initialize the left and right pointers.
leftPointer = left;
rightPointer = right - 1;

// While the left pointer is less than the right pointer,
// compare the elements at the left pointer and the right pointer.
// If the element at the left pointer is less than the pivot element,
// increment the left pointer.
// If the element at the right pointer is greater than the pivot element,
// decrement the right pointer.
// If the elements at the left pointer and the right pointer are equal,
// do nothing.
while (leftPointer < rightPointer) {
if (array[leftPointer] < pivot) {
leftPointer++;
} else if (array[rightPointer] > pivot) {
rightPointer--;
} else {
// The elements at the left pointer and the right pointer are equal.
// Do nothing.
}
}

// Swap the elements at the left pointer and the right pointer.
array[leftPointer] = array[rightPointer];
array[rightPointer] = pivot;

// Return the index of the pivot element.
return leftPointer;
}
```

Here is an example of how quick sort works on the following array:

```
[8, 4, 7, 9, 2, 6, 1, 5]
```

1. The first step is to choose a pivot element. In this case, we will choose the last element in the array, which is 9.
2. We then initialize the left and right pointers to the first and last elements in the array, respectively.
3. We then compare the elements at the left pointer and the right pointer. In this case, 8 is less than 9, so we increment the left pointer.
4. We then compare the elements at the right pointer and the left pointer. In this case, 9 is greater than 2, so we decrement the right pointer.
5. We then repeat steps 3 and 4 until the left pointer is equal to the right pointer.
6. At this point, the left pointer is pointing to the element 6, which is less than the pivot element. We swap the elements at the left pointer and the pivot element.
7. The pivot element is now in the correct position, and we can recursively sort the left and right sub-arrays.

The following is the sorted array:

```
[1, 2, 4, 5, 6, 7, 8, 9]
```

## Hashtags

* #Quick sort
* #C#
* #Sorting
* #algorithms
*​
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top