Insertion Sort Java

package test;

public class InsertionSort {

public int[] sort(int []arr)
{
int key,i;
for(int j=1;j<arr.length;j++)
{
key=arr[j];
i=j-1;
while(i>=0 && arr[i]>key)
{
arr[i+1]=arr[i];
arr[i]=key;
i–;
}
}

return arr;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int []arr= {4,3,2,10,1};
InsertionSort sort=new InsertionSort();
int []target=sort.sort(arr);
for(int i=0;i<target.length;i++)
{
System.out.println(target[i]);
}
}

}

Advertisements
Posted in Data Structures, Java, Uncategorized | Tagged | Leave a comment

Java GC

https://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java#memory-management-in-java-8211-young-generation

Posted in Uncategorized | Leave a comment

Number Complement

https://leetcode.com/problems/number-complement/description/

public class Solution {

public int findComplement(int num) {
String binary=Integer.toBinaryString(num);
char []converted=binary.toCharArray();
for(int i=0;i<converted.length;i++)
{
if(converted[i]==’0′)
{
converted[i]=’1′;
}
else
{
converted[i]=’0′;
}

}
return Integer.parseInt(String.valueOf(converted),2);
}
public static void main(String[] args) {
Solution i1=new Solution();
System.out.println(“Binary value: “+i1.findComplement(5));
}

}

Posted in Uncategorized | Leave a comment

Remove Duplicates from an Array

package test;

import java.util.HashSet;
import java.util.Set;
import org.testng.annotations.Test;

public class ArrayDuplicate {

@Test
public void testRemoveArrayDuplicates()
{
int a[]= {1,2,1,3,4,2,5};
Set<Integer> set=new HashSet<Integer>();
for(int i=0;i<a.length;i++)
{
set.add(a[i]);
}

System.out.println(set);
}
}

Posted in Uncategorized | Leave a comment

Implement Queue in Java

package test;

import org.testng.annotations.Test;

class QNodes
{
QNodes front,rear,next;
int data;
public QNodes(int data)
{
this.data=data;
}
}
public class Queue {

QNodes front;
QNodes rear;
public void enqueue(int data)
{
if(front==null)
{
rear=new QNodes(data);
front=rear;
}

else
{
rear.next=new QNodes(data);
rear=rear.next;
}
}//end of enqueue
public int dequeue()
{
int data=0;
if(front==null)
{

System.out.println(“Queue is empty”);
System.exit(0);
}
else
{
data=front.data;
front=front.next;

}

return data;
}
public void display()
{
QNodes temp;
temp=front;
while(temp!=null)
{
System.out.println(“Data seen is “+temp.data);
temp=temp.next;
}
}

@Test
public void testQueue()
{
Queue queue=new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
queue.dequeue();
System.out.println(“Data seen after dequeueis “);
queue.display();
queue.dequeue();
System.out.println(“Data seen after dequeueis “);
queue.display();
queue.dequeue();
System.out.println(“Data seen after dequeueis “);
queue.display();
queue.dequeue();
System.out.println(“Data seen after dequeueis “);
queue.display();

}
}

Posted in Data Structures, Java, Uncategorized | Tagged | Leave a comment

Implement Stack Java

package test;

import org.testng.annotations.Test;

class Nodes
{
int data;
Nodes next;
Nodes prev;
public Nodes(int data)
{
this.data=data;
}
}
public class Stack {

Nodes top;

void push(int data)
{
//Nodes temp ;
Nodes temp=new Nodes(data);
temp.next=top;
top=temp;

} //end of push

public int pop()
{
int data = 0;
if(top==null)
{
System.out.println(“STACK IS EMPTY”);
System.exit(0);
}

else
{
data=top.data;
top=top.next;
}

return data;
}

public void display()
{
Nodes temp;
temp=top;
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}
}

@Test
public void testStack()
{
Stack st=new Stack();
st.push(10);
st.push(20);
st.push(30);
st.display();
int data=st.pop();
System.out.println(“Data popped is “+data);
st.display();
}
}

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment

LinkedList Java

class Node
{
int data;
Node next;

public Node(int data)
{
this.data=data;
}

}

public class TestLL{

@Test
public void testLinkedList()
{
Node n1=new Node(10);
Node n2=new Node(20);
n1.next=n2;
Node temp;
temp=n1;
//Display LinkedList
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}

//Deleting a node
if(n1.next.data==20)
{
n1.next=n1.next.next;
}

Node temp1;
temp1=n1;
while(temp1!=null)
{
System.out.println(“After deleting data “+temp1.data);
temp1=temp1.next;
}

}

 

}

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment