Share insertion sort c++,

vylamlekieu

New member
#C ++, #Sorting, #InserSh Sắp xếp, #Time phức tạp, #AlGorithM ## Sắp xếp chèn trong C ++

Sắp xếp chèn là một thuật toán sắp xếp đơn giản hoạt động bằng cách liên tục chèn phần tử tiếp theo vào vị trí chính xác trong một danh sách được sắp xếp.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 dữ liệu.

Sắp xếp chèn là một trong những thuật toán sắp xếp đơn giản nhất, nhưng nó cũng là một trong những thuật toán ít hiệu quả nhất.Độ phức tạp về thời gian của nó là O (n^2), có nghĩa là cần có thời gian bậc hai để sắp xếp một danh sách các yếu tố N.Tuy nhiên, loại chèn thường được sử dụng như một công cụ giảng dạy vì nó dễ hiểu và thực hiện.

### Thuật toán

Thuật toán sắp xếp chèn hoạt động bằng cách liên tục chèn phần tử tiếp theo vào vị trí chính xác trong 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 đó nó thêm 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 cùng một lúc.

Để chèn một phần tử vào danh sách được sắp xếp, thuật toán bắt đầu bằng cách so sánh phần tử mới với phần tử đầu tiên trong danh sách được sắp xếp.Nếu phần tử mới nhỏ hơn phần tử đầu tiên, thì nó được chèn trước phần tử đầu tiên.Mặt khác, thuật toán tiếp tục so sánh phần tử mới với phần tử tiếp theo trong danh sách được sắp xếp.Quá trình này tiếp tục cho đến khi phần tử mới được chèn vào vị trí chính xác trong danh sách được sắp xếp.

### Độ phức tạp thời gian

Độ phức tạp thời gian của loại chèn là O (n^2).Điều này có nghĩa là thuật toán dành thời gian bậc hai để sắp xếp một danh sách các yếu tố N.Độ phức tạp của thời gian bậc hai của loại chèn là do thực tế là thuật toán phải so sánh từng yếu tố của danh sách chưa được phân loại với mọi yếu tố của danh sách được sắp xếp.

### Độ phức tạp không gian

Độ phức tạp không gian của loại chèn là O (1).Điều này có nghĩa là thuật toán không yêu cầu thêm không gian để sắp xếp dữ liệu.Thuật toán chỉ yêu cầu một biến tạm thời để lưu trữ phần tử đang được chèn vào danh sách được sắp xếp.

### Các ứng dụng

Sắp xếp chèn thường được sử dụng để sắp xếp danh sách nhỏ dữ liệu.Nó cũng được sử dụng như một chương trình con trong các thuật toán sắp xếp khác, chẳng hạn như sắp xếp hợp nhất và sắp xếp nhanh.

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

* [Sắp xếp chèn - Wikipedia] (Insertion sort - Wikipedia)
* [Sắp xếp chèn - GeekSforGeek] (geeksforgeek.org - geeksforgeek Resources and Information.)
* [Sắp xếp chèn - Hướng dẫn] (https://www.tutorialspoint.com/data_structure_algorithms/inserts_sort.htm)
=======================================
#C++, #Sorting, #Insertion Sort, #Time Complexity, #AlGorithM ## Insertion Sort in C++

Insertion sort is a simple sorting algorithm that works by repeatedly inserting the next element into the correct position in a sorted list. It is an in-place sorting algorithm, meaning that it does not require any extra space to sort the data.

Insertion sort is one of the simplest sorting algorithms, but it is also one of the least efficient. Its time complexity is O(n^2), which means that it takes quadratic time to sort a list of n elements. However, insertion sort is often used as a teaching tool because it is easy to understand and implement.

### Algorithm

The insertion sort algorithm works by repeatedly inserting the next element into the correct position in a sorted list. The algorithm starts with an empty list, and then it adds each element of the unsorted list to the sorted list one at a time.

To insert an element into the sorted list, the algorithm starts by comparing the new element to the first element in the sorted list. If the new element is less than the first element, then it is inserted before the first element. Otherwise, the algorithm continues to compare the new element to the next element in the sorted list. This process continues until the new element is inserted into the correct position in the sorted list.

### Time Complexity

The time complexity of insertion sort is O(n^2). This means that the algorithm takes quadratic time to sort a list of n elements. The quadratic time complexity of insertion sort is due to the fact that the algorithm must compare each element of the unsorted list to every element of the sorted list.

### Space Complexity

The space complexity of insertion sort is O(1). This means that the algorithm does not require any extra space to sort the data. The algorithm only requires a temporary variable to store the element that is being inserted into the sorted list.

### Applications

Insertion sort is often used to sort small lists of data. It is also used as a subroutine in other sorting algorithms, such as merge sort and quick sort.

### References

* [Insertion Sort - Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)
* [Insertion Sort - GeeksforGeeks](https://www.geeksforgeeks.org/insertion-sort/)
* [Insertion Sort - TutorialsPoint](https://www.tutorialspoint.com/data_structures_algorithms/insertion_sort.htm)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top