Beruflich Dokumente
Kultur Dokumente
---------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Array
{
int[] nArray;
Array(int arrSize)
{
nArray = new int[arrSize];
}
void initialize()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the elements of the array");
for (int i=0; i<=nArray.length-1; i++)
nArray[i] = sc.nextInt();
}
void display()
{
for(int i=0; i<=nArray.length-1; i++)
System.out.print(" "+nArray[i]+" ");
System.out.println();
}
void arrange()//Ascending Order
{
int temp;
----------------------------------------------------------Page 1 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)
2. Illustrate the output of SELECTION SORT after each pass on the following
numbers: 9, 4, 3, 1, 6.
Method 1:
import java.util.Scanner;
class Array
{
int[] nArray;
Array(int arrSize)
{
nArray = new int[arrSize];
}
void initialize()
----------------------------------------------------------Page 2 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)
void display()
{
for(int i=0; i<=nArray.length-1; i++)
System.out.print(" "+nArray[i]+" ");
System.out.println();
}
void arrange()//Ascending Order using MAX approach
{
int max, pos;
for(int i=0; i<=nArray.length-2; i++)//Number of iterations
{
//Initializing max and pos
max = nArray[0];
pos = 0;
for(int j=0; j<=nArray.length-1-i; j++)//Processing of array elements
{
if(nArray[j]>max)//finding the max element
{
max = nArray[j];
pos = j;
}
}//end of the inner for loop
//Swapping the max element at pos with element at ith position
nArray[pos]=nArray[nArray.length-1 - i];
nArray[nArray.length-1 - i] = max;
System.out.print("Pass/Itertation: "+(i+1)+" ==> ");
----------------------------------------------------------Page 3 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)
display();
Output:
nArray[i] = sc.nextInt();
}
void display()
{
for(int i=0; i<=nArray.length-1; i++)
System.out.print(" "+nArray[i]+" ");
System.out.println();
}
void arrange()//Ascending Order
{
int temp;
Output:
----------------------------------------------------------Page 5 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)