I tried to create a Swap function and call it inside a Sort function but it is not working. Please see my code below.
$(document).ready(function(){
var myArray = [18,3,90,25,2,27,36, 22, 4, 77, 15, 9, 43, 54, 11, 5]; var testString = '';
function Swap(a, b){
var temp;
temp = a;
a = b;
b = temp;
}
function Sort(array){
for(var j = 0; j< array.length-1; j++){
for(var k = 1; k< array.length; k++){
if(array[k] < array[k - 1]){
Swap(array[k] , array[k - 1]);
}
}
}
}
Sort(myArray);
for(var i = 0; i < myArray.length; i++){ testString += ", " + myArray[i]; }
alert(testString.substring(2));
})
If I perform the swap inside of the for loop of the Sort() function instead of calling to the Swap() function to perform the swap, everything works fine.
I just want to know why my call to the Swap() function is not working.
So if you do the swapping method inside the for loop then only it will work, because the swap method is only swapping the values of two variables it is not rearranging the values in the array, which is what you are trying to achieve via sorting.
So do the sorting like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script>
var myArray = [18,3,90,25,2,27,36, 22, 4, 77, 15, 9, 43, 54, 11, 5];
var testString = '';
function Swap(a, b){
var temp;
temp = a;
a = b;
b = temp;
}
function Sort(array){
for(var j = 0; j< array.length-1; j++){
for(var k = 1; k< array.length; k++){
if(array[k] < array[k - 1]){
// Swap(parseInt( array[k]) ,parseInt( array[k - 1]));
var temp=array[k];
array[k]=array[k-1];
array[k-1]=temp;
}
}
}
}
Sort(myArray);
for(var i = 0; i < myArray.length; i++){
testString += ", " + myArray[i];
}
alert(testString.substring(2));
</script>
</head>
<body>
</body>
</html>
I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.
I have posted an easier way for this, you were just passing the values of array, not the actual array itself . Hence the values were not persisted in the array after swapping.
Check my code below , I have posted 2 different ways to do that . The second one is neat but bit of risky to play with other library involved.
Method 1 :
//Modify your Swap function like below
//I am passing the position as well as the array itself
function Swap(arr,a, b){
var temp;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
//Then call it from Sort function like below
function Sort(array){
for(var j = 0; j< array.length-1; j++){
for(var k = 1; k< array.length; k++){
if(array[k] < array[k - 1]){
Swap(array,k, k - 1); // pass the actual array and the position
}
}
}
}
Method 2 :
Add the Swap method to the builtin Array prototype , be careful
//Add the method to the prototype
Array.prototype.Swap = function (x,y) {
var b = this[x];
this[x] = this[y];
this[y] = b;
return this; // return the current array instance
}
//Call it from the code like below
function Sort(array){
for(var j = 0; j< array.length-1; j++){
for(var k = 1; k< array.length; k++){
if(array[k] < array[k - 1]){
array.Swap(k,k-1) //Call it like this way
}
}
}
}
Hi, thanks for replying. Your solution works but I guess the way your Swap function is called and what are passed in as arguments are a little different than what I expect.
The following is an example I've found that calls the Swap function and pass in arguments the way I would expect arguments to be passed in.
Member
28 Points
113 Posts
Cannot Create And Call Swap Function
Oct 24, 2016 04:18 AM|madjester|LINK
I tried to create a Swap function and call it inside a Sort function but it is not working. Please see my code below.
If I perform the swap inside of the for loop of the Sort() function instead of calling to the Swap() function to perform the swap, everything works fine.
I just want to know why my call to the Swap() function is not working.
Member
28 Points
113 Posts
Re: Cannot Create And Call Swap Function
Oct 24, 2016 08:23 AM|madjester|LINK
I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.
Participant
1222 Points
371 Posts
Re: Cannot Create And Call Swap Function
Oct 24, 2016 08:33 AM|shivigupta31web|LINK
Hi madjester,
So if you do the swapping method inside the for loop then only it will work, because the swap method is only swapping the values of two variables it is not rearranging the values in the array, which is what you are trying to achieve via sorting.
So do the sorting like this:
Hope this answers your question.
Stay humble.
Member
370 Points
137 Posts
Re: Cannot Create And Call Swap Function
Oct 24, 2016 08:40 AM|Nilishere|LINK
I have posted an easier way for this, you were just passing the values of array, not the actual array itself . Hence the values were not persisted in the array after swapping.
Check my code below , I have posted 2 different ways to do that . The second one is neat but bit of risky to play with other library involved.
Method 1 :
Method 2 :
Add the Swap method to the builtin Array prototype , be careful
Let me know if this works for you
Member
28 Points
113 Posts
Re: Cannot Create And Call Swap Function
Oct 24, 2016 03:45 PM|madjester|LINK
Hi, thanks for replying. Your solution works but I guess the way your Swap function is called and what are passed in as arguments are a little different than what I expect.
The following is an example I've found that calls the Swap function and pass in arguments the way I would expect arguments to be passed in.
http://stackoverflow.com/questions/18817810/is-it-possible-to-write-a-numeric-swap-function-in-javascript