Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Why is this merge sort not sorting equals correctly?
mergeSortMain is all I need help with, when there are equal elements it fails to input all indices and it should have all indices, do you see anything wrong? fromStart is supposed to be from start of its half, the id. indices[0] is id storage, indices[1] is lower bound, indices[2] is upper bound. indices[3] is a flag for which half. aux[0] and aux[1] are just intermediary values. aux[2] is initial indices[1] and aux[3] is initial indices[2]. Run this in Rust playground.
//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
main() is a test function. All problems are in that mergeSortMain. I had a working version that worked differently if you want that. X E.
3 posts - 2 participants
🏷️ Rust_feed