gaza165 Posted November 27, 2008 Share Posted November 27, 2008 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 More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 One thing that stands out plain as day, this.... for ($i = 1; $i < Size; $i++) { should be.... for ($i = 1; $i < Size($Nums); $i++) { Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700567 Share on other sites More sharing options...
gaza165 Posted November 27, 2008 Author Share Posted November 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700582 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 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)); Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700589 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 Were not here to do your homework. Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700591 Share on other sites More sharing options...
gaza165 Posted November 27, 2008 Author Share Posted November 27, 2008 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?? Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700604 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700609 Share on other sites More sharing options...
gaza165 Posted November 27, 2008 Author Share Posted November 27, 2008 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" Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700614 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 So, initialize it. This is a php board. Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700619 Share on other sites More sharing options...
gaza165 Posted November 27, 2008 Author Share Posted November 27, 2008 Sorry thorpe, i will have a hunt else where. thanks for all your help... Link to comment https://forums.phpfreaks.com/topic/134550-creating-my-own-sort-function/#findComment-700622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.