官术网_书友最值得收藏!

Bubble sort

The algorithm that we will implement in this chapter is well-known as bubble sort. The approach is very simple. Begin at the start of the cards and compare the first and the second card. If the first card is later in lexicographic order than the second one, then swap the two cards. Then repeat this for the card that is at the second place now, then the third, and so on. There is a card that is lexicographically the latest, say Wilson. When we get this card and start to compare it with the next one, we will always swap them; this way, Wilson's card will travel to the last place where it has to be after the sort. All we have to do is repeat this travelling from the start and do the occasional swapping of cards again, but this time only to the last but one element. This time, the second latest element will get to its place—say, Wilkinson will be right before Wilson. If we have n cards, and we repeat this n-1 times, all cards will get to their place.

In the following sections, we will create a Java project that implements this algorithm.

主站蜘蛛池模板: 宾阳县| 谷城县| 乌鲁木齐县| 盐池县| 临洮县| 铜梁县| 开原市| 浦县| 华容县| 察雅县| 峨边| 温州市| 应城市| 建瓯市| 太康县| 眉山市| 平度市| 井陉县| 嘉峪关市| 呼和浩特市| 淅川县| 枞阳县| 吐鲁番市| 什邡市| 永善县| 南开区| 盖州市| 北票市| 临沧市| 米脂县| 新宾| 齐齐哈尔市| 甘洛县| 比如县| 那坡县| 大姚县| 延津县| 阳高县| 科技| 仪陇县| 靖江市|