Jump to content

Creating my own sort function


gaza165

Recommended Posts

I have an array of numbers and am trying to create a function to sort those numbers into order... but as part of my university practical i have been asked to make the sort function myself isntead of using phps built in sort function... this is what i have so far

 

<?php

$numbers = array(23,41,3,56,42,9,82,56,29);

function sortAscending($Nums) {

for ($i = 1; $i < Size; $i++) {
    $Next = $Nums[$i];
    for ($j = $i - 1; ($j >= 0) && ($Nums[$j] > $Next); $j--) {
      $Nums[$j + 1] = $Nums[$j];
    }
    $Nums[$j + 1] = $Next;
  }  
  return($Nums);  
}

print_r(sortAscending($numbers));


?>

 

 

any ideas as to where im going wrong....

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/
Share on other sites

Sure, i am working from another language called Processing 1.0, i am trying to write this method for Processing but want to figure it out in PHP first so then i can adapt it to work in my processing application....

 

So now i have that working..... now what i need the another method to do is to take these array of numbers and reverse it...

 

so.....

 

23,41,3,56,42,9,82,56,29

 

needs to be

 

29,56,82,9,42,56,3,41,23

 

 

???

how can i edit this

Uhh...array_reverse() does this, but w/e.  Try:

 

<?php
function reverse($array) {
    $temp = array();
    for($i = count($array) - 1;$i>=0;$i--) {
        $temp[] = $array[$i];
    }
    return $temp;
}
$numbers = array(23,41,3,56,42,9,82,56,29);
print_r(reverse($numbers));

LOL...

 

I do appreciate the help but was wondering if you could take a look at this code

 


int[] SortDescending(int[] Nums) {
  int i,
      j;
  int Size = Nums.length;
  int Next;
      
  for (i = 1; i < Size; i++) {
    Next = Nums[i];
    for (j = i - 1; (j >= 0) && (Nums[j] < Next); j--) {
      Nums[j + 1] = Nums[j];
    }
    Nums[j + 1] = Next;
  }  
  return(Nums);  
}


int[] Reverse(int[] Nums) { 
  int i;
  int Size = Nums.length;
  
    for(i = 9 - 1;  i >= 0;  i--) {
        Nums[i] = Nums[i];
    }
    return(Nums);
}

 

is there any reason as to why the Reverse function would not work??

Don't hardcode the length in the for loop, that's why you made the Size var.  And you need a temp array.  Try this:

 

int[] Reverse(int[] Nums) {
int[] temp;
int size = Nums.length;
int i = size;	
int counter = 0;

for (;i>=0;i--) {
	temp[counter] = Nums[i];
	counter++;
}
return temp;
}

 

At least as far as I can see, that should work.  Idk the syntax of that language really, but this seems to be fine.

int[] Reverse(int[] Nums) {
   
   int[] temp;
   int size = Nums.length;
   int i = size;   
   int counter = 0;

   for (;i>=0;i--) {
      temp[counter] = Nums[i];
      counter++;
   }
   return(temp);
}

 

it returns an error saying....

 

"The local variable temp may not have been initialized"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.