Sorting an array without arr.sort() method, what's going on here?
I believe I've figured this out, but wanted some insight as to how this is working in reality. I have this function here that sorts numbers in ascending order. My understanding is that loops going past the array length returns undefined and had it initially written as such below.
However, it appears that the last number in the array (6) does not get reached and the function leaves that number behind. Arr.length - 1 should be an index of 5 meaning that arr[5] gets called at some point, but it doesn't seem to be the case?
In the function below the first one, I took out arr.length - 1 and simply put arr.length and it appears to work correctly, but arr.length = 6 in this case. My understanding is list[6] does not exist or would be undefined in this case, so can someone help me understand why the second function works and not the first for sorting?
Thanks!
// this one doesn't work
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
// this one works?
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
arrays sorting
|
show 1 more comment
I believe I've figured this out, but wanted some insight as to how this is working in reality. I have this function here that sorts numbers in ascending order. My understanding is that loops going past the array length returns undefined and had it initially written as such below.
However, it appears that the last number in the array (6) does not get reached and the function leaves that number behind. Arr.length - 1 should be an index of 5 meaning that arr[5] gets called at some point, but it doesn't seem to be the case?
In the function below the first one, I took out arr.length - 1 and simply put arr.length and it appears to work correctly, but arr.length = 6 in this case. My understanding is list[6] does not exist or would be undefined in this case, so can someone help me understand why the second function works and not the first for sorting?
Thanks!
// this one doesn't work
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
// this one works?
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
arrays sorting
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18
|
show 1 more comment
I believe I've figured this out, but wanted some insight as to how this is working in reality. I have this function here that sorts numbers in ascending order. My understanding is that loops going past the array length returns undefined and had it initially written as such below.
However, it appears that the last number in the array (6) does not get reached and the function leaves that number behind. Arr.length - 1 should be an index of 5 meaning that arr[5] gets called at some point, but it doesn't seem to be the case?
In the function below the first one, I took out arr.length - 1 and simply put arr.length and it appears to work correctly, but arr.length = 6 in this case. My understanding is list[6] does not exist or would be undefined in this case, so can someone help me understand why the second function works and not the first for sorting?
Thanks!
// this one doesn't work
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
// this one works?
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
arrays sorting
I believe I've figured this out, but wanted some insight as to how this is working in reality. I have this function here that sorts numbers in ascending order. My understanding is that loops going past the array length returns undefined and had it initially written as such below.
However, it appears that the last number in the array (6) does not get reached and the function leaves that number behind. Arr.length - 1 should be an index of 5 meaning that arr[5] gets called at some point, but it doesn't seem to be the case?
In the function below the first one, I took out arr.length - 1 and simply put arr.length and it appears to work correctly, but arr.length = 6 in this case. My understanding is list[6] does not exist or would be undefined in this case, so can someone help me understand why the second function works and not the first for sorting?
Thanks!
// this one doesn't work
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
// this one works?
let list = [1, 10, 9, 8, 3, 6];
function sortNumbers(arr) {
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(arr[i]);
let temp = arr[i];
if (arr[i] < arr[j]) {
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
arrays sorting
arrays sorting
edited Jan 2 at 6:08
Adrian Shum
28.8k763107
28.8k763107
asked Jan 2 at 4:46
Elbert BaeElbert Bae
32
32
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18
|
show 1 more comment
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18
|
show 1 more comment
3 Answers
3
active
oldest
votes
The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.
The second function works because it goes from 0 until it is less than 6, so it does reach j=5
add a comment |
arr.length = 6, so the for loop must index the array from 0 until it is less than arr.length
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
add a comment |
arr.length = 6, and your loop is (i < arr.length - 1) means i value is 0 to (6-1=5). it means i until i is less than 5. if you use less than equal to (<=) operator then your problem resolve.it means loop is value of i is less than or equal to 5.
the array index start from 0 to n-1 so value is (i=0 ; i<=n-1;i++).
add a comment |
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%2f54001306%2fsorting-an-array-without-arr-sort-method-whats-going-on-here%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.
The second function works because it goes from 0 until it is less than 6, so it does reach j=5
add a comment |
The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.
The second function works because it goes from 0 until it is less than 6, so it does reach j=5
add a comment |
The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.
The second function works because it goes from 0 until it is less than 6, so it does reach j=5
The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.
The second function works because it goes from 0 until it is less than 6, so it does reach j=5
answered Jan 2 at 5:11
Hoggie JohnsonHoggie Johnson
606
606
add a comment |
add a comment |
arr.length = 6, so the for loop must index the array from 0 until it is less than arr.length
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
add a comment |
arr.length = 6, so the for loop must index the array from 0 until it is less than arr.length
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
add a comment |
arr.length = 6, so the for loop must index the array from 0 until it is less than arr.length
arr.length = 6, so the for loop must index the array from 0 until it is less than arr.length
answered Jan 2 at 5:19
Garrett WesleyGarrett Wesley
111
111
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
add a comment |
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Trivial answers must go in comment section. Please see stackoverflow.com/help/how-to-answer
– Ankur Aggarwal
Jan 2 at 5:29
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
Oof... I should've caught that, oops. Thanks for pointing it out!
– Elbert Bae
Jan 2 at 5:38
add a comment |
arr.length = 6, and your loop is (i < arr.length - 1) means i value is 0 to (6-1=5). it means i until i is less than 5. if you use less than equal to (<=) operator then your problem resolve.it means loop is value of i is less than or equal to 5.
the array index start from 0 to n-1 so value is (i=0 ; i<=n-1;i++).
add a comment |
arr.length = 6, and your loop is (i < arr.length - 1) means i value is 0 to (6-1=5). it means i until i is less than 5. if you use less than equal to (<=) operator then your problem resolve.it means loop is value of i is less than or equal to 5.
the array index start from 0 to n-1 so value is (i=0 ; i<=n-1;i++).
add a comment |
arr.length = 6, and your loop is (i < arr.length - 1) means i value is 0 to (6-1=5). it means i until i is less than 5. if you use less than equal to (<=) operator then your problem resolve.it means loop is value of i is less than or equal to 5.
the array index start from 0 to n-1 so value is (i=0 ; i<=n-1;i++).
arr.length = 6, and your loop is (i < arr.length - 1) means i value is 0 to (6-1=5). it means i until i is less than 5. if you use less than equal to (<=) operator then your problem resolve.it means loop is value of i is less than or equal to 5.
the array index start from 0 to n-1 so value is (i=0 ; i<=n-1;i++).
answered Jan 2 at 5:30
Akhil SinghAkhil Singh
335113
335113
add a comment |
add a comment |
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%2f54001306%2fsorting-an-array-without-arr-sort-method-whats-going-on-here%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
arr.length = 6. i < arr.length which means when i will become 6 the equation will become invalid and then it will not enter the loop
– Brij Raj Kishore
Jan 2 at 5:06
and this seems like kind of a bubble sort not quick sort
– Brij Raj Kishore
Jan 2 at 5:07
@BrijRajKishore Is quick sort a different method? I'll have to look into that!
– Elbert Bae
Jan 2 at 5:40
Yes quicksort is indeed a very different sorting algorithm.
– Brij Raj Kishore
Jan 2 at 6:10
a bit off topic: your sorting algorithm is highly inefficient and looks strange. I highly recommend you checking out some basic sorting algorithms, like bubble sort, instead of inventing something strange.
– Adrian Shum
Jan 2 at 6:18