Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Why is this not sorting equals correctly?

JosiahMaybe

Platinum Coder
What is wrong is it does not handle exact equals correctly. Here is my merge sort, you can try it in Rust playground. just the thing called mergeSortMain and it is clearly marked what parts are for equals. It is Rust. I have no warn things so I can see results easily. It does all else correctly.
Code:
//8/7/2025, 5:47PM, better version. X E.
//8/19/2025, 4:16PM, begin work on double buffered thread per element version. X E.
//8:26PM, break, broken same number stuff. X E.
use getrandom;
//use std::mem;
#[allow(warnings)]
fn random_range(start:i32,end:i32) -> i32 {
    let test = getrandom::u32();
    if !test.is_ok() {
        return start;
    }
    let test1 = test.unwrap();
    let mut test2 = start;
    let mut bit = 0;
    while (1<<bit) <= end-start {
        test2+=((((test1 >> bit) & 1) << bit) as i32);
        bit+=1;
    }
    if test2 > end {
        //X E
        return end;
        //X E
    } else if test2 < start {
        //X E
        return start;
        //X E
    } else {
        //X E
        return test2;
        //X E
    }
}
fn random() -> f32 {
    //X E
    return random_range(0,1) as f32;
    //X E
}
//X E
#[allow(warnings)]
fn mergeSortMain(id:usize,step:&Vec<usize>,shapeNumber:usize,zDists:&mut Vec<[f32; 2]>,drawOrder:&mut Vec<usize>,tempOrder:&mut Vec<usize>) {
    //Exit early if index is out of bounds or buffer is 1 or less.
    if (id >= shapeNumber || shapeNumber == 0 || shapeNumber == 1) {
        //X E
        return;
        //X E
    }
    //Start merge sort
    let mut fromStart:usize=0;
    let mut indices:Vec<usize> = vec![id,0,0,0];
    if (step[0]*2<shapeNumber) {
        indices[1]=(id-id%(step[0]*2));
    }
    let mut indices1:Vec<usize> = vec![0,0,0,0];
    //Find both ends and starts and id from its half start.
    if (indices[1]+step[0]*2>shapeNumber) {
        if (step[0]==1) {
            //Copy this one.
            if (step[1]==0) {
                tempOrder[id]=drawOrder[id];
            } else {
                drawOrder[id]=tempOrder[id];
            }
            //X E
            return;
            //X E
        }
        //Figure this half
        indices1[0]=indices[1];
        if (indices1[0]+step[0]<shapeNumber) {
            indices1[0]=indices1[0]+step[0];
        } else if (indices1[0]+step[0]/2<shapeNumber) {
            indices1[0]+=step[0]/2;
        } else {
            //Copy this one.
            if (step[1]==0) {
                tempOrder[id]=drawOrder[id];
            } else {
                drawOrder[id]=tempOrder[id];
            }
            //X E
            return;
            //X E
        }
        //Assign based on which half.
        if (indices[0]>=indices1[0]) {
            indices[2]=indices1[0]-1;
            fromStart=indices[0]-indices1[0];
        } else {
            fromStart=indices[0]-indices[1];
            indices[1]=indices1[0];
            indices[2]=shapeNumber-1;
            indices[3]=1;
        }
    } else if (indices[0]>=indices[1]+step[0]) {
        indices[2]=indices[1]+step[0]-1;
        fromStart=indices[0]-(indices[2]+1);
    } else {
        fromStart=indices[0]-indices[1];
        indices[2]=indices[1]+step[0]*2-1;
        indices[1]+=step[0];
        indices[3]=1;
    }
    //Set initial sets.
    indices1[2]=indices[1];
    indices1[3]=indices[2];
    //Be efficient, only check step[1] once.
    if (step[1]==0) {
        //Loop and find where to input.
        while (indices[1]<indices[2]) {
            //Compare and eliminate all possibilities including current one. Round up.
            indices1[1]=(indices[1]+indices[2]+1)/2;
            indices1[0]=drawOrder[indices1[1]];
            if (zDists[drawOrder[indices[0]]][0]>zDists[indices1[0]][0]) {
                indices[2]=indices1[1]-1;
            } else if (zDists[drawOrder[indices[0]]][0]<zDists[indices1[0]][0]) {
                indices[1]=indices1[1]+1;
            } else if (zDists[drawOrder[indices[0]]][1]>zDists[indices1[0]][1]) {
                indices[2]=indices1[1]-1;
            } else if (zDists[drawOrder[indices[0]]][1]<zDists[indices1[0]][1]) {
                indices[1]=indices1[1]+1;
            } else {
                //Equal numbers found, set before or after based on half.
                if (indices[3]==1) {
                    indices[1]=indices1[1]+1;
                    indices1[0]=1;
                    if (indices[1]+indices1[0]<=indices1[3]) {
                        while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]+indices1[0]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]+indices1[0]]][1]) {
                            if (indices[1]+indices1[0]*2<=indices1[3]) {
                                indices1[0]*=2;
                            } else {
                                break;
                            }
                        }
                        while (indices1[0] != 0) {
                            if (indices[1]+indices1[0]<=indices1[3]) {
                                if (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]+indices1[0]]][0]&&
                                zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]+indices1[0]]][1]) {
                                    indices[1]+=indices1[0];
                                }
                            }
                            indices1[0]/=2;
                        }
                    }
                    if (indices[1]<=indices1[3]) {
                        while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                            zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1]) {
                            indices[1]+=1;
                            if (indices[1]>indices1[3]) {
                                break;
                            }
                        }
                    }
                } else {
                    indices[1]=indices1[1];
                    indices1[0]=1;
                    if (indices[1] != 0) {
                        if (indices[1]-indices1[0]>=indices1[2]) {
                            while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]-indices1[0]]][0]&&
                            zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]-indices1[0]]][1]) {
                                if (indices[1]<indices[0]*2) {
                                    break;
                                } else if (indices[1]-indices[0]*2>=indices1[2]) {
                                    indices1[0]*=2;
                                } else {
                                    break;
                                }
                            }
                            while (indices1[0] != 0) {
                                if (indices[1]>=indices1[0]*2) {
                                    if (indices[1]-indices1[0]>=indices1[2]) {
                                        if (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]+indices1[0]]][0]&&
                                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]+indices1[0]]][1]) {
                                           indices[1]-=indices1[0];
                                        }
                                    }
                                }
                                indices1[0]/=2;
                            }
                        }
                    }
                    while (indices[1]>indices1[2]&&zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1]) {
                        indices[1]-=1;
                    }
                    while (!(zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1])||indices[1]<indices1[2]) {
                        indices[1]+=1;
                    }
                }
                break;
            }
        }
        if (indices[1]==indices[2]) {
            //Compare and find proper place.
            indices1[0]=drawOrder[indices[1]];
            if (zDists[drawOrder[indices[0]]][0]<zDists[indices1[0]][0]||(zDists[drawOrder[indices[0]]][0]==zDists[indices1[0]][0]&&zDists[drawOrder[indices[0]]][1]<zDists[indices1[0]][1])) {
                indices[1]+=1;
            }
        }
    } else {
        //Loop and find where to input.
        while (indices[1]<indices[2]) {
            //Compare and eliminate all possibilities including current one. Round up.
            indices1[1]=(indices[1]+indices[2]+1)/2;
            indices1[0]=tempOrder[indices1[1]];
            if (zDists[tempOrder[indices[0]]][0]>zDists[indices1[0]][0]) {
                indices[2]=indices1[1]-1;
            } else if (zDists[tempOrder[indices[0]]][0]<zDists[indices1[0]][0]) {
                indices[1]=indices1[1]+1;
            } else if (zDists[tempOrder[indices[0]]][1]>zDists[indices1[0]][1]) {
                indices[2]=indices1[1]-1;
            } else if (zDists[tempOrder[indices[0]]][1]<zDists[indices1[0]][1]) {
                indices[1]=indices1[1]+1;
            } else {
                //Equal numbers found, set before or after based on half.
                if (indices[3]==1) {
                    indices[1]=indices1[1]+1;
                    indices1[0]=1;
                    if (indices[1]+indices1[0]<=indices1[3]) {
                        while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]+indices1[0]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]+indices1[0]]][1]) {
                            if (indices[1]+indices1[0]*2<=indices1[3]) {
                                indices1[0]*=2;
                            } else {
                                break;
                            }
                        }
                        while (indices1[0] != 0) {
                            if (indices[1]+indices1[0]<=indices1[3]) {
                                if (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]+indices1[0]]][0]&&
                                zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]+indices1[0]]][1]) {
                                    indices[1]+=indices1[0];
                                }
                            }
                            indices1[0]/=2;
                        }
                    }
                    if (indices[1]<=indices1[3]) {
                        while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                            zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1]) {
                            indices[1]+=1;
                            if (indices[1]>indices1[3]) {
                                break;
                            }
                        }
                    }
                } else {
                    indices[1]=indices1[1];
                    indices1[0]=1;
                    if (indices[1] != 0) {
                        if (indices[1]-indices1[0]>=indices1[2]) {
                            while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]-indices1[0]]][0]&&
                            zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]-indices1[0]]][1]) {
                                if (indices[1]<indices1[0]*2) {
                                    break;
                                } else if (indices[1]-indices1[0]*2>=indices1[2]) {
                                    indices1[0]*=2;
                                } else {
                                    break;
                                }
                            }
                            while (indices1[0] != 0) {
                                if (indices[1]>=indices1[0]*2) {
                                    if (indices[1]-indices1[0]>=indices1[2]) {
                                        if (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]+indices1[0]]][0]&&
                                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]+indices1[0]]][1]) {
                                           indices[1]-=indices1[0];
                                        }
                                    }
                                }
                                indices1[0]/=2;
                            }
                        }
                    }
                    while (indices[1]>indices1[2]&&zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1]) {
                        indices[1]-=1;
                    }
                    while (!(zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1])||indices[1]<indices1[2]) {
                        indices[1]+=1;
                    }
                }
                break;
            }
        }
        if (indices[1]==indices[2]) {
            //Compare and find proper place.
            indices1[0]=tempOrder[indices[1]];
            if (zDists[tempOrder[indices[0]]][0]<zDists[indices1[0]][0]||(zDists[tempOrder[indices[0]]][0]==zDists[indices1[0]][0]&&zDists[tempOrder[indices[0]]][1]<zDists[indices1[0]][1])) {
                indices[1]+=1;
            }
        }
    }
    //Place correctly.
    if (step[0]*2<shapeNumber) {
        indices1[0]=indices[1]-indices1[2]+fromStart+(indices[0]-indices[0]%(step[0]*2));
    } else {
        indices1[0]=indices[1]-indices1[2]+fromStart;
    }
    if (step[1]==0) {
        tempOrder[indices1[0]]=drawOrder[indices[0]];
    } else {
        drawOrder[indices1[0]]=tempOrder[indices[0]];
    }
    //X E
}
//X E
#[allow(warnings)]
fn main() {
    //Generate data.
    let size = random_range(2,10);
    println!("{}",size);
    let mut drawOrder=vec![0usize; size as usize];
    let mut tempOrder=vec![0usize; size as usize];
    let mut zDists=vec![[0.0f32,0.0f32]; size as usize];
    let mut index=size as usize;
    while index != 0 {
        index-=1;
        drawOrder[index]=index;
        zDists[index]=[random(),random()];
        while zDists[index][0] != zDists[index][0] || zDists[index][1] != zDists[index][1] {
            zDists[index]=[random(),random()];
        }
    }
    let mut step=vec![1usize,0usize];
    let mut num=0usize;
    while step[0]<size as usize {
        index=0;
        while index < (size as usize) {
            mergeSortMain(index,&step,size as usize,&mut zDists,&mut drawOrder,&mut tempOrder);
            index+=1;
        }
        print!("[");
        num=step[0]*2;
        index=0;
        while index != size as usize {
            if (step[1]==1) {
                print!("({},{})",zDists[drawOrder[index]][0],zDists[drawOrder[index]][1]);
            } else {
                print!("({},{})",zDists[tempOrder[index]][0],zDists[tempOrder[index]][1]);
            }
            if index+1==size as usize {
                break;
            }
            if index==num-1 {
                print!("|");
                num+=step[0]*2;
            } else {
                print!(",");
            }
            index+=1;
        }
        print!("]");
        println!("");
        num=step[0]*2;
        index=0;
        while index != size as usize {
            if index != num-1&&index+1<size as usize {
                if (step[1]==1) {
                    if zDists[drawOrder[index]][0]<zDists[drawOrder[index+1]][0] {
                        println!("Error: {}",num);
                    } else if zDists[drawOrder[index]][0]==zDists[drawOrder[index+1]][0] {
                        if zDists[drawOrder[index]][1]<zDists[drawOrder[index+1]][1] {
                            println!("Error: {}",num);
                        }
                    }
                } else {
                    if zDists[tempOrder[index]][0]<zDists[tempOrder[index+1]][0] {
                        println!("Error: {}",num);
                    } else if zDists[tempOrder[index]][0]==zDists[tempOrder[index+1]][0] {
                        if zDists[tempOrder[index]][1]<zDists[tempOrder[index+1]][1] {
                            println!("Error: {}",num);
                        }
                    }
                }
            }
            if index==num-1 {
                num+=step[0]*2;
                if num>size as usize {
                    num=size as usize;
                }
            }
            index+=1;
        }
        step[0]*=2;
        if step[1]==0 {
            step[1]=1;
        } else {
            step[1]=0;
        }
    }
    println!("X E.");
    //X E
}
//X E
X E.
 
Updated version, it still has the same problem but is closer to correct.
Code:
//8/7/2025, 5:47PM, better version. X E.
//8/19/2025, 4:16PM, begin work on double buffered thread per element version. X E.
//8:26PM, break, broken same number stuff. X E.
//8/20/2025, 12:53PM, return. X E.
//2:05PM, break, still broken. X E.
use getrandom;
//use std::mem;
#[allow(warnings)]
fn random_range(start:i32,end:i32) -> i32 {
    let test = getrandom::u32();
    if !test.is_ok() {
        return start;
    }
    let test1 = test.unwrap();
    let mut test2 = start;
    let mut bit = 0;
    while (1<<bit) <= end-start {
        test2+=((((test1 >> bit) & 1) << bit) as i32);
        bit+=1;
    }
    if test2 > end {
        //X E
        return end;
        //X E
    } else if test2 < start {
        //X E
        return start;
        //X E
    } else {
        //X E
        return test2;
        //X E
    }
}
fn random() -> f32 {
    //X E
    return random_range(0,1) as f32;
    //X E
}
//X E
#[allow(warnings)]
fn mergeSortMain(id:usize,step:&Vec<usize>,shapeNumber:usize,zDists:&mut Vec<[f32; 2]>,drawOrder:&mut Vec<usize>,tempOrder:&mut Vec<usize>) {
    //Exit early if index is out of bounds or buffer is 1 or less.
    if (id >= shapeNumber || shapeNumber == 0 || shapeNumber == 1) {
        //X E
        return;
        //X E
    }
    //Start merge sort
    let mut fromStart:usize=0;
    let mut indices:Vec<usize> = vec![id,0,0,0];
    if (step[0]*2<shapeNumber) {
        indices[1]=(indices[0]-indices[0]%(step[0]*2));
    }
    let mut aux:Vec<usize> = vec![0,0,0,0];
    //Find both ends and starts and id from its half start.
    if (indices[1]+step[0]*2>shapeNumber) {
        if (step[0]==1) {
            //Copy this one.
            if (step[1]==0) {
                tempOrder[indices[0]]=drawOrder[indices[0]];
            } else {
                drawOrder[indices[0]]=tempOrder[indices[0]];
            }
            //X E
            return;
            //X E
        }
        //Figure this half
        aux[0]=indices[1];
        if (aux[0]+step[0]<shapeNumber) {
            aux[0]=aux[0]+step[0];
        } else if (aux[0]+step[0]/2<shapeNumber) {
            aux[0]+=step[0]/2;
        } else {
            //Copy this one.
            if (step[1]==0) {
                tempOrder[indices[0]]=drawOrder[indices[0]];
            } else {
                drawOrder[indices[0]]=tempOrder[indices[0]];
            }
            //X E
            return;
            //X E
        }
        //Assign based on which half.
        if (indices[0]>=aux[0]) {
            indices[2]=aux[0]-1;
            fromStart=indices[0]-aux[0];
            indices[3]=1;
        } else {
            fromStart=indices[0]-indices[1];
            indices[1]=aux[0];
            indices[2]=shapeNumber-1;
        }
    } else if (indices[0]>=indices[1]+step[0]) {
        indices[2]=indices[1]+step[0]-1;
        fromStart=indices[0]-(indices[2]+1);
        indices[3]=1;
    } else {
        fromStart=indices[0]-indices[1];
        indices[2]=indices[1]+step[0]*2-1;
        indices[1]+=step[0];
    }
    //Set initial sets.
    aux[2]=indices[1];
    aux[3]=indices[2];
    //Be efficient, only check step[1] once.
    if (step[1]==0) {
        //Loop and find where to input.
        while (indices[1]<indices[2]) {
            //Compare and eliminate all possibilities including current one. Round up.
            aux[1]=(indices[1]+indices[2]+1)/2;
            aux[0]=drawOrder[aux[1]];
            if (zDists[drawOrder[indices[0]]][0]>zDists[aux[0]][0]) {
                indices[2]=aux[1]-1;
            } else if (zDists[drawOrder[indices[0]]][0]<zDists[aux[0]][0]) {
                indices[1]=aux[1]+1;
            } else if (zDists[drawOrder[indices[0]]][1]>zDists[aux[0]][1]) {
                indices[2]=aux[1]-1;
            } else if (zDists[drawOrder[indices[0]]][1]<zDists[aux[0]][1]) {
                indices[1]=aux[1]+1;
            } else {
                //Equal numbers found, set before or after based on half.
                if (indices[3]==1) {
                    indices[1]=aux[1]+1;
                    aux[0]=1;
                    if (indices[1]+aux[0]<=aux[3]) {
                        while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]+aux[0]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]+aux[0]]][1]) {
                            if (indices[1]+aux[0]*2<=aux[3]) {
                                aux[0]*=2;
                            } else {
                                break;
                            }
                        }
                        while (aux[0] != 0) {
                            if (indices[1]+aux[0]<=aux[3]) {
                                if (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]+aux[0]]][0]&&
                                zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]+aux[0]]][1]) {
                                    indices[1]+=aux[0];
                                }
                            }
                            aux[0]/=2;
                        }
                    }
                    if (indices[1]<=aux[3]) {
                        while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                            zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1]) {
                            indices[1]+=1;
                            if (indices[1]>aux[3]) {
                                break;
                            }
                        }
                    }
                } else {
                    indices[1]=aux[1];
                    aux[0]=1;
                    if (indices[1] != 0) {
                        if (indices[1]-aux[0]>=aux[2]) {
                            while (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]-aux[0]]][0]&&
                            zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]-aux[0]]][1]) {
                                if (indices[1]<aux[0]*2) {
                                    break;
                                } else if (indices[1]-aux[0]*2>=aux[2]) {
                                    aux[0]*=2;
                                } else {
                                    break;
                                }
                            }
                            while (aux[0] != 0) {
                                if (indices[1]>=aux[0]) {
                                    if (indices[1]-aux[0]>=aux[2]) {
                                        if (zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]-aux[0]]][0]&&
                                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]-aux[0]]][1]) {
                                           indices[1]-=aux[0];
                                        }
                                    }
                                }
                                aux[0]/=2;
                            }
                        }
                    }
                    while (indices[1]>aux[2]&&zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1]) {
                        indices[1]-=1;
                    }
                    while (!(zDists[drawOrder[indices[0]]][0]==zDists[drawOrder[indices[1]]][0]&&
                        zDists[drawOrder[indices[0]]][1]==zDists[drawOrder[indices[1]]][1])||indices[1]<aux[2]) {
                        indices[1]+=1;
                    }
                }
                break;
            }
        }
        if (indices[1]==indices[2]) {
            //Compare and find proper place.
            aux[0]=drawOrder[indices[1]];
            if (zDists[drawOrder[indices[0]]][0]<zDists[aux[0]][0]||(zDists[drawOrder[indices[0]]][0]==zDists[aux[0]][0]&&zDists[drawOrder[indices[0]]][1]<zDists[aux[0]][1])) {
                indices[1]+=1;
            }
        }
    } else {
        //Loop and find where to input.
        while (indices[1]<indices[2]) {
            //Compare and eliminate all possibilities including current one. Round up.
            aux[1]=(indices[1]+indices[2]+1)/2;
            aux[0]=tempOrder[aux[1]];
            if (zDists[tempOrder[indices[0]]][0]>zDists[aux[0]][0]) {
                indices[2]=aux[1]-1;
            } else if (zDists[tempOrder[indices[0]]][0]<zDists[aux[0]][0]) {
                indices[1]=aux[1]+1;
            } else if (zDists[tempOrder[indices[0]]][1]>zDists[aux[0]][1]) {
                indices[2]=aux[1]-1;
            } else if (zDists[tempOrder[indices[0]]][1]<zDists[aux[0]][1]) {
                indices[1]=aux[1]+1;
            } else {
                //Equal numbers found, set before or after based on half.
                if (indices[3]==1) {
                    indices[1]=aux[1]+1;
                    aux[0]=1;
                    if (indices[1]+aux[0]<=aux[3]) {
                        while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]+aux[0]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]+aux[0]]][1]) {
                            if (indices[1]+aux[0]*2<=aux[3]) {
                                aux[0]*=2;
                            } else {
                                break;
                            }
                        }
                        while (aux[0] != 0) {
                            if (indices[1]+aux[0]<=aux[3]) {
                                if (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]+aux[0]]][0]&&
                                zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]+aux[0]]][1]) {
                                    indices[1]+=aux[0];
                                }
                            }
                            aux[0]/=2;
                        }
                    }
                    if (indices[1]<=aux[3]) {
                        while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                            zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1]) {
                            indices[1]+=1;
                            if (indices[1]>aux[3]) {
                                break;
                            }
                        }
                    }
                } else {
                    indices[1]=aux[1];
                    aux[0]=1;
                    if (indices[1] != 0) {
                        if (indices[1]-aux[0]>=aux[2]) {
                            while (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]-aux[0]]][0]&&
                            zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]-aux[0]]][1]) {
                                if (indices[1]<aux[0]*2) {
                                    break;
                                } else if (indices[1]-aux[0]*2>=aux[2]) {
                                    aux[0]*=2;
                                } else {
                                    break;
                                }
                            }
                            while (aux[0] != 0) {
                                if (indices[1]>=aux[0]) {
                                    if (indices[1]-aux[0]>=aux[2]) {
                                        if (zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]-aux[0]]][0]&&
                                            zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]-aux[0]]][1]) {
                                            indices[1]-=aux[0];
                                        }
                                    }
                                }
                                aux[0]/=2;
                            }
                        }
                    }
                    while (indices[1]>aux[2]&&zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1]) {
                        indices[1]-=1;
                    }
                    while (!(zDists[tempOrder[indices[0]]][0]==zDists[tempOrder[indices[1]]][0]&&
                        zDists[tempOrder[indices[0]]][1]==zDists[tempOrder[indices[1]]][1])||indices[1]<aux[2]) {
                        indices[1]+=1;
                    }
                }
                break;
            }
        }
        if (indices[1]==indices[2]) {
            //Compare and find proper place.
            aux[0]=tempOrder[indices[1]];
            if (zDists[tempOrder[indices[0]]][0]<zDists[aux[0]][0]||(zDists[tempOrder[indices[0]]][0]==zDists[aux[0]][0]&&zDists[tempOrder[indices[0]]][1]<zDists[aux[0]][1])) {
                indices[1]+=1;
            }
        }
    }
    //Place correctly.
    if (step[0]*2<shapeNumber) {
        aux[0]=indices[1]-aux[2]+fromStart+(indices[0]-indices[0]%(step[0]*2));
    } else {
        aux[0]=indices[1]-aux[2]+fromStart;
    }
    if (step[1]==0) {
        tempOrder[aux[0]]=drawOrder[indices[0]];
    } else {
        drawOrder[aux[0]]=tempOrder[indices[0]];
    }
    //X E
}
//X E
#[allow(warnings)]
fn main() {
    //Generate data.
    let size = random_range(2,10);
    println!("{}",size);
    let mut drawOrder=vec![0usize; size as usize];
    let mut tempOrder=vec![0usize; size as usize];
    let mut zDists=vec![[0.0f32,0.0f32]; size as usize];
    let mut index=size as usize;
    while index != 0 {
        index-=1;
        drawOrder[index]=index;
        zDists[index]=[random(),random()];
        while zDists[index][0] != zDists[index][0] || zDists[index][1] != zDists[index][1] {
            zDists[index]=[random(),random()];
        }
    }
    let mut step=vec![1usize,0usize];
    let mut num=0usize;
    while step[0]<size as usize {
        index=0;
        while index < (size as usize) {
            mergeSortMain(index,&step,size as usize,&mut zDists,&mut drawOrder,&mut tempOrder);
            index+=1;
        }
        if step[1]==0 {
            println!("{:?}",tempOrder);
        } else {
            println!("{:?}",drawOrder);
        }
        print!("[");
        num=step[0]*2;
        index=0;
        while index != size as usize {
            if (step[1]==1) {
                print!("({},{})",zDists[drawOrder[index]][0],zDists[drawOrder[index]][1]);
            } else {
                print!("({},{})",zDists[tempOrder[index]][0],zDists[tempOrder[index]][1]);
            }
            if index+1==size as usize {
                break;
            }
            if index==num-1 {
                print!("|");
                num+=step[0]*2;
            } else {
                print!(",");
            }
            index+=1;
        }
        print!("]");
        println!("");
        num=step[0]*2;
        index=0;
        while index != size as usize {
            if index != num-1&&index+1<size as usize {
                if (step[1]==1) {
                    if zDists[drawOrder[index]][0]<zDists[drawOrder[index+1]][0] {
                        println!("Error: {}",num);
                    } else if zDists[drawOrder[index]][0]==zDists[drawOrder[index+1]][0] {
                        if zDists[drawOrder[index]][1]<zDists[drawOrder[index+1]][1] {
                            println!("Error: {}",num);
                        }
                    }
                } else {
                    if zDists[tempOrder[index]][0]<zDists[tempOrder[index+1]][0] {
                        println!("Error: {}",num);
                    } else if zDists[tempOrder[index]][0]==zDists[tempOrder[index+1]][0] {
                        if zDists[tempOrder[index]][1]<zDists[tempOrder[index+1]][1] {
                            println!("Error: {}",num);
                        }
                    }
                }
            }
            if index==num-1 {
                num+=step[0]*2;
                if num>size as usize {
                    num=size as usize;
                }
            }
            index+=1;
        }
        step[0]*=2;
        if step[1]==0 {
            step[1]=1;
        } else {
            step[1]=0;
        }
    }
    index=0;
    while index<(size as usize) {
        if step[1]==1 {
            if !tempOrder.contains(&index) {
                println!("Error:{} missed",index);
            }
        } else {
            if !drawOrder.contains(&index) {
                println!("Error:{} missed",index);
            }
        }
        index+=1;
    }
    println!("X E.");
    //X E
}
//X E
X E.
 
I suggest you start from scratch, because you're writing unmaintainable code.

If you want people to find your logic errors, you need to make your program more readable.

In my opinion, the most important lesson in programming is that complicated processes can always be broken down into small simple processes.

There is no obvious hierachy in your code.

A function never needs to be longer than the number of lines on a screen. Subroutines have 2 purposes, avoiding duplication, but also improving readability by breaking a program up.

I like looking for errors, it feels like doing a puzzle, but your program feels like work.
 
I suggest you start from scratch, because you're writing unmaintainable code.

If you want people to find your logic errors, you need to make your program more readable.

In my opinion, the most important lesson in programming is that complicated processes can always be broken down into small simple processes.

There is no obvious hierachy in your code.

A function never needs to be longer than the number of lines on a screen. Subroutines have 2 purposes, avoiding duplication, but also improving readability by breaking a program up.

I like looking for errors, it feels like doing a puzzle, but your program feels like work.
//Equal numbers found, set before or after based on half.

Only stuff in {} brackets with that is faulty. There are two. In fact, that is the bulk of the code, feel free to start from scratch only in that. X E.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom