I have a 10x loop to sort an array of numbers B
(copy of original array A
) and record the run-time, since the array gets sorted at the end of a loop i created a duplicate array B
to be changed and use A
to set it to it's original state at the begin of a new loop.
when it sorts B
, A
automatically gets sorted too even though i only send B
.
why does this happen and how to change it?
loop:
for (i in 1..10) {
val B = A
val time = measureTimeMillis {
val sorting = Insertion(B); sorting.sor
}
println(""+ cycle++ + "\t" + time)
}
sort:
class Insertion(var B: IntArray) {
fun sort(): Unit {
for( j in 1 until B.size){
var key = B[j]
var i = j-1
while( i > -1 && B[i] > key){
B[i+1] = B[i]
i= i - 1
}
B[i+1] = key
}
}
}