0% found this document useful (0 votes)
5 views7 pages

array_10th_array

The document contains Java implementations of various sorting and searching algorithms, including bubble sort and selection sort for both numbers and names, as well as linear and binary search methods. Each algorithm prompts the user for input, processes the data, and outputs the sorted or searched results. The code examples demonstrate fundamental programming concepts and data manipulation techniques in Java.

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views7 pages

array_10th_array

The document contains Java implementations of various sorting and searching algorithms, including bubble sort and selection sort for both numbers and names, as well as linear and binary search methods. Each algorithm prompts the user for input, processes the data, and outputs the sorted or searched results. The code examples demonstrate fundamental programming concepts and data manipulation techniques in Java.

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Bubble Sorting of Numbers

import java.util.*;
class bubble_sort_number
{
public static void main(String[] args)
{
System.out.println("enter size of array");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int temp;
System.out.println("enter number in array");
for (int i=0; i<n; i++)
{
a[i]=sc.nextInt();
}
for (int i=0 ; i<n; i++)
{
for (int j=0; j<n-1-i; j++)
{
if (a[j]>a[j + 1])
{
temp=a[j];
a[j]=a[j + 1];
a[j+1]=temp;
}
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}
Bubble Sorting of Names
import java.util.*;
class bubble_word
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
int n=sc.nextInt();
System.out.println("enter word in array");
String ar[]=new String[n];
for(int i=0;i<n;i++)
{
ar[i]=sc.nextLine();
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n-i-1; j++)
{
if (ar[j].compareTo(ar[j+1])>0)
{
String t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
}
}
System.out.println("\nsorted array");
for(int i=0;i<n;i++)
{
System.out.println(ar[i]);
}
}
}
Selection Sorting of Number
import java.util.*;
class selection_sorting_number
{
public static void main(String[] args)
{
System.out.println("enter size of array");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("enter number in array");
for (int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
int temp,min,j;
for (int i = 0; i < n; i++)
{
min = i;
for (j = i+1; j < n; j++)
{
if (a[j]<a[min])
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
for (int i = 0; i < n; i++)
{
System.out.print(a[i] + " ");
}
}
}
Selection Sorting of Names
import java.util.*;
class selection_sorting
{
public static void main(String[] args)
{
System.out.println("enter size of array");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String a[] = new String[n]; int min,j;
System.out.println("enter number in array");
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextLine();
}
String temp;
for (int i = 0; i < a.length; i++)
{
min = i;
for (j = i+1; j < a.length; j++)
{
if (a[j].compareTo(a[min])<0)
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i] + " ");
}
}
}
Linear Searching
import java.util.*;
class linear_searching
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter number in array");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println("enter number to search");
int number=sc.nextInt(); int c=0;
for(int i=0;i<a.length;i++)
{
if(number==a[i])
{
c++;
System.out.println("number found at "+i+" index number");
}
}
if(c==0)
System.out.println("number not found");
else
System.out.println("number found "+c+" times");
}
}
Binary Searching of Number
import java.util.*;
class binary_search_num
{
public static void main(String args[])
{
int i, n, item, first, last, middle;
Scanner sc = new Scanner(System.in);
System.out.println("enter number of elements");
n = sc.nextInt();
int array[] = new int[n];
System.out.println("enter " + n + " integers");
for (i = 0; i < n; i++)
{
array[i] = sc.nextInt();
}
System.out.println("enter the search value:");
item=sc.nextInt();
first=0; last=n-1;middle=(first+last)/2;
while( first <= last )
{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item+" found at "+middle +".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}
}
Binary Searching of Names
import java.util.*;
class binary_name
{
static int binarySearch(String[] arr, String x)
{
int l=0, r=arr.length-1;
while (l<=r)
{
int m=l+(r-l)/2;
int res=x.compareTo(arr[m]);
if (res==0)
return m;
if (res>0)
l=m+1;
else
r=m-1;
}
return -1;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
int n=sc.nextInt();
String arr[]=new String[n];
System.out.println("enter word in array");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLine();
}
System.out.println("enter word to search");
String x=sc.nextLine();
int result=binarySearch(arr, x);
if (result==-1)
System.out.println("word not present");
else
System.out.println("word found at" + "index "+result);
}
}

You might also like