冒泡排序算法
- 功能:数字从小到大排列
- 算法: 每一趟依次比较相邻的两个数,将小数放前面,大数放后面,直到一趟只剩下最后一个元素
- 时间复杂度:O(n^2)
- 举例[5, 4, 3, 2, 1] -> [1, 2, 3, 4, 5] 的冒泡排序过程
冒泡排序前:[5, 4, 3, 2, 1]
Index{i, [j, j+1]}={0, [0, 1]}冒泡排序中:[4, 5, 3, 2, 1]
Index{i, [j, j+1]}={0, [1, 2]}冒泡排序中:[4, 3, 5, 2, 1]
Index{i, [j, j+1]}={0, [2, 3]}冒泡排序中:[4, 3, 2, 5, 1]
Index{i, [j, j+1]}={0, [3, 4]}冒泡排序中:[4, 3, 2, 1, 5]
Index{i, [j, j+1]}={1, [0, 1]}冒泡排序中:[3, 4, 2, 1, 5]
Index{i, [j, j+1]}={1, [1, 2]}冒泡排序中:[3, 2, 4, 1, 5]
Index{i, [j, j+1]}={1, [2, 3]}冒泡排序中:[3, 2, 1, 4, 5]
Index{i, [j, j+1]}={2, [0, 1]}冒泡排序中:[2, 3, 1, 4, 5]
Index{i, [j, j+1]}={2, [1, 2]}冒泡排序中:[2, 1, 3, 4, 5]
Index{i, [j, j+1]}={3, [0, 1]}冒泡排序中:[1, 2, 3, 4, 5]
冒泡排序后:[1, 2, 3, 4, 5]
Kotlin 实现的demo
代码
package bubble
class BubbleTest {
companion object {
@JvmStatic
fun main(args: Array<String>) {
sortBubble(intArrayOf(5, 4, 3, 2, 1))
sortBubblePlus(intArrayOf(1, 2, 3, 4, 5))
}
/**
* 冒泡排序算法
* 功能:数字从小到大排列
* 算法: 每一趟依次比较相邻的两个数,将小数放前面,大数放后面,直到一趟只剩下最后一个元素
* 时间复杂度:O(n^2)
*/
fun sortBubble(data: IntArray) {
println("\nsortBubble()==================================")
println("冒泡排序前:${data.toList()}")
for (i in data.indices) {
for (j in 0 until data.size - 1 - i) {
if (data[j] > data[j + 1]) {
val temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
}
println(" Index{i, [j, j+1]}={$i, [$j, ${j + 1}]}冒泡排序中:${data.toList()}")
}
}
println("冒泡排序后:${data.toList()}")
println("sortBubble()==================================\n")
}
/**
* 优化: 设置一个标记位,如果没有发生任何交换,则为有序,不需要再执行
*/
fun sortBubblePlus(data: IntArray){
println("\nsortBubblePlus()==================================")
println("冒泡排序前:${data.toList()}")
var flag = 0
for (i in data.indices) {
for (j in 0 until data.size - 1 - i) {
if (data[j] > data[j + 1]) {
flag = 1
val temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
}
println(" Index{i, [j, j+1]}={$i, [$j, ${j + 1}]}冒泡排序中:${data.toList()}")
}
if (flag == 0) {
println(" sortBubblePlus: 有序,不需要再执行外循环")
break
}
}
println("冒泡排序后:${data.toList()}")
println("sortBubblePlus()==================================\n")
}
}
}
运行结果
"C:\Program Files\Android\Android Studio\jre\bin\java.exe" "-javaagent:C:\Program Files\Android\Android Studio\lib\idea_rt.jar=51320:C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath D:\AndroidSDK\platforms\android-29\android.jar;D:\AndroidSDK\platforms\android-29\data\res;D:\AsProject\gitProject\ReciteInterviewQuestions\mylibrary\build\intermediates\javac\debug\classes;D:\AsProject\gitProject\ReciteInterviewQuestions\mylibrary\build\tmp\kotlin-classes\debug;D:\AsProject\gitProject\ReciteInterviewQuestions\mylibrary\build\intermediates\compile_only_not_namespaced_r_class_jar\debug\R.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-android-extensions-runtime\1.3.72\83423235971335be77d2ea025008bc9959738ffc\kotlin-android-extensions-runtime-1.3.72.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.3.72\8032138f12c0180bc4e51fe139d4c52b46db6109\kotlin-stdlib-1.3.72.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common\1.3.72\6ca8bee3d88957eaaaef077c41c908c9940492d8\kotlin-stdlib-common-1.3.72.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\androidx.collection\collection\1.1.0\1f27220b47669781457de0d600849a5de0e89909\collection-1.1.0.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\androidx.lifecycle\lifecycle-common\2.1.0\c67e7807d9cd6c329b9d0218b2ec4e505dd340b7\lifecycle-common-2.1.0.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\androidx.arch.core\core-common\2.1.0\b3152fc64428c9354344bd89848ecddc09b6f07e\core-common-2.1.0.jar;C:\Users\珞落\.gradle\caches\modules-2\files-2.1\androidx.annotation\annotation\1.1.0\e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8\annotation-1.1.0.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\eddd3afdd70de58f8cd62a2efe1aaa1b\jetified-core-ktx-1.3.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\e46f3e16fd8948e8e4c48c913cb404d3\appcompat-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\e46f3e16fd8948e8e4c48c913cb404d3\appcompat-1.1.0\res;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\7619667f73099691456e81e3fedf8086\fragment-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\8bdc78f309f7ef2e274359e8a4f096da\jetified-appcompat-resources-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\8bdc78f309f7ef2e274359e8a4f096da\jetified-appcompat-resources-1.1.0\res;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\f66fa33308b0619d349f33f29e1b9234\drawerlayout-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\1570490a9aefa8bf7c72c2091e5007e6\viewpager-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\c38deefe61a4b29c2fedadb5cb828cb9\loader-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\cb7465d23e93e74bedcba12369934f8a\jetified-activity-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\721dcdb357351f758b5df456f00974d6\vectordrawable-animated-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\5449d447cecb7a5b7105df425966ce92\vectordrawable-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\4bf5efb9c6ca1cc1042b4ed281aac9d8\customview-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\8106c9afeb27cc0c50f85995740f877c\core-1.3.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\8106c9afeb27cc0c50f85995740f877c\core-1.3.0\res;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\b37b2aeed146cfc3b1274c4cf21d4028\cursoradapter-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\b741faf0c0e978004788a3922a8a438d\versionedparcelable-1.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\8164904e8dbba255593df041e8bd30ea\lifecycle-runtime-2.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\59c8a6e28f96474652a981d6333afcdf\lifecycle-viewmodel-2.1.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\3a332f7b3ba12f0d97b1eff5486f8fcf\jetified-savedstate-1.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\c20b3a9c01fac1c58487516ff423d6b0\lifecycle-livedata-2.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\68a64a089b7f098f26bb791f76de8be0\lifecycle-livedata-core-2.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\24d5b49fe2b36ef44f901a0f8118cef3\core-runtime-2.0.0\jars\classes.jar;C:\Users\珞落\.gradle\caches\transforms-2\files-2.1\32691ed1dd96a709bdd6754d66b7ebfd\interpolator-1.0.0\jars\classes.jar bubble.BubbleTest
sortBubble()==================================
冒泡排序前:[5, 4, 3, 2, 1]
Index{i, [j, j+1]}={0, [0, 1]}冒泡排序中:[4, 5, 3, 2, 1]
Index{i, [j, j+1]}={0, [1, 2]}冒泡排序中:[4, 3, 5, 2, 1]
Index{i, [j, j+1]}={0, [2, 3]}冒泡排序中:[4, 3, 2, 5, 1]
Index{i, [j, j+1]}={0, [3, 4]}冒泡排序中:[4, 3, 2, 1, 5]
Index{i, [j, j+1]}={1, [0, 1]}冒泡排序中:[3, 4, 2, 1, 5]
Index{i, [j, j+1]}={1, [1, 2]}冒泡排序中:[3, 2, 4, 1, 5]
Index{i, [j, j+1]}={1, [2, 3]}冒泡排序中:[3, 2, 1, 4, 5]
Index{i, [j, j+1]}={2, [0, 1]}冒泡排序中:[2, 3, 1, 4, 5]
Index{i, [j, j+1]}={2, [1, 2]}冒泡排序中:[2, 1, 3, 4, 5]
Index{i, [j, j+1]}={3, [0, 1]}冒泡排序中:[1, 2, 3, 4, 5]
冒泡排序后:[1, 2, 3, 4, 5]
sortBubble()==================================
sortBubblePlus()==================================
冒泡排序前:[1, 2, 3, 4, 5]
Index{i, [j, j+1]}={0, [0, 1]}冒泡排序中:[1, 2, 3, 4, 5]
Index{i, [j, j+1]}={0, [1, 2]}冒泡排序中:[1, 2, 3, 4, 5]
Index{i, [j, j+1]}={0, [2, 3]}冒泡排序中:[1, 2, 3, 4, 5]
Index{i, [j, j+1]}={0, [3, 4]}冒泡排序中:[1, 2, 3, 4, 5]
sortBubblePlus: 有序,不需要再执行外循环
冒泡排序后:[1, 2, 3, 4, 5]
sortBubblePlus()==================================
Process finished with exit code 0