public void nonRecursiveMergeSort(int[] nums) {
int loop = (int) Math.ceil(Math.log(nums.length)/Math.log(2));
Stack<int[]> stack1 = new Stack<>();
Stack<int[]> stack2 = new Stack<>();
for (int num : nums) {
int[] a = {num};
stack1.push(a);
}
while (loop > 0) {
while (!stack1.empty()) {
if (stack1.size() > 1) stack2.push(merge(stack1.pop(), stack1.pop()));
else if (stack1.size() == 1) stack2.push(stack1.pop());
}
loop--;
if (loop == 0) System.arraycopy(stack2.pop(), 0, nums, 0, nums.length);
while (loop > 0&&!stack2.empty()) {
if (stack2.size() > 1) stack1.push(merge(stack2.pop(), stack2.pop()));
else if (stack2.size() == 1) stack1.push(stack2.pop());
}
loop--;
if (loop == 0) System.arraycopy(stack1.pop(), 0, nums, 0, nums.length);
}
}
public int[] merge(int[] nums1, int[] nums2) {
int length1 = nums1.length, length2 = nums2.length;
int[] nums3 = new int[length1 + length2];
int i = 0, j = 0, k = 0;
while (i < length1&&j < length2) {
nums3[k++] = (nums1[i] < nums2[j])?nums1[i++]:nums2[j++];
}
while (i < length1) {
nums3[k++] = nums1[i++];
}
while (j < length2) {
nums3[k++] = nums2[j++];
}
return nums3;
}