Skip to content

Commit 0a4860c

Browse files
[Shivani] : added algorithm problems
0 parents  commit 0a4860c

7 files changed

+311
-0
lines changed

src/com/bridgelabz/Anagram.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.bridgelabz;
2+
3+
import java.util.*;
4+
/*
5+
* 6. An Anagram Detection Example
6+
* a. Desc -> One string is an anagram of another if the second is simply
7+
* a rearrangement of the first. For example, 'heart' and 'earth' are anagrams...
8+
* b. I/P -> Take 2 Strings as Input such abcd and dcba and Check for Anagrams
9+
* c. O/P -> The Two Strings are Anagram or not....
10+
*/
11+
public class Anagram {
12+
13+
public static void main(String[] args) {
14+
Scanner sc = new Scanner(System.in);
15+
// taking two strings for two words
16+
System.out.println("Enter the first word");
17+
String str1 = sc.nextLine();
18+
System.out.println("Enter the second word");
19+
String str2 = sc.nextLine();
20+
// lower case
21+
str1 = str1.toLowerCase();
22+
str2 = str2.toLowerCase();
23+
// Checking length of two string
24+
if (str1.length() == str2.length()) {
25+
// if length are same this block willl execute
26+
// convert string into char array
27+
char[] charArray1 = str1.toCharArray();
28+
char[] charArray2 = str2.toCharArray();
29+
// if sorted array is same, string is anagram
30+
Arrays.sort(charArray1);
31+
Arrays.sort(charArray2);
32+
// checking two are same or not if true if block will execute or else will
33+
boolean result = Arrays.equals(charArray1, charArray2);
34+
if (result) {
35+
System.out.println(str1 + " and " + str2 + " are anagram");
36+
} else {
37+
System.out.println(str1 + " and " + str2 + " not anagram");
38+
}
39+
} else {
40+
// if length of two string not equal this block will execute
41+
System.out.println("invalid word");
42+
}
43+
sc.close();
44+
}
45+
}

src/com/bridgelabz/BinaryTree.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.bridgelabz;
2+
3+
import java.util.*;
4+
5+
public class BinaryTree {
6+
static Scanner sc = new Scanner(System.in);
7+
8+
int Search(int arr[], int l, int r, int x) {
9+
if (r >= l) {
10+
int mid = l + (r - l) / 2;
11+
12+
if (arr[mid] == x) // element is present at the mid
13+
{
14+
return mid;
15+
}
16+
// If element is smaller than mid, then it is present in left subarray
17+
if (arr[mid] > x)
18+
return Search(arr, l, mid - 1, x);
19+
else
20+
return Search(arr, mid + 1, r, x); // element present in right subarray
21+
}
22+
23+
return -1; // element is not present
24+
}
25+
26+
public static void main(String[] args) {
27+
BinaryTree bTree = new BinaryTree();
28+
System.out.println("Enter the no of elements: ");
29+
int n = sc.nextInt();
30+
int[] arr = new int[n];
31+
System.out.println("enter elements of binary tree");
32+
for (int i = 0; i < arr.length; i++) {
33+
arr[i] = sc.nextInt();
34+
}
35+
System.out.println("elemets are : ");
36+
for (int i = 0; i < arr.length; i++) {
37+
System.out.println(arr[i]);
38+
}
39+
System.out.println("enter value for x");
40+
int x = sc.nextInt();
41+
int result = bTree.Search(arr, 0, n - 1, x);
42+
if (result == -1)
43+
System.out.println("Element not found");
44+
else
45+
System.out.println("Element found at index " + result);
46+
}
47+
48+
}

src/com/bridgelabz/BubbleSort.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.bridgelabz;
2+
3+
public class BubbleSort {
4+
/*
5+
* 4. Bubble Sort
6+
* a. Desc -> Reads in integers prints them in sorted order using Bubble Sort
7+
* b. I/P -> read in the list ints
8+
* c. O/P -> Print the Sorted List
9+
*/
10+
11+
public void bubbleSort() {
12+
int[] a = { 9, 20, 55, 79, 44, 68, 66, 54, 35, 37, 25 };
13+
14+
// for loop for printing the elements of an array
15+
System.out.println("Elements of array are :");
16+
for (int i = 0; i < a.length; i++) {
17+
System.out.print(" " + a[i] + " ");
18+
}
19+
// nested for loop for bubble sort operation
20+
for (int i = 0; i < a.length; i++) {
21+
for (int j = 0; j < a.length - i - 1; j++) {
22+
if (a[j] > a[j + 1]) {
23+
int temp = a[j];
24+
a[j] = a[j + 1];
25+
a[j + 1] = temp;
26+
}
27+
}
28+
}
29+
System.out.println("\nElements of array after bubble sort is :");
30+
for (int i = 0; i < a.length; i++) {
31+
System.out.print(" " + a[i] + " ");
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
BubbleSort bs = new BubbleSort();
37+
bs.bubbleSort();
38+
}
39+
}

src/com/bridgelabz/InsertionSort.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bridgelabz;
2+
3+
public class InsertionSort {
4+
/*
5+
* 3. Insertion Sort
6+
* a. Desc -> Reads in strings and prints them in sorted order using insertion sort.
7+
* b. I/P -> read in the list words
8+
* c. Logic -> Use Insertion Sort to sort the words in the String array
9+
* d. O/P -> Print the Sorted List
10+
*/
11+
public void sort(String[] arr) {
12+
for (int i = 1; i < arr.length; ++i) {
13+
String key = arr[i];
14+
int j = i - 1;
15+
while (j >= 0 && key.length() < arr[j].length()) {
16+
arr[j + 1] = arr[j];
17+
j = j - 1;
18+
}
19+
arr[j + 1] = key;
20+
}
21+
for (int i = 0; i < arr.length; i++) {
22+
System.out.print(arr[i] + " ");
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
InsertionSort ob = new InsertionSort();
28+
String[] arr = { "Somiya", "is", "a", "good", "Girl" };
29+
System.out.println("Before:");
30+
for (int i = 0; i < arr.length; i++) {
31+
System.out.print(arr[i] + " ");
32+
}
33+
System.out.println("\nAfter: ");
34+
ob.sort(arr);
35+
}
36+
}

src/com/bridgelabz/MergeSort.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.bridgelabz;
2+
3+
public class MergeSort {
4+
5+
/*
6+
* 5. Merge Sort - Write a program to do Merge Sort of list of Strings.
7+
* a. Logic -> To Merge Sort an array, we divide it into two halves, sort the two halves
8+
* independently, and then merge the results to sort the full array. To sort a[lo, hi),
9+
* we use the following recursive strategy:
10+
* b. Base case: If the subarray length is 0 or 1, it is already sorted.
11+
* c. Reduction step: Otherwise, compute mid = lo + (hi - lo) / 2, recursively sort the
12+
* two subarrays a[lo, mid) and a[mid, hi), and merge them to produce a sorted
13+
* result.
14+
*/
15+
public void displayArray(int arr[], int n){
16+
for (int i=0; i<n; i++){
17+
System.out.print(arr[i]+" ");
18+
}
19+
System.out.println();
20+
}
21+
/*
22+
* Divie array to sub arrays and merge to function
23+
*/
24+
public void splitAndMerge(int arr[], int low, int high){
25+
if (low<high){
26+
int mid = (low+high) / 2;
27+
splitAndMerge(arr, low, mid);
28+
splitAndMerge(arr, mid+1, high);
29+
merge(arr, low, mid, high);
30+
}
31+
}
32+
/*
33+
* merge divided arrays to function
34+
*/
35+
public void merge(int arr[], int low, int mid, int high){
36+
int i = low, h = low, j = mid+1, k, temp[] = new int[10];
37+
while (h<=mid && j<=high){
38+
if (arr[h] <= arr[j]){
39+
temp[i] = arr[h];
40+
h++;
41+
}else {
42+
temp[i] = arr[j];
43+
j++;
44+
}
45+
i++;
46+
}
47+
if (h>mid){
48+
for (k=j; k<=high; k++){
49+
temp[i] = arr[k];
50+
i++;
51+
}
52+
}else {
53+
for (k=h; k<= mid; k++){
54+
temp[i] = arr[k];
55+
i++;
56+
}
57+
}
58+
for (k=low; k<=high; k++){
59+
arr[k] = temp[k];
60+
}
61+
}
62+
public static void main(String[] args)
63+
{
64+
MergeSort mergSort = new MergeSort();
65+
int arr[] = {48, 93, 28, 44, 1, 0, -1, 3};
66+
int len = arr.length;
67+
System.out.println("Before MergeSort the array elements : ");
68+
mergSort.displayArray(arr, len);
69+
/*
70+
* Dividing array to sub arrays
71+
*/
72+
mergSort.splitAndMerge(arr, 0, len-1);
73+
System.out.println("After MergeSort the array elements : ");
74+
mergSort.displayArray(arr, len);
75+
76+
}
77+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.bridgelabz;
2+
3+
public class PermutationOfString {
4+
/*
5+
* Write static functions to return all permutations of a String using iterative method and
6+
* Recursion method. Check if the arrays returned by two string functions are equal.
7+
*/
8+
static int count = 0;
9+
10+
public static void main(String[] args) {
11+
String str = "ABCD";
12+
int n = str.length();
13+
PermutationOfString permutation = new PermutationOfString();
14+
permutation.permutate(str, 0, n - 1);
15+
System.out.println("Number of permutactions for the given string is " + count);
16+
}
17+
18+
private void permutate(String str, int firstIndex, int lastIndex) {
19+
20+
if (firstIndex == lastIndex) {
21+
System.out.println(str);
22+
count++;
23+
} else {
24+
for (int i = firstIndex; i <= lastIndex; i++) {
25+
str = swap(str, firstIndex, i);
26+
permutate(str, firstIndex + 1, lastIndex);
27+
str = swap(str, firstIndex, i);
28+
}
29+
}
30+
}
31+
32+
public String swap(String a, int i, int j) {
33+
char temp;
34+
35+
char[] charArray = a.toCharArray();
36+
37+
temp = charArray[i];
38+
charArray[i] = charArray[j];
39+
charArray[j] = temp;
40+
41+
return String.valueOf(charArray);
42+
}
43+
44+
}

src/com/bridgelabz/PrimeNumber.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bridgelabz;
2+
3+
public class PrimeNumber {
4+
//Take a range of 0 - 1000 Numbers and find the Prime numbers in that range.
5+
public static boolean isPrime(int n) {
6+
if (n < 2) // if n is less than 2 it will return false
7+
return false;
8+
for (int i = 2; i < n; i++) {
9+
if (n % i == 0)
10+
return false;
11+
}
12+
return true;
13+
}
14+
public static void main(String[] args) {
15+
int lower = 0;
16+
int upper = 1000;
17+
System.out.println("Prime numbers between 0 to 1000 are "); // printing prime numbers till 1000
18+
for (int i = lower; i <= upper; i++)
19+
if (isPrime(i))
20+
System.out.println(i);
21+
}
22+
}

0 commit comments

Comments
 (0)