mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
I have also deleted these files in my commit
This commit is contained in:
@ -1,74 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Implements a Binary Search in Java
|
||||
*
|
||||
* @author unknown
|
||||
*/
|
||||
class BinarySearch
|
||||
{
|
||||
/**
|
||||
* This method implements the Binary Search
|
||||
*
|
||||
* @param array The array to make the binary search
|
||||
* @param key The number you are looking for
|
||||
* @param lb The lower bound
|
||||
* @param up The upper bound
|
||||
* @return the location of the key
|
||||
**/
|
||||
public static int BS(int array[], int key, int lb, int ub)
|
||||
{ if (lb>ub)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mid=(lb+ub)/2;
|
||||
|
||||
if (key<array[mid])
|
||||
{
|
||||
return (BS(array, key, lb, mid-1));
|
||||
}
|
||||
else if (key>array[mid])
|
||||
{
|
||||
return (BS(array, key, mid+1, ub));
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is the main method of Binary Search
|
||||
*
|
||||
* @author Unknown
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input=new Scanner(System.in);
|
||||
int array[]=new int[10] ;
|
||||
int key;
|
||||
|
||||
//Input
|
||||
System.out.println("Enter an array of 10 numbers : ");
|
||||
for (int i=0; i<10 ;i++ )
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
System.out.println("Enter the number to be searched : ");
|
||||
key=input.nextInt();
|
||||
|
||||
int index=BS(array, key, 0, 9);
|
||||
if (index!=-1)
|
||||
{
|
||||
System.out.println("Number found at index number : " + index);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Not found");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#import java.util.*;
|
||||
class Binary_Decimal
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%10;
|
||||
s+=d*(int)Math.pow(2,c++);
|
||||
k/=10;
|
||||
}
|
||||
System.out.println("Binary number:"+n);
|
||||
System.out.println("Decimal equivalent:"+s);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class BubbleSort
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
for(int j=i+1; j<6; j++)
|
||||
{
|
||||
if(array[j]>array[i])
|
||||
{
|
||||
int temp=array[j];
|
||||
array[j]=array[i];
|
||||
array[i]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#import java.util.*;
|
||||
class Decimal_Binary
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,s=0,c=0,d;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%2;
|
||||
s=s+d*(int)Math.pow(10,c++);
|
||||
k/=2;
|
||||
}//converting decimal to binary
|
||||
System.out.println("Decimal number:"+n);
|
||||
System.out.println("Binary equivalent:"+s);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#import java.util.*;
|
||||
class Decimal_Octal
|
||||
{
|
||||
public static void main()
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%8;
|
||||
s+=d*(int)Math.pow(10,c++);
|
||||
k/=8;
|
||||
}
|
||||
System.out.println("Decimal number:"+n);
|
||||
System.out.println("Octal equivalent:"+s);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class InsertionSort
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
int temp=array[i];
|
||||
int j=i-1;
|
||||
while(j>=0 && temp<array[j] )
|
||||
{
|
||||
array[j+1]=array[j];
|
||||
j--;
|
||||
}
|
||||
|
||||
array[j+1]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package sorts;
|
||||
/**
|
||||
* This is my implementation of an insertion sort.
|
||||
*
|
||||
* I decided to do this when i didn't feel comfortable enough about implementing
|
||||
* different types of sorts, so this is my trial and error to try and get myself to implement
|
||||
* the various sorts correctly.
|
||||
*
|
||||
* @author Kody C. Kendall
|
||||
*
|
||||
*/
|
||||
public class InsertionSort {
|
||||
|
||||
|
||||
public int[] insertionIntArraySort(int[] initialArray){
|
||||
|
||||
int[] sortedArray = new int[initialArray.length];
|
||||
|
||||
//Marking first element as sorted.
|
||||
sortedArray[0] = initialArray[0];
|
||||
|
||||
//For each element in the initialArray
|
||||
for (int index = 1; index < initialArray.length; index++){
|
||||
|
||||
//Finding the last index that was sorted
|
||||
int lastSortedIndex = index;
|
||||
|
||||
//Extracting the next element to be compared w/ other sorted elements from initial array
|
||||
int extractedElement = initialArray[index];
|
||||
|
||||
//Compare the values of the sorted index to the current extractedElement
|
||||
for (lastSortedIndex = index; lastSortedIndex > 0; lastSortedIndex--){
|
||||
|
||||
//If our extracted element is smaller than element to the right, switch them.
|
||||
if (sortedArray[lastSortedIndex-1] > extractedElement){
|
||||
//move the element to the left of extractedElement to the right in sortedArray
|
||||
sortedArray[lastSortedIndex] = sortedArray[lastSortedIndex-1];
|
||||
//And move the current extractedElement to the left one (since it's smaller).
|
||||
sortedArray[lastSortedIndex-1] = extractedElement;
|
||||
}
|
||||
else{
|
||||
//insert element where it is.
|
||||
sortedArray[lastSortedIndex] = extractedElement;
|
||||
//Terminating loop since element is in the right spot.
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sortedArray;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class SelectionSort
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
int min=i;
|
||||
for(int j=i+1; j<6; j++)
|
||||
{
|
||||
if(array[j]<array[min])
|
||||
{
|
||||
min=j;
|
||||
}
|
||||
}
|
||||
int temp=array[i];
|
||||
array[i]=array[min];
|
||||
array[min]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
import java.io.*;
|
||||
class stringreverse
|
||||
{
|
||||
String reverseString(String str)
|
||||
{
|
||||
String reverse=" ";
|
||||
if(str.length()==1)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
else
|
||||
{
|
||||
reverse=reverse+str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
|
||||
return reverse;
|
||||
}
|
||||
}
|
||||
public static void main(String args[])
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the string");
|
||||
String srr=br.readLine();
|
||||
System.out.println("Reverse="+reverseString(srr));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user