NAME – LOKESH
BANTHIA
CLASS – XI A
ROLL NUMBER – 10
COMPUTER PROJECT
LINEAR SEARCH
// Java code for linearly searching x in arr[]. If x
// is present then return its location, otherwise
// return -1
import [Link].*;
class Linear_Search
public static int search(int arr[], int x)
int n = [Link];
for(int i = 0; i < n; i++)
if(arr[i] == x)
return i;
return -1;
public static void main()
Scanner sc=new Scanner([Link]);
[Link]("Enter number of terms to be inputed");
int n = [Link]();
int arr[]=new int[n];
[Link]("Enter the numbers");
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Enter the number to be searched");
int x = [Link]();
int result = search(arr, x);
if(result == -1)
[Link]("Element is not present in array");
else
[Link]("Element is present at index " + result);
OUTPUT –
1st -
Enter number of terms to be inputed
Enter the numbers
23
31
17
19
56
61
Enter the number to be searched
Element is not present in array
2nd –
Enter number of terms to be inputed
Enter the numbers
15
16
17
181
19
Enter the number to be searched
17
Element is present at index 2
Binary Search
// Java implementation of recursive Binary Search
import [Link].*;
class Binary_Search
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(int arr[], int l, int r, int x)
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
// We reach here when element is not present
// in array
return -1;
// Driver method to test above
public static void main()
Scanner sc=new Scanner([Link]);
Binary_Search ob = new Binary_Search();
[Link]("Enter number of terms to be inputed");
int n = [Link]();
int arr[]=new int[n];
[Link]("Enter the numbers");
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Enter the number to be searched");
int x = [Link]();
int result = [Link](arr, 0, n - 1, x);
if (result == -1)
[Link]("Element not present");
else
[Link]("Element found at index " + result);
OUTPUT –
1ST –
Enter number of terms to be inputed
Enter the numbers
23
45
43
Enter the number to be searched
43
Element found at index 3
2ND –
Enter number of terms to be inputed
Enter the numbers
3564
374
2763
72
Enter the number to be searched
337
Element not present
SELECTION SORT
// Java program for implementation of Selection Sort
import [Link].*;
class Selection_Sort
void sort(int arr[])
int n = [Link];
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
// Prints the array
void printArray(int arr[])
int n = [Link];
for (int i=0; i<n; ++i)
[Link](arr[i]+" ");
[Link]();
// Driver code to test above
public static void main()
Selection_Sort ob = new Selection_Sort();
Scanner sc=new Scanner([Link]);
[Link]("Enter number of terms to be inputed");
int n = [Link]();
int arr[]=new int[n];
[Link]("Enter the numbers");
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link](arr);
[Link]("Sorted array");
[Link](arr);
OUTPUT –
1ST –
Enter number of terms to be inputed
Enter the numbers
35
43846
35
314
85
Sorted array
2 4 35 35 85 314 43846
2ND –
Enter number of terms to be inputed
Enter the numbers
35
58
1235
34
567
Sorted array
34 35 58 567 1235
BUBBLE SORT
// Java program for implementation of Bubble Sort
import [Link].*;
class Bubble_Sort
void bubbleSort(int arr[])
int n = [Link];
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
/* Prints the array */
void printArray(int arr[])
int n = [Link];
for (int i=0; i<n; ++i)
[Link](arr[i] + " ");
[Link]();
// Driver method to test above
public static void main()
Bubble_Sort ob = new Bubble_Sort();
Scanner sc=new Scanner([Link]);
[Link]("Enter number of terms to be inputed");
int n = [Link]();
int arr[]=new int[n];
[Link]("Enter the numbers");
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link](arr);
[Link]("Sorted array");
[Link](arr);
OUTPUT –
1ST –
Enter number of terms to be inputed
Enter the numbers
253
252
47
83
47
36
Sorted array
1 36 47 47 83 252 253
2ND –
Enter number of terms to be inputed
Enter the numbers
13
56
435
768
Sorted array
3 4 13 56 435 768
ISBN NUMBER VERIFICATION
// Java program to check if
// a given ISBN isvalid or not
import [Link].*;
class ISBN_Verification
static boolean isValidISBN(String isbn)
// length must be 10
int n = [Link]();
if (n != 10)
return false;
// Computing weighted sum
// of first 9 digits
int sum = 0;
for (int i = 0; i < 9; i++)
int digit = [Link](i) - '0';
if (0 > digit || 9 < digit)
return false;
sum += (digit * (10 - i));
// Checking last digit.
char last = [Link](9);
if (last != 'X' && (last < '0' ||
last > '9'))
return false;
// If last digit is 'X', add 10
// to sum, else add its value
sum += ((last == 'X') ? 10 : (last - '0'));
// Return true if weighted sum
// of digits is divisible by 11.
return (sum % 11 == 0);
// Driver code
public static void main()
[Link]("Enter the ISBN Number");
Scanner sc=new Scanner([Link]);
String isbn = [Link]();
if (isValidISBN(isbn))
[Link]("Valid");
else
[Link]("Invalid");
OUTPUT –
1ST - 2ND –
Enter the ISBN Number Enter the ISBN Number
1681956993 2753738262
Valid Valid
3RD - 4TH-
Enter the ISBN Number Enter the ISBN Number
1232435666 47475759
Invalid Invalid