hello everyone,

hope you are having a good weekend :)

I am working on an algorithm problem, I wrote a psuedo code for iterative merge sort and transferred it to a java code, Iterative merge code is the non-recursive version of mergeSort, every thing is working perfectly but the annoying problem is that when I try my code on an odd-array I just neglects the last element and keep it in place even if it is the smallest value.

This is my code

and this is the result I got: [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 17, 20, 5]Code:`public static void main(String[] args){`

int[] list = {4, 7, 1, 8, 9, 2, 0, 3, 10, 17, 5, 20, 5};

mergeSort(list, 0 , list.length-1);

System.out.println(Arrays.toString(list));

}

private static void mergeSort(int[] list, int s, int e){

int m = 1; //counts the level, depending on number of elements in each sublist, 1-2-4-8...

int end = 0; //variable to be used to determine the last sublist depending on length of list

while ( m < e){ //while not reached the final list

int count = 0;

int i = 0;

while (i < e-m ){ //while we can divide the original array into subarrays of m elements

if(count == list.length/(m*2))

end = e-1;

else end = i+2*m-1;

merge(list, i, i+m-1 , end); //merge the sublist of m elements from s to end

i = i + 2*m; //increment i by 2*m

count++;

}

m = m * 2; //double the m to apply 2-4-8...

}

}

I didn't post the merge method since i am sure that it is correct, I tried having many condition before the start of the merging but none worked. So hope someone knows any trick :)

appreciate any help :(blush): :(angel):