-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from rpjayasekara/heapSort-java
Heap sort java
- Loading branch information
Showing
6 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
def isSubsetSum(set,n, sum) : | ||
|
||
# Base Cases | ||
if (sum == 0) : | ||
return True | ||
if (n == 0 and sum != 0) : | ||
return False | ||
|
||
# If last element is greater than | ||
# sum, then ignore it | ||
if (set[n - 1] > sum) : | ||
return isSubsetSum(set, n - 1, sum) | ||
|
||
# else, check if sum can be obtained | ||
# by any of the following | ||
# (a) including the last element | ||
# (b) excluding the last element | ||
return isSubsetSum(set, n-1, sum) or isSubsetSum(set, n-1, sum-set[n-1]) | ||
|
||
|
||
# Test case | ||
set = [3, 34, 4, 12, 5, 2] | ||
sum = 100 | ||
n = len(set) | ||
if (isSubsetSum(set, n, sum) == True) : | ||
print("Found a subset with given sum") | ||
else : | ||
print("No subset with given sum") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class BinarySearchTree { | ||
|
||
/* Class containing left and right child of current node and key value*/ | ||
class Node { | ||
int key; | ||
Node left, right; | ||
|
||
public Node(int item) { | ||
key = item; | ||
left = right = null; | ||
} | ||
} | ||
|
||
// Root of BST | ||
Node root; | ||
|
||
// Constructor | ||
BinarySearchTree() { | ||
root = null; | ||
} | ||
|
||
// This method mainly calls insertRec() | ||
void insert(int key) { | ||
root = insertRec(root, key); | ||
} | ||
|
||
/* A recursive function to insert a new key in BST */ | ||
Node insertRec(Node root, int key) { | ||
|
||
/* If the tree is empty, return a new node */ | ||
if (root == null) { | ||
root = new Node(key); | ||
return root; | ||
} | ||
|
||
/* Otherwise, recur down the tree */ | ||
if (key < root.key) | ||
root.left = insertRec(root.left, key); | ||
else if (key > root.key) | ||
root.right = insertRec(root.right, key); | ||
|
||
/* return the (unchanged) node pointer */ | ||
return root; | ||
} | ||
|
||
// This method mainly calls InorderRec() | ||
void inorder() { | ||
inorderRec(root); | ||
} | ||
|
||
// A utility function to do inorder traversal of BST | ||
void inorderRec(Node root) { | ||
if (root != null) { | ||
inorderRec(root.left); | ||
System.out.println(root.key); | ||
inorderRec(root.right); | ||
} | ||
} | ||
|
||
// Driver Program to test above functions | ||
public static void main(String[] args) { | ||
BinarySearchTree tree = new BinarySearchTree(); | ||
|
||
/* Let us create following BST | ||
50 | ||
/ \ | ||
30 70 | ||
/ \ / \ | ||
20 40 60 80 */ | ||
tree.insert(50); | ||
tree.insert(30); | ||
tree.insert(20); | ||
tree.insert(40); | ||
tree.insert(70); | ||
tree.insert(60); | ||
tree.insert(80); | ||
|
||
// print inorder traversal of the BST | ||
tree.inorder(); | ||
} | ||
} | ||
// This code is contributed by Ankur Narain Verma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#Class which represents a node of binary tree | ||
class Node: | ||
def __init__(self,key): | ||
self.left = None | ||
self.right = None | ||
self.val = key | ||
|
||
#Function which add a key to binary tree | ||
def insert(root,node): | ||
if root is None: | ||
root = node | ||
else: | ||
if root.val < node.val: | ||
if root.right is None: | ||
root.right = node | ||
else: | ||
insert(root.right, node) | ||
else: | ||
if root.left is None: | ||
root.left = node | ||
else: | ||
insert(root.left, node) | ||
#Function to do inorder tree traversal | ||
def inorder(root): | ||
if root: | ||
inorder(root.left) | ||
print(root.val) | ||
inorder(root.right) | ||
|
||
#Function for searching a key in the tree | ||
def search(root,key): | ||
|
||
# Base Cases: root is null or key is present at root | ||
if root is None: | ||
return False | ||
elif root.val == key: | ||
return True | ||
|
||
# Key is greater than root's key | ||
if root.val < key: | ||
return search(root.right,key) | ||
|
||
# Key is smaller than root's key | ||
return search(root.left,key) | ||
|
||
|
||
#Test case | ||
|
||
r = Node(50) | ||
insert(r,Node(30)) | ||
insert(r,Node(20)) | ||
insert(r,Node(40)) | ||
insert(r,Node(70)) | ||
insert(r,Node(60)) | ||
insert(r,Node(80)) | ||
|
||
print "Result of traversing" | ||
inorder(r) | ||
|
||
|
||
print "Is 25 in the tree?", search(r,25) | ||
print "Is 30 in the tree?", search(r,30) | ||
print "Is 2 in the tree?", search(r,2) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class LinearSearch | ||
{ | ||
// This function returns index of element x in arr[] | ||
static int search(int arr[], int n, int x) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
// Return the index of the element if the element | ||
// is found | ||
if (arr[i] == x) | ||
return i; | ||
} | ||
|
||
// return -1 if the element is not found | ||
return -1; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Test | ||
{ | ||
// Pushing element on the top of the stack | ||
static void stack_push(Stack<Integer> stack) | ||
{ | ||
for(int i = 0; i < 5; i++) | ||
{ | ||
stack.push(i); | ||
} | ||
} | ||
|
||
// Popping element from the top of the stack | ||
static void stack_pop(Stack<Integer> stack) | ||
{ | ||
System.out.println("Pop :"); | ||
|
||
for(int i = 0; i < 5; i++) | ||
{ | ||
Integer y = (Integer) stack.pop(); | ||
System.out.println(y); | ||
} | ||
} | ||
|
||
// Displaying element on the top of the stack | ||
static void stack_peek(Stack<Integer> stack) | ||
{ | ||
Integer element = (Integer) stack.peek(); | ||
System.out.println("Element on stack top : " + element); | ||
} | ||
|
||
// Searching element in the stack | ||
static void stack_search(Stack<Integer> stack, int element) | ||
{ | ||
Integer pos = (Integer) stack.search(element); | ||
|
||
if(pos == -1) | ||
System.out.println("Element not found"); | ||
else | ||
System.out.println("Element is found at position " + pos); | ||
} | ||
|
||
|
||
public static void main (String[] args) | ||
{ | ||
Stack<Integer> stack = new Stack<Integer>(); | ||
|
||
stack_push(stack); | ||
stack_pop(stack); | ||
stack_push(stack); | ||
stack_peek(stack); | ||
stack_search(stack, 2); | ||
stack_search(stack, 6); | ||
} | ||
} |