Share c++ sort source code

blackrabbit723

New member
### C ++ Sắp xếp mã nguồn

** 1.Giới thiệu**

Sắp xếp là một hoạt động cơ bản trong khoa học máy tính.Nó được sử dụng để sắp xếp dữ liệu theo một thứ tự cụ thể, chẳng hạn như tăng dần hoặc giảm dần.Có nhiều thuật toán sắp xếp khác nhau có sẵn, mỗi thuật toán có ưu điểm và nhược điểm riêng.Trong bài viết này, chúng tôi sẽ thảo luận về mã nguồn sắp xếp C ++ cho các thuật toán sắp xếp sau:

* Sắp xếp bong bóng
* Sắp xếp lựa chọn
* Sắp xếp chèn
* Hợp nhất sắp xếp
* Sắp xếp nhanh chóng

** 2.Sắp xếp bong bóng **

Sắp xếp bong bóng là một thuật toán sắp xếp đơn giản hoạt động bằng cách so sánh liên tục các yếu tố liền kề và hoán đổi chúng nếu chúng không đúng thứ tự.Thuật toán tiếp tục cho đến khi tất cả các yếu tố theo đúng thứ tự.Sắp xếp bong bóng không phải là một thuật toán sắp xếp rất hiệu quả, nhưng nó rất đơn giản để thực hiện.Mã nguồn sắp xếp C ++ cho Sắp xếp bong bóng được hiển thị bên dưới:

`` `C ++
#include <Istream>

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

void bongbbledort (int mảng [], int n) {
for (int i = 0; i <n - 1; i ++) {
for (int j = 0; j <n - i - 1; j ++) {
if (mảng [j]> mảng [j + 1]) {
int temp = mảng [j];
mảng [j] = mảng [j + 1];
mảng [j + 1] = temp;
}
}
}
}

int main () {
int mảng [] = {10, 5, 2, 4, 7, 1, 8, 9, 6, 3};
int n = sizeof (mảng) / sizeof (mảng [0]);

bong bóng (mảng, n);

for (int i = 0; i <n; i ++) {
cout << mảng << "";
}

cout << endl;

trả lại 0;
}
`` `

** 3.Sắp xếp lựa chọn **

Loại lựa chọn là một thuật toán sắp xếp hoạt động bằng cách liên tục tìm phần tử nhỏ nhất trong phần chưa được phân loại của mảng và hoán đổi nó với phần tử đầu tiên trong phần chưa được phân loại.Thuật toán tiếp tục cho đến khi tất cả các yếu tố được sắp xếp.Sắp xếp lựa chọn là một thuật toán sắp xếp hiệu quả hơn so với loại bong bóng, nhưng nó vẫn không hiệu quả lắm.Mã nguồn sắp xếp C ++ để sắp xếp lựa chọn được hiển thị bên dưới:

`` `C ++
#include <Istream>

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

void lựa chọnSort (int mảng [], int n) {
for (int i = 0; i <n - 1; i ++) {
int minindex = i;
for (int j = i+1; j <n; j ++) {
if (mảng [j] <mảng [minindex]) {
minindex = j;
}
}

int temp = mảng ;
mảng = mảng [minindex];
mảng [minindex] = temp;
}
}

int main () {
int mảng [] = {10, 5, 2, 4, 7, 1, 8, 9, 6, 3};
int n = sizeof (mảng) / sizeof (mảng [0]);

Lựa chọn (mảng, n);

for (int i = 0; i <n; i ++) {
cout << mảng << "";
}

cout << endl;

trả lại 0;
}
`` `

**4.Sắp xếp chèn**

Sắp xếp chèn là một thuật toán sắp xếp hoạt động bằng cách liên tục chèn các phần tử vào một danh sách được sắp xếp.Thuật toán bắt đầu với một danh sách trống và sau đó chèn từng phần tử của danh sách chưa được phân loại vào danh sách được sắp xếp ở vị trí chính xác.Sắp xếp chèn là một thuật toán sắp xếp hiệu quả hơn so với sắp xếp và phân loại bong bóng, nhưng nó vẫn không hiệu quả lắm.Mã nguồn sắp xếp C ++ để sắp xếp chèn được hiển thị bên dưới:

`` `C ++
#include <Istream>

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

Vô hiệu
=======================================
### C++ Sort Source Code

**1. Introduction**

Sorting is a fundamental operation in computer science. It is used to arrange data in a specific order, such as ascending or descending. There are many different sorting algorithms available, each with its own advantages and disadvantages. In this article, we will discuss the C++ sort source code for the following sorting algorithms:

* Bubble sort
* Selection sort
* Insertion sort
* Merge sort
* Quick sort

**2. Bubble Sort**

Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. The algorithm continues until all elements are in the correct order. Bubble sort is not a very efficient sorting algorithm, but it is simple to implement. The C++ sort source code for bubble sort is shown below:

```c++
#include <iostream>

using namespace std;

void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {10, 5, 2, 4, 7, 1, 8, 9, 6, 3};
int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

for (int i = 0; i < n; i++) {
cout << arr << " ";
}

cout << endl;

return 0;
}
```

**3. Selection Sort**

Selection sort is a sorting algorithm that works by repeatedly finding the smallest element in the unsorted part of the array and swapping it with the first element in the unsorted part. The algorithm continues until all elements are sorted. Selection sort is a more efficient sorting algorithm than bubble sort, but it is still not very efficient. The C++ sort source code for selection sort is shown below:

```c++
#include <iostream>

using namespace std;

void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

int temp = arr;
arr = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int arr[] = {10, 5, 2, 4, 7, 1, 8, 9, 6, 3};
int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, n);

for (int i = 0; i < n; i++) {
cout << arr << " ";
}

cout << endl;

return 0;
}
```

**4. Insertion Sort**

Insertion sort is a sorting algorithm that works by repeatedly inserting elements into a sorted list. The algorithm starts with an empty list and then inserts each element of the unsorted list into the sorted list in the correct position. Insertion sort is a more efficient sorting algorithm than bubble sort and selection sort, but it is still not very efficient. The C++ sort source code for insertion sort is shown below:

```c++
#include <iostream>

using namespace std;

void
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top