长度为n的数组乱序存放着0到n-1,只能0与其他数进行交换,实现排序
作者:互联网
我的思路:
只用一个for循环从数组第一个开始遍历下去,
1)如果第 i 个位置与下标为 i 的数组的值不相等,
2)再判断数组第一个元素是否为0,
3)为0则将0与数组下标为 i 的数进行交换
4)将0与数组第一个元素的值作为下标对应的数进行交换
5)将0与数组第一个元素进行交换
如:array = 301254
第0个位置对应的是3不等于0,则将0与array[3]进行交换得到321054
再将0与数组第一个元素进行交换即与3进行交换得到 021354,第一次循环完成
然后接下来是第二次循环:
第1个位置对应的是0等于0,则将0与array[1]进行交换得到201354
再将0与数组第一个元素的值作为下标对应的数进行交换即与array[2]交换得到210354
再将0与数组第一个元素进行交换即与2进行交换得到012354
。。。
最后得到012345
---------------------------以下为在Android Studio 上的kotlin代码 -------------------------
package com.hh.joehu.swapwithzero
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
class MainActivity : AppCompatActivity() {
companion object {
var array:IntArray = intArrayOf(9,0,1,2,5,4,8,7,6,3,10,15,12,14,13,11)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sort(array)
}
private fun swapWithZero(array:IntArray, number : Int){
var zIndex:Int = -1
var nIndex:Int = -1
for(i in 0..(array.size-1)){
if(array[i] == 0){
zIndex = i
}
if(array[i] == number){
nIndex = i
}
}
val temp:Int = array[zIndex]
array[zIndex] = array[nIndex]
array[nIndex] = temp
}
private fun sort(array:IntArray) {
val len :Int = array.size
if(len <= 1){
return
}
for(i in 0..(len-1)){
if(i != array[i]){
if(array[0] == 0){
swapWithZero(array,array[i])
print(array)
}
swapWithZero(array,array[array[0]])
swapWithZero(array,array[0])
print(array)
}
}
}
private fun print(arr:IntArray){
for(i in 0..(arr.size-1)){
Log.e("abc","aaa " + arr[i] + " ")
}
Log.e("abc","----------------------")
}
}
结果:
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
02-27 19:42:42.079 1270-1270/? E/abc: aaa 0
02-27 19:42:42.079 1270-1270/? E/abc: aaa 1
02-27 19:42:42.079 1270-1270/? E/abc: aaa 2
02-27 19:42:42.079 1270-1270/? E/abc: aaa 3
02-27 19:42:42.079 1270-1270/? E/abc: aaa 4
02-27 19:42:42.079 1270-1270/? E/abc: aaa 5
02-27 19:42:42.079 1270-1270/? E/abc: aaa 6
02-27 19:42:42.079 1270-1270/? E/abc: aaa 7
02-27 19:42:42.079 1270-1270/? E/abc: aaa 8
02-27 19:42:42.079 1270-1270/? E/abc: aaa 9
02-27 19:42:42.079 1270-1270/? E/abc: aaa 10
02-27 19:42:42.079 1270-1270/? E/abc: aaa 11
02-27 19:42:42.079 1270-1270/? E/abc: aaa 12
02-27 19:42:42.079 1270-1270/? E/abc: aaa 13
02-27 19:42:42.079 1270-1270/? E/abc: aaa 14
02-27 19:42:42.079 1270-1270/? E/abc: aaa 15
02-27 19:42:42.079 1270-1270/? E/abc: ----------------------
标签:02,abc,42.079,1270,19,27,数组,排序,乱序 来源: https://blog.csdn.net/wdnwdn520/article/details/87986056