9.3.1 Most simple sort implementation
No matter what programming language you are learning, when you are learning about loops and arrays, a sort algorithm is usually introduced as an example, and this algorithm is generally a bubble sort. Not its name is very good, but that this algorithm is the simplest way of thinking, the most easy to understand. So, even though you may have learned to bubble sort, we started our sort trip from this algorithm.
Bubble sort (Bubble sort) is a sort of interchange, its basic idea is: 22 compare the keywords of adjacent records, if the reverse order is exchanged, until there is no reverse sequence of records. The bubble implementation can vary a lot in detail, and we'll explain the idea of bubble sequencing in 3 different bubble implementation codes, respectively. Here, let's take a look at some of the more easily understood.
/* To order table L as Exchange sort (Bubble sort primary edition)/
void BubbleSort0 (sqlist *l)
{
int i,j;
for (i=1;i<l->length;i++)
{for (
j=i+1;j<=l->length;j++)
{
if l->r[i]>l->r[ J])
{
swap (L,I,J);/* Exchange l->r[i] and L->r[j] value *
/
}}
}
This code, strictly speaking, is not a standard bubble sort algorithm, because it does not meet the "22 comparison adjacent record" bubble sort idea, it should be the most simple exchange sort. Its idea is to let each keyword, and each of the following keyword comparison, if the large exchange, so that the first position of the keyword after a cycle must become the minimum value. As shown in Figure 9-3-2, let's say that the keyword sequence we're sorting is {9,1,5,8,3,7,4,6,2}, and when I=1, 9 and 1, the first position of 1 is smaller than the following keyword, so it's the smallest value. When the i=2, the second place successively from 9 to 5, replaced by 3, replaced by 2, completed the second small number exchange. The following digital transform is similar and is no longer introduced.
It should be the easiest sort of code to write, but this simple and understandable code is flawed. After observation, it was found that after sorting the positions of 1 and 2, there was little help in ordering the rest of the keywords (the number 3 was changed to the last one). In other words, the efficiency of this algorithm is very low.
Source: http://www.cnblogs.com/cj723/archive/2011/04/15/2016679.html