Welcome to “Algorithms and the Beauty of Programming” ↑ pay attention to us!
This article was first published on the wechat official account “Beauty of Algorithms and Programming”. Please follow us and learn more about this series of articles in time.
1 Problem Description
The sorting algorithm is how to arrange the records according to the requirements. Sorting algorithm has received considerable attention in many fields, especially in the processing of large amounts of data. A good algorithm can save a lot of resources. Sorting is the most basic and common algorithm, in the process of programming algorithms are based on sorting algorithm transformation. And many algorithms rely on sorting, sorting before they can write.
2 Problem Analysis
Today, we will roughly explain bubble sort, select sort and insert sort JS code implementation.
3 Solution
The first is bubbling, “bubbling” as the name implies is to float the lighter bubbles in the water, and the heavier ones sink. If we think of arrays, we can think of smaller numbers floating up and larger numbers sinking down. The idea is to iterate through the array to be sorted, compare two adjacent elements in turn, and swap them if their order does not meet the criteria (e.g., from largest to smallest). The task of visiting an element is repeated until no neighboring elements need to be swapped, and the array is sorted. (Code below)