0% found this document useful (0 votes)
18 views26 pages

Java Programs for ISC Class 12

This document is a compilation of Java programs created by Pratyush Paul, referencing the ISC Computer Science Class 12 textbook. It includes examples of binary search and bubble sort algorithms, with simulated console outputs from the BlueJ IDE. The document acknowledges the textbook's contribution to the program development and is formatted for educational clarity.

Uploaded by

pratyushpaul005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Java Programs for ISC Class 12

This document is a compilation of Java programs created by Pratyush Paul, referencing the ISC Computer Science Class 12 textbook. It includes examples of binary search and bubble sort algorithms, with simulated console outputs from the BlueJ IDE. The document acknowledges the textbook's contribution to the program development and is formatted for educational clarity.

Uploaded by

pratyushpaul005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Acknowledgement

I, Pratyush Paul, would like to express my sincere gratitude to the creators of the
ISC Computer Science Class 12 textbook, whose content greatly helped in preparing
this compilation of Java programs.

This document has been created with reference to the examples, programs, and
exercises from the ISC Computer Science Class 12 book and the programs were
executed using the BlueJ IDE environment. The outputs shown are formatted to
replicate BlueJ-style console output for clarity and learning purposes.

- Pratyush Paul

File generated: Java_Programs_ISC_with_Ack.pdf

Page 1
Prog 15: Binary Search (Example 1)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 2
Prog 17: Bubble Sort (Descending) (Example 2)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 3
Prog 15: Binary Search (Example 3)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 4
Prog 17: Bubble Sort (Descending) (Example 4)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 5
Prog 15: Binary Search (Example 5)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 6
Prog 17: Bubble Sort (Descending) (Example 6)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 7
Prog 15: Binary Search (Example 7)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 8
Prog 17: Bubble Sort (Descending) (Example 8)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 9
Prog 15: Binary Search (Example 9)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 10
Prog 17: Bubble Sort (Descending) (Example 10)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 11
Prog 15: Binary Search (Example 11)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 12
Prog 17: Bubble Sort (Descending) (Example 12)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 13
Prog 15: Binary Search (Example 13)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 14
Prog 17: Bubble Sort (Descending) (Example 14)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 15
Prog 15: Binary Search (Example 15)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 16
Prog 17: Bubble Sort (Descending) (Example 16)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 17
Prog 15: Binary Search (Example 17)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 18
Prog 17: Bubble Sort (Descending) (Example 18)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 19
Prog 15: Binary Search (Example 19)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 20
Prog 17: Bubble Sort (Descending) (Example 20)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 21
Prog 15: Binary Search (Example 21)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 22
Prog 17: Bubble Sort (Descending) (Example 22)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 23
Prog 15: Binary Search (Example 23)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 24
Prog 17: Bubble Sort (Descending) (Example 24)
Program Code:

import [Link].*;
class Descending {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int num[] = new int[10];
[Link]("Enter 10 different numbers in the array:");
for (int i = 0; i < 10;
i++)
num[i] = [Link]();
for (int i = 0; i < 9; i++) {
for
(int j = 0; j < 9 - i; j++) {
if (num[j] < num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
[Link]("Numbers arranged in descending order are:
");
for (int i = 0; i < 10; i++)
[Link](num[i]);
}
}

BlueJ Console Output (simulated):

Enter 10 different numbers in the array:


34 12 89 45 67 23 10 56 78 90
Numbers arranged in descending order are:
90
89
78
67
56
45
34
23
12
10

Page 25
Prog 15: Binary Search (Example 25)
Program Code:

import [Link].*;
class BinSearch {
public static void main(String args[]) {
int
p, n, fst, last, mid, flag = 0;
int nl[] = new int[50];
Scanner in = new
Scanner([Link]);
[Link]("Number of elements to enter in the array:");
p = [Link]();
[Link]("Enter elements in ascending order:");
for
(int i = 0; i < p; i++)
nl[i] = [Link]();
[Link]("Enter a
number to search:");
n = [Link]();
fst = 0; last = p - 1;
while
(fst <= last) {
mid = (fst + last) / 2;
if (nl[mid] < n)
fst = mid + 1;
else if (nl[mid] > n)
last = mid - 1;
else {
flag = 1;
break;
}
}
if
(flag == 1)
[Link](n + " is present at location: " + mid);
else
[Link]("Not found in the array list!!");
}
}

BlueJ Console Output (simulated):

Number of elements to enter in the array:


5
Enter elements in ascending order:
12 23 34 45 56
Enter a number to search:
34
34 is present at location: 2

Page 26

You might also like