"Dahua Data Structure" 9th Chapter 9.3 Bubble Sort (UP) _ Big Talk data structure

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.