# SolvedDistance (bubble) sort problem

#### Liduen

##### Hacker
Dank Tier VIP
Hi guys,

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.

C++:
``````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:

C++:
``````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!!

Greetings,
Liduen

Last edited:

#### Liduen

##### Hacker
Dank Tier VIP
Its hard to read this clearly on my phone but your bubble sort looks all wrong.

You only need a single greater/less than comparison in your if statement.

For(i = 0; i < 32; ++i)
For(j = 0; j < 32; ++j)
If( item[j]<item)
Swap

Keep in mind bubble sort is a bit slow.

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:

C++:
``````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:

C++:
``````for(int i = 0; i < 32; i++){
for(int j = 0; j < 32; j++){
if((p[i].fDistance > p[j].fDistance && p[i].nDistancePriority < p[j].nDistancePriority) || (p[i].fDistance < p[j].fDistance && p[i].nDistancePriority > p[j].nDistancePriority)){
//swap
}
}
}``````
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!

Liduen

Last edited:

#### squeenie

##### Hacker
Meme Tier VIP
Dank Tier Donator
Its hard to read this clearly on my phone but your bubble sort looks all wrong.

You only need a single greater/less than comparison in your if statement.

For(i = 0; i < 32; ++i)
For(j = 0; j < 32; ++j)
If( item[j]<item)
Swap

Keep in mind bubble sort is a bit slow.

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.

Last edited:

#### till0sch

##### Respected Hacker
Dank Tier VIP
Dank Tier Donator
Well for lowest just go for int lowest= item[0], then loop through and if something's smaller (item<lowest) then make lowest be that item

#### squeenie

##### Hacker
Meme Tier VIP
Dank Tier Donator
Well for lowest just go for int lowest= item[0], then loop through and if something's smaller (item<lowest) then make lowest be that item

This is what I meant to say, just worded much better.
You type better english than I do

#### squeenie

##### Hacker
Meme Tier VIP
Dank Tier Donator
Actually it was right, my bad

Attention! Before you post:

99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

• Fill out the form correctly
• Tell us the game name & coding language