Skip to content

Commit 0fbc886

Browse files
Merge branch 'master' into neha-practice
2 parents e1dc09f + 4cea04f commit 0fbc886

37 files changed

+1184
-33
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codefortomorrow.advanced.chapter13.examples;
2+
3+
public class BinarySearch {
4+
5+
// Java implementation of recursive Binary Search
6+
// Returns index of x if it is present in arr[l..
7+
// r], else return -1
8+
public static int binarySearch(int arr[], int l, int r, int x) {
9+
if (r >= l) {
10+
int mid = l + (r - l) / 2;
11+
12+
// If the element is present at the
13+
// middle itself
14+
if (arr[mid] == x) return mid;
15+
16+
// If element is smaller than mid, then
17+
// it can only be present in left subarray
18+
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
19+
20+
// Else the element can only be present
21+
// in right subarray
22+
return binarySearch(arr, mid + 1, r, x);
23+
}
24+
25+
// We reach here when element is not present
26+
// in array
27+
return -1;
28+
}
29+
30+
// Driver method to test above
31+
public static void main(String args[]) {
32+
int arr[] = { 2, 3, 4, 10, 40 };
33+
int n = arr.length;
34+
int x = 10;
35+
int result = binarySearch(arr, 0, n - 1, x);
36+
if (result == -1) System.out.println(
37+
"Element not present"
38+
); else System.out.println("Element found at index " + result);
39+
}
40+
/* This code is contributed by Rajat Mishra
41+
https://www.geeksforgeeks.org/binary-search/ */
42+
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codefortomorrow.advanced.chapter13.examples;
2+
3+
public class Fibonacci {
4+
5+
public static int fibonacciRecursion(int nthNumber) {
6+
//use recursion
7+
if (nthNumber == 0) {
8+
return 0;
9+
} else if (nthNumber == 1) {
10+
return 1;
11+
}
12+
return (
13+
fibonacciRecursion(nthNumber - 1) +
14+
fibonacciRecursion(nthNumber - 2)
15+
);
16+
}
17+
18+
public static int fibonacciLoop(int nthNumber) {
19+
//use loop
20+
int previouspreviousNumber, previousNumber = 0, currentNumber = 1;
21+
for (int i = 1; i < nthNumber; i++) {
22+
previouspreviousNumber = previousNumber;
23+
previousNumber = currentNumber;
24+
currentNumber = previouspreviousNumber + previousNumber;
25+
}
26+
return currentNumber;
27+
}
28+
// Credit: https://dev.to/khalilsaboor/fibonacci-recursion-vs-iteration--474l
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.codefortomorrow.advanced.chapter13.practice;
2+
3+
public class RecurMergeSort {
4+
5+
public static void recurMergeSort(Comparable[] array, int start, int end) {}
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codefortomorrow.advanced.chapter13.solutions;
2+
3+
public class RecurMergeSort {
4+
5+
public static void recurMergeSort(Comparable[] array, int start, int end) {
6+
if (start >= end) {
7+
return;
8+
}
9+
int middle = (start + end) / 2;
10+
recurMergeSort(array, start, middle);
11+
recurMergeSort(array, middle + 1, end);
12+
int i = start;
13+
int j = middle + 1;
14+
Comparable[] sortedArray = new Comparable[end - start + 1];
15+
int k = 0;
16+
while (i <= middle && j <= end) {
17+
if (array[i].compareTo(array[j]) > 0) {
18+
sortedArray[k] = array[i];
19+
i++;
20+
} else {
21+
sortedArray[k] = array[j];
22+
j++;
23+
}
24+
k++;
25+
}
26+
while (i <= middle) {
27+
sortedArray[k] = array[i];
28+
i++;
29+
k++;
30+
}
31+
while (j <= end) {
32+
sortedArray[k] = array[j];
33+
j++;
34+
k++;
35+
}
36+
k = 0;
37+
for (int l = start; l <= end; l++) {
38+
array[l] = sortedArray[k];
39+
k++;
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codefortomorrow.advanced.chapter14.examples;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/
9+
*/
10+
public class BufferedReaderExample {
11+
12+
public static void main(String[] args) {
13+
BufferedReader br = null;
14+
BufferedReader br2 = null;
15+
16+
try {
17+
br = new BufferedReader(new FileReader("B:\\myfile.txt"));
18+
19+
//One way of reading the file
20+
System.out.println("Reading the file using readLine() method:");
21+
String contentLine = br.readLine();
22+
while (contentLine != null) {
23+
System.out.println(contentLine);
24+
contentLine = br.readLine();
25+
}
26+
27+
br2 = new BufferedReader(new FileReader("B:\\myfile2.txt"));
28+
29+
//Second way of reading the file
30+
System.out.println("Reading the file using read() method:");
31+
int num = 0;
32+
char ch;
33+
while ((num = br2.read()) != -1) {
34+
ch = (char) num;
35+
System.out.print(ch);
36+
}
37+
} catch (IOException ioe) {
38+
ioe.printStackTrace();
39+
}
40+
}
41+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package com.codefortomorrow.advanced.chapter14.examples
1+
package com.codefortomorrow.advanced.chapter14.examples;
2+
23
import java.io.*;
34

45
public class FileIO {
@@ -7,22 +8,20 @@ public static void main(String[] args) throws IOException {
78
FileInputStream in = null;
89
FileOutputStream out = null;
910
File file = new File("names.txt");
10-
11+
1112
try {
1213
in = new FileInputStream(file);
1314
out = new FileOutputStream("names_upper.txt");
1415
int c;
15-
while ((c = in.read()) != -1)
16-
out.write(Character.toUpperCase((char)c));
17-
16+
while ((c = in.read()) != -1) out.write(
17+
Character.toUpperCase((char) c)
18+
);
1819
} catch (IOException e) {
1920
System.out.println("An IO Exception occurred.");
20-
e.printStackTrace(); // Prints error output stream.
21+
e.printStackTrace(); // Prints error output stream.
2122
} finally {
22-
if (in != null)
23-
in.close();
24-
if (out != null)
25-
out.close();
23+
if (in != null) in.close();
24+
if (out != null) out.close();
2625
}
2726
}
2827
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
package com.codefortomorrow.advanced.chapter14.examples
1+
package com.codefortomorrow.advanced.chapter14.examples;
2+
23
import java.io.*;
34

45
public class TryWithResources {
56

67
public static void main(String[] args) throws IOException {
78
File file = new File("names.txt");
8-
9-
try (FileInputStream in = new FileInputStream(file);
10-
FileOutputStream out = new FileOutputStream("names_upper.txt")) {
119

10+
try (
11+
FileInputStream in = new FileInputStream(file);
12+
FileOutputStream out = new FileOutputStream("names_upper.txt")
13+
) {
1214
int c;
13-
while ((c = in.read()) != -1)
14-
out.write(Character.toUpperCase((char)c));
15-
15+
while ((c = in.read()) != -1) out.write(
16+
Character.toUpperCase((char) c)
17+
);
1618
} catch (IOException e) {
1719
System.out.println("An IO Exception occurred.");
18-
e.printStackTrace(); // Prints error output stream.
20+
e.printStackTrace(); // Prints error output stream.
1921
}
2022
}
2123
}

src/com/codefortomorrow/advanced/chapter14/practice/Average.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter14.practice;
2+
23
import java.io.*;
34

45
/*
@@ -24,8 +25,8 @@ and display the average on the next (101st)
2425
*/
2526

2627
public class Average {
27-
public static void main(String[] args) {
2828

29+
public static void main(String[] args) {
2930
// Add code here.
3031
}
3132
}

src/com/codefortomorrow/advanced/chapter14/practice/Bank.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ balance upon initialization (through the constructor).
2727
*/
2828

2929
public class Bank {
30-
31-
public static void main(String[] args) {
3230

31+
public static void main(String[] args) {
3332
// Your code here.
3433
}
3534
}
36-
3735
// Add other classes here.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
3+
import java.io.*;
4+
5+
/*
6+
Practice: Use File I/O to compare two files lexicographically.
7+
Lexicographical order is very similar to alphabetical order,
8+
except that it includes more than just lowercase alphabets.
9+
Given two text files f1 and f2, write a program that will
10+
compare their contents and output to the console the
11+
title of the file which comes first lexicographically.
12+
13+
Hint: Java’s String.compareTo method may be of use to you.
14+
*/
15+
16+
public class Lexico {
17+
18+
public static void main(String[] args) {
19+
// Add code here
20+
}
21+
}

0 commit comments

Comments
 (0)