Home > php教程 > PHP开发 > Classic sorting algorithm - Bubble sort Bubble sort

Classic sorting algorithm - Bubble sort Bubble sort

高洛峰
Release: 2016-12-19 13:08:28
Original
1208 people have browsed it

The principle is to compare adjacent numbers in pairs and exchange them in order from small to large or from large to small.

After such a trip, the largest or smallest number is exchanged to the last digit,

Then start from the beginning again Start pairwise comparison and exchange and end at the penultimate digit. The rest is similar to the example.

The example is sorting from small to large,

Original array to be sorted | 6 | 2 | 4 | 1 | 5 | 9 |


First sorting (outer loop)

First pairwise comparison 6 > 2 exchange (inner loop)

State before exchange | 6 | 2 | 4 | 1 | 5 | 9 |

State after exchange | 2 | 6 | 4 | 1 | 5 | 9 |

Second pairwise comparison, 6 > 4 exchange

Pre-exchange state | 2 | 6 | 4 | 1 | 5 | 9 |

Exchange After state | 2 | 4 | 6 | 1 | 5 | 9 |

The third pairwise comparison, 6 > 1 exchange

Pre-exchange state | 2 | 4 | 6 | 1 | 5 | 9 |

State after exchange | 2 | 4 | 1 | 6 | 5 | 9 |

The fourth pairwise comparison, 6 > 5 exchange

State before exchange | 2 | 4 | 1 | 6 | 5 | 9 |

State after exchange| 2 | 4 | 1 | 5 | 6 | 9 |

The fifth pairwise comparison, 6

State before exchange| 2 | 4 | 1 | 5 | 6 | 9 |

State after exchange| 2 | 4 | 1 | 5 | 6 | 9 |

Second sorting (outer loop)

First pairwise comparison 2

State before exchange | 2 | 4 | 1 | 5 | 6 | 9 |

State after exchange | 2 | 4 | 1 | 5 | 6 | 9 |

Second pairwise comparison, 4 > 1 exchange

State before exchange | 2 | 4 | 1 | 5 | 6 | 9 |
State after exchange | 2 | 1 | 4 | 5 | 6 | 9 |

The third pairwise comparison, 4

Pre-exchange state | 2 | 1 | 4 | 5 | 6 | 9 |
Post-exchange state | 2 | 1 | 4 | 5 | 6 | 9 |

The fourth pairwise comparison, 5 < ; 6 No exchange

Pre-exchange state | 2 | 1 | 4 | 5 | 6 | 9 |

Post-exchange state | 2 | 1 | 4 | 5 | 6 | 9 |

The third sorting (outside Loop)

First pairwise comparison 2 > 1 exchange

State after exchange | 2 | 1 | 4 | 5 | 6 | 9 |

State after exchange | 1 | 2 | 4 | 5 | 6 | 9 |

Second pairwise comparison, 2

State after exchange| 1 | 2 | 4 | 5 | 6 | 9 |
State after exchange| 1 | 2 | 4 | 5 | 6 | 9 |

The third pairwise comparison, 4

State after exchange| 1 | 2 | 4 | 5 | 6 | 9 |
State after exchange| 1 | 2 | 4 | 5 | 6 | 9 |

The fourth sorting (outer loop) without swapping

The fifth sorting (outer loop) without swapping


After sorting, the final result is output 1 2 4 5 6 9

The code is for reference only

static void bubble_sort(int[] unsorted)
        {
            for (int i = 0; i < unsorted.Length; i++)
            {
                for (int j = i; j < unsorted.Length; j++)
                {
                    if (unsorted[i] > unsorted[j])
                    {
                        int temp = unsorted[i];
                        unsorted[i] = unsorted[j];
                        unsorted[j] = temp;
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            int[] x = { 6, 2, 4, 1, 5, 9 };
            bubble_sort(x);
            foreach (var item in x)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
Copy after login

Bubble sort animation demonstration

Classic sorting algorithm - Bubble sort Bubble sort



For more classic sorting algorithms - Bubble sort Bubble sort related articles please pay attention to PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template