Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imtrinity94/6afe1005718c456fb3528e19cbbe6aec to your computer and use it in GitHub Desktop.
Save imtrinity94/6afe1005718c456fb3528e19cbbe6aec to your computer and use it in GitHub Desktop.
Sort an array without changing place of negative numbers in Javascript
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
]*/
@imtrinity94
Copy link
Author

ES5 JS code with bubble sort technique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment