I need help with my solution the algorithm question below. My code works but the elements to right side of the array are not supposed to be sorted. My solution came from a java code.
You are given an array of integers. Rearrange the array so that all zeroes are at the beginning of the array.For example,a = [4,2,0,1,0,3,0] -> [0,0,0,4,1,2,3]
Java Code
public static void moveZeroesToBeginning(int[] a) {
int boundary = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 0) {
Utils.swap(a, i, boundary);
boundary += 1;
}
}
}
Swift code
func moveZerosTotheFront(arrays:[Int] )->[Int] {
var result = arrays
var boundary = 0
for index in 0...arrays.count-1{
if result[index] == 0{
print(index )
result.swapAt(index, boundary)
boundary+=1
}
}
return result
}