Skip to content

Commit be7d0fd

Browse files
committed
binarySearchSimple
1 parent 8da4c27 commit be7d0fd

4 files changed

Lines changed: 124 additions & 20 deletions

File tree

‎src/algo/sortingandsearching/InsertionSort.java‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@
1010
public class InsertionSort {
1111

1212
// sorts by shifting
13-
public static <T extends Comparable> void sort(T[] a){
13+
public static <T extends Comparable> void sort(T[] a) {
1414
for (int i = 1; i < a.length; i++) {
15-
for (int j = i; j >0; j--) {
16-
if(a[j-1].compareTo(a[j])>0){
17-
T temp=a[j-1];
18-
a[j-1]=a[j];
19-
a[j]=temp;
20-
}
15+
for (int j = i; j > 0; j--) {
16+
if (a[j - 1].compareTo(a[j]) > 0) {
17+
T temp = a[j - 1];
18+
a[j - 1] = a[j];
19+
a[j] = temp;
20+
}
2121
}
2222
}
2323
}
24+
2425
// sorts by swapping
25-
public static <T extends Comparable> void sortBySwap(T[] a){
26+
public static <T extends Comparable> void sortBySwap(T[] a) {
2627
for (int i = 1; i < a.length; i++) {
27-
int j=i;
28-
T ii=a[i];
28+
int j = i;
29+
T ii = a[i];
2930

30-
while (j>0 && a[j-1].compareTo(ii)>0) a[j]=a[--j];
31+
while (j > 0 && a[j - 1].compareTo(ii) > 0) a[j] = a[--j];
3132

32-
if(j!=i) a[j]=ii;
33+
if (j != i) a[j] = ii;
3334
}
3435
}
36+
3537
//simple version, easy to understand.
36-
public static void sortSimple(int[] a){
37-
for (int i = 1; i <a.length ; i++) {
38-
for (int j = i; j >0 ; j--) {
39-
if(a[j-1]>a[j]){ //swap
40-
int temp=a[j-1];
41-
a[j-1]=a[j];
42-
a[j]=temp;
38+
public static void sortSimple(int[] a) {
39+
for (int i = 1; i < a.length; i++) {
40+
for (int j = i; j > 0; j--) {
41+
if (a[j - 1] > a[j]) { //swap
42+
int temp = a[j - 1];
43+
a[j - 1] = a[j];
44+
a[j] = temp;
4345
}
4446
}
4547
}

‎src/algo/todo‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mergeSort
66
radixSort
77
heapSort
88

9-
109
binarySearch
1110
quickSelect
1211

‎src/ds/BST.java‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 12/16/16.
5+
*/
6+
// Binary Search Tree, two nodes (left, right) one parent.
7+
// insert -> O(h) can be O(n) if input is sorted
8+
// search -> O(h) can be O(n) if input is sorted
9+
// remove -> O(h) can be O(n) if input is sorted
10+
// no duplicate is allowed, for duplicate supported BST look BSTWithDuplicate.java
11+
// most methods are recursive, look BSTIterative.java for iterative approach BST
12+
13+
public class BST<K extends Comparable> {
14+
private Node root;
15+
16+
public void insert(K k) {
17+
root = putRecursive(root, k, null);
18+
}
19+
20+
public boolean search(K k) {
21+
return findRecursive(k, root);
22+
}
23+
public void delete(K k){
24+
25+
}
26+
public K findMax(){
27+
return findMaxRecursive(root);
28+
}
29+
public K findMin(){
30+
return findMinRecursive(root);
31+
}
32+
33+
private K findMaxRecursive(Node x) {
34+
if(x.right==null)return x.value;
35+
else return findMaxRecursive(x.right);
36+
}
37+
private K findMinRecursive(Node x) {
38+
if(x.left==null)return x.value;
39+
else return findMinRecursive(x.left);
40+
}
41+
42+
private boolean findRecursive(K k, Node x) {
43+
if (x == null) return false;
44+
if (x.value.equals(k))
45+
return true;
46+
else if (x.value.compareTo(k) > 0)
47+
return findRecursive(k, x.left);
48+
else
49+
return findRecursive(k, x.right);
50+
}
51+
52+
private Node putRecursive(Node x, K k, Node parent) {
53+
if (x == null) return new Node(k, parent);
54+
if (x.value.compareTo(k) > 0)
55+
x.left = putRecursive(x.left, k, x);
56+
else if (x.value.compareTo(k) < 0)
57+
x.right = putRecursive(x.right, k, x);
58+
x.size = 1 + size(x.left) + size(x.right);
59+
return x;
60+
}
61+
62+
63+
64+
//rank of tree
65+
private int size(Node x) {
66+
return x == null ? 0 : x.size;
67+
}
68+
69+
// BST node
70+
private class Node {
71+
K value;
72+
int size = 1;
73+
Node left;
74+
Node right;
75+
Node parent;
76+
77+
78+
public Node(K k, Node parent) {
79+
this.value = k;
80+
this.parent = parent;
81+
}
82+
}
83+
}

‎src/ds/BSTTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 12/16/16.
5+
*/
6+
public class BSTTest {
7+
public static void main(String[] args) {
8+
BST<Integer> bst=new BST<>();
9+
bst.insert(4);
10+
bst.insert(3);
11+
bst.insert(3);
12+
bst.insert(7);
13+
bst.insert(5);
14+
bst.insert(6);
15+
bst.insert(8);
16+
System.out.println(bst.search(7));
17+
System.out.println(bst.search(1));
18+
System.out.println(bst.search(2));
19+
}
20+
}

0 commit comments

Comments
 (0)