I've got a problem with my code.
The idea behind it:
- I have an array of objects/players "p[ ]".
- All of the objects have 3d coordinates so I can calculate the distance "fDistance" between me and this player.
- All of them have a distance priority "nDistancePriority". The smaller the distance, the smaller the distance priority. The player with nDistancePriority == 0 is nearest to me.

if(bAimbot){
switch(nAimbotMode){
case 0:
//legit [...]
case 1:
//slotnumber [...]
case 2:
//distance
//first set all possible priorities from 0 to 31 (0,1,2,...,30,31)
if(bInitDistancePriotities){ //yes I know i wrote it wrong but I'm too lazy to correct it
for(int i = 0; i < 32; i++) p[i].nDistancePriority = i;
bInitDistancePriotities = false; //do this only once
}

//set distance priority to each enemy (bubble sort)
for(int i = 0; i < 32; i++){
for(int u = 0; u < 31; u++){
//check with the object 1 above the actual object in the array
if((p[u].fDistance > p[u+1].fDistance && p[u].nDistancePriority < p[u+1].nDistancePriority) || (p[u].fDistance < p[u+1].fDistance && p[u].nDistancePriority > p[u+1].nDistancePriority)){
//swap the priorities
nTempDistance = p[u].nDistancePriority;
p[u].nDistancePriority = p[u+1].nDistancePriority;
p[u+1].nDistancePriority = nTempDistance;
}
}
}

//search the enemy with the smallest distance priority -> 0 -> 32
bNoTarget = true;
for(int i = 0; i < 32; i++){
for(int x = 0; x < 32; x++){
if(p[x].nDistancePriority == i){
if(p[x].bValidAimTarget){
nTarget = x;
bNoTarget = false;
break;
}
else break;
}
}
//leave outer for-loop if valid target found
if(!bNoTarget) break;
}
if(!bNoTarget){
WriteProcessMemory(hProcess, (LPVOID) aWriteRotationX, &p[nTarget].fAimbotX, sizeof(float), NULL);
WriteProcessMemory(hProcess, (LPVOID) aWriteRotationY, &p[nTarget].fAimbotY, sizeof(float), NULL);
}
break;
case 3:
//flick(slot) [...]
default: break;
}
}

Now I'm outputting all the numbers with this piece of code:

if(nCounter == 1){
system("cls");
for(int i = 0; i < 32; i++){
for(int z = 0; z < 32; z++){
if(p[z].nDistancePriority == i){
cout << i << " " << p[z].fDistance << endl;
break;
}
}
}
}

But now most of the times I see pretty small values as distances somewhere in the middl of the list and not as it should be on the top of it.
To be honest the list doesn't seem sorted at all:

So somewhere must be a mistake with the sorting or something.
Does someone spot my mistake?
I would really appreciate it!!

EDIT: What I do is just loop through them all once and just compare one to the next and store the closest one. Once you have gone through them all you'll have the closest one.

EDIT: What I do is just loop through them all once and just compare one to the next and store the closest one. Once you have gone through them all you'll have the closest one.

Okay that would look like this:

int nNearestTarget = 0;
for(int i = 0; i < 32; i++){
for(int j = 0; j < 32; j++){
if(p[j].fDistance < p[nNearestTarget].fDistance) nNearestTarget = j;
}
}

But I wanted to keep my list principle as I need it to simply switch to the second one in case the first one is not valid (in my team or sth.)
Now I wrote it like this:

The last one works fine. Allthough I have no idea why my first try won't work, it's simliar in logic and I can't find a mistake yet.
Squeenie you said the bubble sort was messed up- could you explain it a bit further what exactly was wrong?
Thanks as always guys!