Skip to content

Commit 5412fdf

Browse files
Initial Commit
1 parent 67cbf49 commit 5412fdf

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

easyq/src/easyq/Rearrange_1.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package easyq;
2+
/* BY-M.NAVEEN
3+
* engineerscodes
4+
* code -Program for array rotation
5+
* sr.code-Ar2
6+
* moves zeros to end of the array
7+
* */
8+
import java.util.*;
9+
public class Rearrange_1 // class name
10+
{ static Scanner nav=new Scanner (System.in); // calling Scanner and creating object nav
11+
static int array[],size,replace=0; // class instances variables
12+
public Rearrange_1(int c) // constructor
13+
{
14+
size=c;
15+
array=new int[size]; //intialization of variables
16+
}
17+
public static void repair() //method
18+
{
19+
for( int i=0;i<size;i++)
20+
{
21+
if(array[i]!=-1 && array[i]!=i)
22+
{ replace=array[i];
23+
while(array[replace]!=-1&& array[replace]!=replace)
24+
{
25+
int y=array[replace];
26+
array[replace]=replace;
27+
replace=y;
28+
}
29+
array[replace]=replace;
30+
if (array[i]!=-1)
31+
{
32+
array[i]=-1;
33+
}
34+
}
35+
}
36+
System.out.println(" temp array "+Arrays.toString(array));
37+
}
38+
public static void input()
39+
{
40+
for(int i=0;i<array.length;i++)
41+
array[i]=nav.nextInt();
42+
System.out.println("element in array are::"+Arrays.toString(array));
43+
repair();
44+
}
45+
public static void main(String arg[])
46+
{
47+
System.out.println("enter the size of the array");
48+
Rearrange_1 n=new Rearrange_1 (nav.nextInt());
49+
System.out.println("Enter the element of array");
50+
input();
51+
}
52+
}
53+
//[-1, -1, 6, 1, 9, 3, 2, -1, 4, -1]

0 commit comments

Comments
 (0)