Sorting algorithm gone wrong
im trying to translate a sortingmethod from Javascript to php and i have run in to some trouble.
The code looks as follows:
private static function quicksort($ids, $dists, $left, $right) {
if ($right - $left <= 20) {
for ($i = $left + 1; $i <= $right; $i++) {
$temp = $ids[$i];
$tempDist = $dists[$temp];
$j = $i - 1;
while ($j >= $left && $dists[$ids[$j]] > $tempDist) {
$ids[$j + 1] = $ids[$j--];
}
$ids[$j + 1] = $temp;
}
} else {
$median = ($left + $right) >> 1;
$i = $left + 1;
$j = $right;
self::swap($ids, $median, $i);
if ($dists[$ids[$left]] > $dists[$ids[$right]]) self::swap($ids, $left, $right);
if ($dists[$ids[$i]] > $dists[$ids[$right]]) self::swap($ids, $i, $right);
if ($dists[$ids[$left]] > $dists[$ids[$i]]) self::swap($ids, $left, $i);
$temp = $ids[$i];
$tempDist = $dists[$temp];
while (true) {
do $i++; while ($dists[$ids[$i]] < $tempDist);
do $j--; while ($dists[$ids[$j]] > $tempDist);
if ($j < $i) break;
self::swap($ids, $i, $j);
}
$ids[$left + 1] = $ids[$j];
$ids[$j] = $temp;
if ($right - $i + 1 >= $j - $left) {
self::quicksort($ids, $dists, $i, $right);
self::quicksort($ids, $dists, $left, $j - 1);
} else {
self::quicksort($ids, $dists, $left, $j - 1);
self::quicksort($ids, $dists, $i, $right);
}
}
}
private static function swap($arr, $i, $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
I run this code with these parameters:
- $ids - array with 80 objects
- $dists - array with 80 objects
- $left - 0
- $right - 79
and get an error on this line saying Undefined offset: 161
do $i++; while ($dists[$ids[$i]] < $tempDist);
php
|
show 1 more comment
im trying to translate a sortingmethod from Javascript to php and i have run in to some trouble.
The code looks as follows:
private static function quicksort($ids, $dists, $left, $right) {
if ($right - $left <= 20) {
for ($i = $left + 1; $i <= $right; $i++) {
$temp = $ids[$i];
$tempDist = $dists[$temp];
$j = $i - 1;
while ($j >= $left && $dists[$ids[$j]] > $tempDist) {
$ids[$j + 1] = $ids[$j--];
}
$ids[$j + 1] = $temp;
}
} else {
$median = ($left + $right) >> 1;
$i = $left + 1;
$j = $right;
self::swap($ids, $median, $i);
if ($dists[$ids[$left]] > $dists[$ids[$right]]) self::swap($ids, $left, $right);
if ($dists[$ids[$i]] > $dists[$ids[$right]]) self::swap($ids, $i, $right);
if ($dists[$ids[$left]] > $dists[$ids[$i]]) self::swap($ids, $left, $i);
$temp = $ids[$i];
$tempDist = $dists[$temp];
while (true) {
do $i++; while ($dists[$ids[$i]] < $tempDist);
do $j--; while ($dists[$ids[$j]] > $tempDist);
if ($j < $i) break;
self::swap($ids, $i, $j);
}
$ids[$left + 1] = $ids[$j];
$ids[$j] = $temp;
if ($right - $i + 1 >= $j - $left) {
self::quicksort($ids, $dists, $i, $right);
self::quicksort($ids, $dists, $left, $j - 1);
} else {
self::quicksort($ids, $dists, $left, $j - 1);
self::quicksort($ids, $dists, $i, $right);
}
}
}
private static function swap($arr, $i, $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
I run this code with these parameters:
- $ids - array with 80 objects
- $dists - array with 80 objects
- $left - 0
- $right - 79
and get an error on this line saying Undefined offset: 161
do $i++; while ($dists[$ids[$i]] < $tempDist);
php
3
Are you doing this as an exercise? If not, you should just usesort()
– Karsten Koop
Nov 20 '18 at 11:03
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
JavaScript allows accessing of non-existent array elements or string positions, and will just returnundefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.
– misorude
Nov 20 '18 at 11:41
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18
|
show 1 more comment
im trying to translate a sortingmethod from Javascript to php and i have run in to some trouble.
The code looks as follows:
private static function quicksort($ids, $dists, $left, $right) {
if ($right - $left <= 20) {
for ($i = $left + 1; $i <= $right; $i++) {
$temp = $ids[$i];
$tempDist = $dists[$temp];
$j = $i - 1;
while ($j >= $left && $dists[$ids[$j]] > $tempDist) {
$ids[$j + 1] = $ids[$j--];
}
$ids[$j + 1] = $temp;
}
} else {
$median = ($left + $right) >> 1;
$i = $left + 1;
$j = $right;
self::swap($ids, $median, $i);
if ($dists[$ids[$left]] > $dists[$ids[$right]]) self::swap($ids, $left, $right);
if ($dists[$ids[$i]] > $dists[$ids[$right]]) self::swap($ids, $i, $right);
if ($dists[$ids[$left]] > $dists[$ids[$i]]) self::swap($ids, $left, $i);
$temp = $ids[$i];
$tempDist = $dists[$temp];
while (true) {
do $i++; while ($dists[$ids[$i]] < $tempDist);
do $j--; while ($dists[$ids[$j]] > $tempDist);
if ($j < $i) break;
self::swap($ids, $i, $j);
}
$ids[$left + 1] = $ids[$j];
$ids[$j] = $temp;
if ($right - $i + 1 >= $j - $left) {
self::quicksort($ids, $dists, $i, $right);
self::quicksort($ids, $dists, $left, $j - 1);
} else {
self::quicksort($ids, $dists, $left, $j - 1);
self::quicksort($ids, $dists, $i, $right);
}
}
}
private static function swap($arr, $i, $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
I run this code with these parameters:
- $ids - array with 80 objects
- $dists - array with 80 objects
- $left - 0
- $right - 79
and get an error on this line saying Undefined offset: 161
do $i++; while ($dists[$ids[$i]] < $tempDist);
php
im trying to translate a sortingmethod from Javascript to php and i have run in to some trouble.
The code looks as follows:
private static function quicksort($ids, $dists, $left, $right) {
if ($right - $left <= 20) {
for ($i = $left + 1; $i <= $right; $i++) {
$temp = $ids[$i];
$tempDist = $dists[$temp];
$j = $i - 1;
while ($j >= $left && $dists[$ids[$j]] > $tempDist) {
$ids[$j + 1] = $ids[$j--];
}
$ids[$j + 1] = $temp;
}
} else {
$median = ($left + $right) >> 1;
$i = $left + 1;
$j = $right;
self::swap($ids, $median, $i);
if ($dists[$ids[$left]] > $dists[$ids[$right]]) self::swap($ids, $left, $right);
if ($dists[$ids[$i]] > $dists[$ids[$right]]) self::swap($ids, $i, $right);
if ($dists[$ids[$left]] > $dists[$ids[$i]]) self::swap($ids, $left, $i);
$temp = $ids[$i];
$tempDist = $dists[$temp];
while (true) {
do $i++; while ($dists[$ids[$i]] < $tempDist);
do $j--; while ($dists[$ids[$j]] > $tempDist);
if ($j < $i) break;
self::swap($ids, $i, $j);
}
$ids[$left + 1] = $ids[$j];
$ids[$j] = $temp;
if ($right - $i + 1 >= $j - $left) {
self::quicksort($ids, $dists, $i, $right);
self::quicksort($ids, $dists, $left, $j - 1);
} else {
self::quicksort($ids, $dists, $left, $j - 1);
self::quicksort($ids, $dists, $i, $right);
}
}
}
private static function swap($arr, $i, $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
I run this code with these parameters:
- $ids - array with 80 objects
- $dists - array with 80 objects
- $left - 0
- $right - 79
and get an error on this line saying Undefined offset: 161
do $i++; while ($dists[$ids[$i]] < $tempDist);
php
php
asked Nov 20 '18 at 11:00
Pei-turnPei-turn
177
177
3
Are you doing this as an exercise? If not, you should just usesort()
– Karsten Koop
Nov 20 '18 at 11:03
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
JavaScript allows accessing of non-existent array elements or string positions, and will just returnundefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.
– misorude
Nov 20 '18 at 11:41
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18
|
show 1 more comment
3
Are you doing this as an exercise? If not, you should just usesort()
– Karsten Koop
Nov 20 '18 at 11:03
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
JavaScript allows accessing of non-existent array elements or string positions, and will just returnundefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.
– misorude
Nov 20 '18 at 11:41
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18
3
3
Are you doing this as an exercise? If not, you should just use
sort()
– Karsten Koop
Nov 20 '18 at 11:03
Are you doing this as an exercise? If not, you should just use
sort()
– Karsten Koop
Nov 20 '18 at 11:03
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
JavaScript allows accessing of non-existent array elements or string positions, and will just return
undefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.– misorude
Nov 20 '18 at 11:41
JavaScript allows accessing of non-existent array elements or string positions, and will just return
undefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.– misorude
Nov 20 '18 at 11:41
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391551%2fsorting-algorithm-gone-wrong%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391551%2fsorting-algorithm-gone-wrong%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Are you doing this as an exercise? If not, you should just use
sort()
– Karsten Koop
Nov 20 '18 at 11:03
I'm trying to translate a sortingmethod from Javascript to php... Why?
– B001ᛦ
Nov 20 '18 at 11:19
JavaScript allows accessing of non-existent array elements or string positions, and will just return
undefined
without issuing any warning or error; PHP however does not, and gives you an “undefined index/offset” in such a case. So you need to test whether what you want to access exists first.– misorude
Nov 20 '18 at 11:41
@KarstenKoop: No but it is a part of a larger library that i am translating so i want it to be the same method.
– Pei-turn
Nov 20 '18 at 12:13
@B001ᛦ: because i need the code to run serverside and i dont have node.js
– Pei-turn
Nov 20 '18 at 12:18