Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Random_java/AVLTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.cdw.dsapart2;

public class AVLTree {
AVLnode root;
AVLTree(){
root = null;
}
void add(int v)
{
root = add(root,v);
}
private int hgt(AVLnode a)
{
return (a==null)? -1:a.height;
}
private AVLnode add(AVLnode root,int v)
{
if(root==null)
return new AVLnode(v);;
if(v<root.value)
root.left = add(root.left,v);
if(v>root.value)
root.right =add(root.right,v);
root.height = Math.max(hgt(root.left),hgt(root.right))+1;
return balance(root);
}
AVLnode balance(AVLnode root)
{
if(isleftheavy(root)) {
if(balancefactor(root.left)<0)
root.left = leftrotate(root.left);
rightrotate(root);
}
else if(isrightheavy(root)) {
if(balancefactor(root.right)>0)
{
root.right = rightrotate(root.right);
}
leftrotate(root);
}
return (root);
}
void sethgt(AVLnode a)
{
a.height = Math.max(hgt(a.left),hgt(a.right))+1;
}
AVLnode rightrotate(AVLnode root)
{
AVLnode newRoot = root.left;
root.left = newRoot.right;
newRoot.right = root;
sethgt(root);
sethgt(newRoot);
return newRoot;
}
AVLnode leftrotate(AVLnode root)
{
AVLnode newRoot = root.right;
root.right = newRoot.left;
newRoot.left = root;
sethgt(root);
sethgt(newRoot);
return newRoot;
}
int balancefactor(AVLnode A)
{
return A==null?-1:hgt(A.left)-hgt(A.right);
}
boolean isleftheavy(AVLnode A)
{
return balancefactor(A)>1? true : false;
}
boolean isrightheavy(AVLnode A)
{
return balancefactor(A)<-1? true : false;
}
}
22 changes: 22 additions & 0 deletions Random_java/AVLnode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cdw.dsapart2;

public class AVLnode {
public int value;
public AVLnode right;
public AVLnode left;
int height;
AVLnode(){
value = 0;
left = null;
right = null;
}
AVLnode(int val)
{
value=val;
}
@Override
public String toString()
{
return "Node:"+value;
}
}
33 changes: 33 additions & 0 deletions Random_java/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class BinarySearch {
public int binarysearchR(int a[],int target)
{
return binarysearchR(a,target,0,a.length-1);
}
int binarysearchR(int a[], int target, int left, int right)
{
if(right<left)
return -1;
int mid = (left + right)/2;
if(a[mid]==target)
return mid;
if(target<a[mid])
return binarysearchR(a,target,left,mid-1);
return binarysearchR(a,target,mid+1,right);
}
int binarysearchI(int[] arr,int target)
{
var left =0;
var right = arr.length-1;
while(left<=right)
{
var mid = (left+right)/2;
if(arr[mid]==target)
return mid;
if(target<arr[mid])
right=mid-1;
else
left=mid+1;
}
return -1;
}
}
24 changes: 24 additions & 0 deletions Random_java/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class BubbleSort {
public void sort(int[] array)
{
for(int i= 0;i<array.length;i++)
{
for(int j=1;j<array.length-i;j++)
{
if(array[j]<array[j-1])
{
//var temp = array[j];
//array[j]=array[j-1];
//array[j-1]=temp;
swap(array,j,j-1);
}
}
}
}
void swap(int a[],int j,int j2)
{
var temp = a[j];
a[j]=a[j2];
a[j2]=temp;
}
}
27 changes: 27 additions & 0 deletions Random_java/BucketSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class BucketSort {
void sort(int arr[],int nob){
var i=0;
for(var bucket : createbuckets(arr,nob))
{
Collections.sort(bucket);
for(var item:bucket)
arr[i++] = item;
}
}
List<List<Integer>> createbuckets(int arr[],int nob)
{
List<Integer> x = new ArrayList<>();
List<List<Integer>> buckets = new ArrayList<>();
for(var i=0;i<nob;i++)
buckets.add(new ArrayList<>());

for(var item : arr)
buckets.get(item/nob).add(item);
return buckets;
}
}
40 changes: 40 additions & 0 deletions Random_java/Chaining.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.helloworld;

import java.util.HashMap;
import java.util.Map;

public class Chaining {

Map<Integer, LinkedList> map = new HashMap<>();
int n = 5;
int Hash(int val)
{
return val % n;
}
void add(int val)
{
int key = Hash(val);
if(map.containsKey(key))
{
LinkedList temp;
temp = map.get(key);
temp.addLast(val);
map.put(key,temp);
}
else
{
LinkedList temp;
temp = new LinkedList();
temp.addLast(val);
map.put(key,temp);
}
}
void print()
{
for (var val: map.entrySet()
) {
System.out.print(val.getKey() + " ");
val.getValue().print();
}
}
}
43 changes: 43 additions & 0 deletions Random_java/CircularQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.helloworld;

public class CircularQ {
int a[] ;
int n;
int front;
int rear;
int count;
CircularQ(int val)
{
n=val;
rear=0;
a = new int[n];
count = 0;
}

boolean isEmpty()
{
if(front == -1)
return true;
else
return false;
}
void add(int val)
{
a[rear]=val;
rear = (rear+1)% n;
count++;
}
void delete()
{
a[front]=0;
front=(front+1)%n;
count--;
}
void print()
{
for (int elem :
a) {
System.out.println(elem);
}
}
}
14 changes: 14 additions & 0 deletions Random_java/CountingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class CountingSort {
void sort(int[] arr,int max)
{
int [] counts = new int[max+1];
for(var item : arr)
{
counts[item]++;
}
int k=0;
for(var i=0;i<counts.length;i++)
for(var j=0;j<counts[i];j++)
arr[k++] = i;
}
}
12 changes: 12 additions & 0 deletions Random_java/ExponentialSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class ExponentialSearch {
int search(int a[],int target)
{
int bound =1;
while(bound < a.length && a[bound]<target)
bound*=2;
var search = new BinarySearch();
int left = bound/2;
int right = Math.min(bound,a.length-1);
return search.binarysearchR(a,target,left,right);
}
}
Loading