Sie sind auf Seite 1von 6

Class X Java (ICSE) Important Questions Fully Solved

---------------------------------------------------------------------------------------------------------------------------------

BUBBLE & SELECTION SORT


1. Illustrate the output of BUBBLE SORT after each pass on the following
numbers: 9, 4, 3, 1, 6.

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)

Class X Java (ICSE) Important Questions Fully Solved


---------------------------------------------------------------------------------------------------------------------------------

for(int i=0; i<=nArray.length-2; i++)//Number of iterations


{
for(int j=0; j<=nArray.length-2; j++)//Processing of array elements
if(nArray[j]>nArray[j+1])
{
temp=nArray[j];
nArray[j]=nArray[j+1];
nArray[j+1]=temp;
}
System.out.print("Pass/Itertation: "+(i+1)+" ==> ");
display();
}

public static void main()


{
Array arrObj = new Array(5);
arrObj.initialize();
arrObj.arrange();

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)

Class X Java (ICSE) Important Questions Fully Solved


---------------------------------------------------------------------------------------------------------------------------------

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 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)

Class X Java (ICSE) Important Questions Fully Solved


---------------------------------------------------------------------------------------------------------------------------------

display();

}//end of outer for loop

public static void main()


{
Array arrObj = new Array(5);
arrObj.initialize();
arrObj.arrange();

Output:

Method 2: (Similar to that of bubble sorting)


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++)
----------------------------------------------------------Page 4 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)

Class X Java (ICSE) Important Questions Fully Solved


---------------------------------------------------------------------------------------------------------------------------------

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;

for(int i=0; i<=nArray.length-2; i++)//Number of iterations


{
for(int j=i+1; j<=nArray.length-1; j++)//Processing of array elements
if(nArray[i]>nArray[j])
{
temp=nArray[j];
nArray[j]=nArray[i];
nArray[i]=temp;
}
System.out.print("Pass/Itertation: "+(i+1)+" ==> ");
display();
}

public static void main()


{
Array arrObj = new Array(5);
arrObj.initialize();
arrObj.arrange();

Output:
----------------------------------------------------------Page 5 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)

Class X Java (ICSE) Important Questions Fully Solved


---------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------Page 6 of 6-----------------------------------------------------Prepared by Radhika Sehgal ( October 2011 ver1.0)

Das könnte Ihnen auch gefallen