PHP Bubble Sort

I haven’t really had any ideas to post about lately; so, I thought that I would post a php bubble sort that I used to sort a multidimensional array (an array holding arrays of names, high scores, dates, and times). Originally, I planned to use usort() with a compare function my_sort() that I found in an example on w3schools; however, this solution didn’t seem to sort correctly(how I needed it to), and I didn’t know how it worked so I couldn’t adjust it properly. Below, is their compare function to be used with usort.

function my_sort($a, $b)  //by my_sort I mean their_sort
{
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}

This, seemed to work to an extent. However, it seemed like it randomly picked where to place items in an array if they were equal. I thought that I could place the new item behind all of the elements that were equal to it if I changed the condition that checked for equality to return -1. This didn’t work, it only placed the element after the first element to which it was equal. If there were more elements in the array that were also equal, they would be placed behind the new element.

In a case where multiple elements had scores that were equal, I needed a solution that would place the most currently attained score  behind those elements. Thus, I wrote my own bubble sort wich is shown below.

$thescore= $_POST["scorepass"];   //new highscore
$thedate=$_POST["datepass"];      //current date
$thetime=$_POST["timepass"]."\n"; //current time
$todelete = $newscores[9]; //since scores are sorted last
                           //element will get deleted
for($ind=8;$ind>=0;$ind--) //search for spot to add
{                          //new highscore
if((int)$thescore==(int)$newscores[$ind][0] && $_POST["user"]==$newscores[$ind][1] && $thedate==$newscores[$ind][2] && $thetime==$newscores[$ind][3]) //already added

/*I don't know if this check is needed anymore, but originally I had the form submit to itself. Now, it submits to another php page which uses header() to redirect to the orignal page. */
{

break;

}
else if(((int)$thescore <= (int)$newscores[$ind][1])|| $ind==0)
//found spot now propegate backwards
{ //we found a greater score our we traversed the whole array
if($ind!=0 ||((int)$thescore <= (int)$newscores[$ind][1]))
//if score doesn't belong in top spot
{
$ind++; //add to spot below the score that is higher
} //else add to front of array for a new top score
$todelete = $newscores[$ind];/*temp var to hold old score
during substitution*/
$newscores[$ind] = array($_POST //add new score to its spot["user"],$thescore,$thedate,$thetime);
while($ind<9) //shift rest of scores back 1 pos.
{
$ind++;
$todeletenxt = $newscores[$ind];
$newscores[$ind] = $todelete;
  if($ind<9)
  {
  $todelete = $todeletenxt;
  }
}
break;
}//end else if
}//end for loop

Hopefully, this is helpful to you. If you know of any better solutions to this problem, please comment. If you can explain how the usort () works in correlation with that compare function, I would enjoy reading comments about that as well.

Advertisement

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.