Last active
February 22, 2022 12:35
-
-
Save imtrinity94/6afe1005718c456fb3528e19cbbe6aec to your computer and use it in GitHub Desktop.
Sort an array without changing place of negative numbers in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [999,-1,4,89,6,7,-99,-90] | |
for (var i = 0; i < arr.length ; i++){ | |
for (var j = i; j< arr.length; j++){ | |
if(arr[i] >= 0 && arr[j] >= 0 && arr[i]> arr[j]){ | |
temp = arr[i] | |
arr[i]=arr[j] | |
arr[j]=temp | |
} | |
else if(arr[i]<0) i++ | |
} | |
} | |
console.log(arr); | |
/*Output: | |
[ | |
4, -1, 6, 7, | |
89, 999, -99, -90 | |
]*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ES5 JS code with bubble sort technique.