Skip to content

Commit eca7a0e

Browse files
Merge pull request TheAlgorithms#80 from varunu28/master
Updated LinearSearch.java
2 parents a0aed9a + 13bd036 commit eca7a0e

File tree

2 files changed

+139
-59
lines changed

2 files changed

+139
-59
lines changed

Conversions/DecimalToAnyBase.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayList;
4+
5+
/**
6+
*
7+
* @author Varun Upadhyay (https://github.com/varunu28)
8+
*
9+
*/
10+
11+
// Driver Program
12+
public class DecimalToAnyBase {
13+
public static void main (String[] args) throws Exception{
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
System.out.println("Enter the decimal input below: ");
16+
int decInput = Integer.parseInt(br.readLine());
17+
System.out.println();
18+
19+
System.out.println("Enter the base below: ");
20+
int base = Integer.parseInt(br.readLine());
21+
System.out.println();
22+
23+
System.out.println("Decimal Input" + " is: " + decInput);
24+
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
25+
26+
br.close();
27+
}
28+
29+
/**
30+
* This method produces a String value of any given input decimal in any base
31+
* @param inp Decimal of which we need the value in base in String format
32+
* @return string format of the converted value in the given base
33+
*/
34+
35+
public static String convertToAnyBase(int inp, int base) {
36+
ArrayList<Character> charArr = new ArrayList<>();
37+
38+
while (inp > 0) {
39+
charArr.add(reVal(inp%base));
40+
inp /= base;
41+
}
42+
43+
StringBuilder str = new StringBuilder(charArr.size());
44+
45+
for(Character ch: charArr)
46+
{
47+
str.append(ch);
48+
}
49+
50+
return str.reverse().toString();
51+
}
52+
53+
/**
54+
* This method produces character value of the input integer and returns it
55+
* @param num integer of which we need the character value of
56+
* @return character value of input integer
57+
*/
58+
59+
public static char reVal(int num) {
60+
if (num >= 0 && num <= 9)
61+
return (char)(num + '0');
62+
else
63+
return (char)(num - 10 + 'A');
64+
}
65+
}

Searches/LinearSearch.java

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
1-
import java.util.Scanner;
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
4+
/**
5+
*
6+
* @author Varun Upadhyay (https://github.com/varunu28)
7+
*
8+
*/
29

310
public class LinearSearch{
4-
/**
5-
* The main method
6-
* @param args Command line arguments
7-
*/
8-
public static void main(String[] args){
9-
10-
Scanner input = new Scanner(System.in);
11-
int[] myArray;
12-
int size = 0;
13-
14-
//Prompt user to create array and its elements
15-
System.out.print("Enter the array size: ");
16-
size = input.nextInt();
17-
myArray = new int[size];
18-
for (int i = 0; i < size; i++){
19-
System.out.print("For index " + i + ", enter an integer: ");
20-
myArray[i] = input.nextInt();
21-
}
22-
23-
//Prompt user to search for particular element
24-
System.out.print("Enter integer to search for: ");
25-
int key = input.nextInt();
26-
27-
//Output array and index of target element, if found
28-
printarray(myArray);
29-
System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key));
30-
31-
input.close();
32-
}
33-
34-
/**
35-
* Linear search method
36-
*
37-
* @param list List to be searched
38-
* @param key Key being searched for
39-
* @return Location of the key
40-
*/
41-
public static int linearsearch(int[] list, int key){
42-
43-
for (int i = 0; i< list.length; i++){
44-
if (list[i] == key){
45-
return i;
46-
}
47-
48-
} return -1;
49-
}
50-
/**
51-
* Helper Method
52-
*
53-
* @param a array to be printed
54-
*/
55-
public static void printarray(int[] a){
56-
System.out.print("The array is: ");
57-
for( int d: a){
58-
System.out.print(d+" ");
59-
}
60-
System.out.println();
61-
}
11+
/**
12+
* The main method
13+
* @param args Command line arguments
14+
*/
15+
public static void main(String[] args) throws Exception {
16+
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
19+
// Test for Integer inputs
20+
Integer[] myArray;
21+
int size = 0;
22+
23+
//Prompt user to create array and its elements
24+
System.out.print("Enter the array size: ");
25+
size = Integer.parseInt(br.readLine());
26+
myArray = new Integer[size];
27+
for (int i = 0; i < size; i++){
28+
System.out.print("For index " + i + ", enter an integer: ");
29+
myArray[i] = Integer.parseInt(br.readLine());
30+
}
31+
32+
//Prompt user to search for particular element
33+
System.out.print("Enter integer to search for: ");
34+
Integer key = Integer.parseInt(br.readLine());
35+
36+
//Output array and index of target element, if found
37+
System.out.printf("The integer %d is found in index %d\n", key, linearSearch(myArray, key));
38+
39+
// Test for String inputs
40+
String[] myArray1;
41+
int size1 = 0;
42+
43+
//Prompt user to create array and its elements
44+
System.out.print("Enter the array size: ");
45+
size1 = Integer.parseInt(br.readLine());
46+
myArray1 = new String[size];
47+
for (int i = 0; i < size1; i++){
48+
System.out.print("For index " + i + ", enter a String: ");
49+
myArray1[i] = br.readLine();
50+
}
51+
52+
//Prompt user to search for particular element
53+
System.out.print("Enter String to search for: ");
54+
String key1 = br.readLine();
55+
56+
//Output array and index of target element, if found
57+
System.out.printf("The string %s is found in index %d\n", key1, linearSearch(myArray1, key1));
58+
}
59+
60+
/**
61+
* Generic Linear search method
62+
*
63+
* @param array List to be searched
64+
* @param value Key being searched for
65+
* @return Location of the key
66+
*/
67+
public static <T extends Comparable<T>> int linearSearch(T[] array, T value) {
68+
int lo = 0;
69+
int hi = array.length - 1;
70+
for (int i = lo; i <= hi; i++) {
71+
if (array[i].compareTo(value) == 0) {
72+
return i;
73+
}
74+
}
75+
return -1;
76+
}
6277
}

0 commit comments

Comments
 (0)