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.
X E.
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