You are on page 1of 16

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 java.util.*;

class Linear_Search

public static int search(int arr[], int x)

int n = arr.length;

for(int i = 0; i < n; i++)

if(arr[i] == x)

return i;

return -1;

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of terms to be inputed");

int n = sc.nextInt();

int arr[]=new int[n];


System.out.println("Enter the numbers");

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

System.out.println("Enter the number to be searched");

int x = sc.nextInt();

int result = search(arr, x);

if(result == -1)

System.out.print("Element is not present in array");

else

System.out.print("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 java.util.*;

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(System.in);

Binary_Search ob = new Binary_Search();

System.out.println("Enter number of terms to be inputed");

int n = sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter the numbers");

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

System.out.println("Enter the number to be searched");

int x = sc.nextInt();

int result = ob.binarySearch(arr, 0, n - 1, x);

if (result == -1)

System.out.println("Element not present");

else
System.out.println("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 java.util.*;

class Selection_Sort

void sort(int arr[])

int n = arr.length;

// 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 = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i]+" ");

System.out.println();

// Driver code to test above

public static void main()

Selection_Sort ob = new Selection_Sort();

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of terms to be inputed");

int n = sc.nextInt();

int arr[]=new int[n];


System.out.println("Enter the numbers");

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

ob.sort(arr);

System.out.println("Sorted array");

ob.printArray(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 java.util.*;

class Bubble_Sort

void bubbleSort(int arr[])

int n = arr.length;

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 = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

// Driver method to test above

public static void main()

Bubble_Sort ob = new Bubble_Sort();

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of terms to be inputed");

int n = sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter the numbers");


for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

ob.bubbleSort(arr);

System.out.println("Sorted array");

ob.printArray(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 java.util.*;

class ISBN_Verification

static boolean isValidISBN(String isbn)

// length must be 10

int n = isbn.length();

if (n != 10)
return false;

// Computing weighted sum

// of first 9 digits

int sum = 0;

for (int i = 0; i < 9; i++)

int digit = isbn.charAt(i) - '0';

if (0 > digit || 9 < digit)

return false;

sum += (digit * (10 - i));

// Checking last digit.

char last = isbn.charAt(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()

System.out.println("Enter the ISBN Number");

Scanner sc=new Scanner(System.in);

String isbn = sc.nextLine();

if (isValidISBN(isbn))

System.out.print("Valid");

else

System.out.print("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

You might also like