Skip to content

Instantly share code, notes, and snippets.

@anil-pace
Created July 25, 2020 14:23
Show Gist options
  • Select an option

  • Save anil-pace/16650e36bc09419d16a57640dd34dfc7 to your computer and use it in GitHub Desktop.

Select an option

Save anil-pace/16650e36bc09419d16a57640dd34dfc7 to your computer and use it in GitHub Desktop.
Rearrange negative numbers on left end and positive numbers on right most end in an array.
function rearrange_pos_negative(arr) {
var i, j;
j = 0;
for (i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
if (i != j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
}
console.log(arr);
}
var arr = [22, 33, -11, -5, -4, 1, 2];
rearrange_pos_negative(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment