Created
August 1, 2017 10:18
-
-
Save 0001vrn/d27d4b0cd5dd5307c4ea3ac06b3c1710 to your computer and use it in GitHub Desktop.
Given an array of positive and negative integers, re-arrange it so that you have positive integers on one end and negative integers on other
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
/* package whatever; // don't place package name! */ | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
/* Name of the class has to be "Main" only if the class is public. */ | |
class PositiveNegativeSeperateUsingDNF | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
// your code goes here | |
int A[] = {2,4,-5,0,-7,-2,2,-5,1,-9,-7,-1}; | |
DutchNationalFlag(A); | |
for(int i:A) | |
System.out.print(i+" "); | |
} | |
public static void DutchNationalFlag(int[] A) { | |
int posStartIndex = 0; | |
for (int i = 0; i < A.length; i++) { | |
if (A[i] < 0) { | |
int temp = A[i]; | |
for (int j = i; j > posStartIndex; j--) { | |
A[j] = A[j-1]; | |
} | |
A[posStartIndex] = temp; | |
if (A[i] < 0) posStartIndex++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment