I am interested to know whether the time complexity of following algorithm is $O(n^2)$ or $O(n\log n)$.
Here is the implementation:
public class HelloWorld
{
public static void main(String[] args)
{
Hello hw = new Hello();
int[] n = {2,3,4,52,5,63,0,664,2,2,56,68,8};
hw.sortUsingDLLWODup(n);
}
}
public class Node
{
public int value;
public Node next;
public Node prev;
public Node(int v) {
value = v;
}
}
public class Hello
{
public void sortUsingDLLWODup(int[] n) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i = 0;i<n.length;i++) {
if(max<n[i])max = n[i];
if(min>n[i])min = n[i];
}
Node root = new Node(min);
Node end = new Node(max);
root.next = end;
end.prev = root;
for(int i = 0;i<n.length;i++)
{
Node current = root;
while(n[i]>current.value && current!=null) {
current = current.next;
}
if(current!=null&¤t.value>n[i]) {
Node temp = current.prev;
Node newNode = new Node(n[i]);
temp.next = newNode;
newNode.prev = temp;
newNode.next = current;
current.prev = newNode;
}
}
Node test = root;
while(test!=null){
System.out.println(test.value);
test = test.next;
}
}
}