Created
July 25, 2020 14:23
-
-
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.
This file contains hidden or 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
| 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