0% found this document useful (0 votes)
54 views16 pages

Name - Lokesh Banthia Class - Xi A Roll Number - 10

The document contains code for various searching and sorting algorithms including linear search, binary search, selection sort, bubble sort, and code to validate an ISBN number. It provides Java code implementation of these algorithms along with sample inputs, outputs and explanations of the algorithms.

Uploaded by

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

Name - Lokesh Banthia Class - Xi A Roll Number - 10

The document contains code for various searching and sorting algorithms including linear search, binary search, selection sort, bubble sort, and code to validate an ISBN number. It provides Java code implementation of these algorithms along with sample inputs, outputs and explanations of the algorithms.

Uploaded by

Lokesh Banthia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy