Monday, November 14, 2011

BlueJ program on re-arranging numbers in an array with positive numbers first


In this BlueJ program, , user will enter values in an array and after re-arranging the values, the positive numbers will be set first and then the negative values. In the first program, a separate array is used to re-arrange the values.

Codes of the program.

import java.io.*;
class Array1
{
int arr[]=new int[10];
int arr1[]=new int[10];
int i,x=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
for(i=0;i<10;i++)
{
 System.out.print("Enter the Number:");
 arr[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<10;i++)
{
if(arr[i]>=0)
arr1[x++]=arr[i];
}
for(i=0;i<10;i++)
{
if(arr[i]<0)
arr1[x++]=arr[i];
}

 System.out.println("\nArranged array is=");
 for(i=0;i<10;i++)
 System.out.print(" "+arr1[i]);
}
public static void main(String args[]) throws Exception
{
 Array1 ob=new Array1();
 ob.take();
}
}

Here after taking the values, the positive numbers are set in the second array first and then the negative number. This technique involves the use of a second array.

Sample input and output of the program

Enter the Number:33
Enter the Number:22
Enter the Number:33
Enter the Number:44
Enter the Number:-23
Enter the Number:2
Enter the Number:-89
Enter the Number:33
Enter the Number:-8
Enter the Number:12

Arranged array is=
 33 22 33 44 2 33 12 -23 -89 -8


Re-arranging numbers without using second array

This is the modified version of the above program. The same job will done but no second array is used here.


import java.io.*;
class Array1
{
int arr[]=new int[10];

int i,j,n,k;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
for(i=0;i<10;i++)
{
 System.out.print("\nEnter the Number:");
 arr[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<10;i++)
{
if(arr[i]>=0)
continue;

for(j=i+1;j<10;j++)
{
 if(arr[j]>=0)
 {
  n=arr[j];
 break;
}
}
if(j!=10)
{
 for(k=j;k>i;k--)
 {
      arr[k]=arr[k-1];
    }
    arr[k]=n;
}
}
 System.out.println("\nArranged array is=");
 for(i=0;i<10;i++)
 System.out.print(" "+arr[i]);
}
public static void main(String args[]) throws Exception
{
 Array1 ob=new Array1();
 ob.take();
}
}

Technical analysis of this BlueJ program

After entering the values in an array, the numbers are searched for negative and if not found the process is continues. If any negative number is found, the next positive number is searched and if any positive number is found, it is stored in a temporary variable and the numbers which lies between the negative number and the positive number are moved one step right. At the end the positive values found which was stored in a temporary variable is set ion the location of the negative number for whom the process started. The same process is continues for all numbers.

Sample input and output

Enter the Number:3

Enter the Number:-8

Enter the Number:-9

Enter the Number:-3

Enter the Number:2

Enter the Number:4

Enter the Number:5

Enter the Number:3

Enter the Number:-66

Enter the Number:5

Arranged array is=
 3 2 4 5 3 5 -8 -9 -3 -66

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner