Last active
August 31, 2016 04:08
-
-
Save chathurawidanage/92fbfecd53abd300770337044d220f7a to your computer and use it in GitHub Desktop.
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
package com.cwidanage.algo.ds; | |
import java.util.Arrays; | |
import java.util.Random; | |
/** | |
* @author Chathura Widanage | |
*/ | |
public class DataStructure { | |
public Object[] arrays = new Object[0]; | |
private int size = 0; | |
public DataStructure() { | |
//arrays[0] = null; | |
} | |
public void insert(Integer item) { | |
if(((size+1)&size)==0){//Math.log(size+1)/Math.log(2) == (int)(Math.log(size+1)/Math.log(2)) <-very stupid way used earlier | |
Object[] newarrays = new Object[arrays.length+1]; | |
for(int i=0;i<arrays.length;i++){ | |
newarrays[i]=arrays[i]; | |
} | |
newarrays[arrays.length]=null; | |
arrays=newarrays; | |
} | |
for (int i = 0; i < arrays.length; i++) { | |
if (arrays[i]==null){ | |
arrays[i]=new Object[(int)Math.pow(2,i)]; | |
((Object[])(arrays[i]))[0]=item; | |
size++; | |
int index=1; | |
for(int j=0;j<i;j++){ | |
for(int k=0;k<((Object[])(arrays[j])).length;k++){ | |
((Object[])(arrays[i]))[index]=((Object[])(arrays[j]))[k]; | |
index++; | |
} | |
arrays[j]=null; | |
} | |
Arrays.sort(((Object[]) (arrays[i]))); | |
break; | |
} | |
} | |
System.out.println(Arrays.deepToString(arrays)); | |
} | |
public static void main(String[] args) { | |
DataStructure ds=new DataStructure(); | |
Random r=new Random(); | |
for (int i=0;i<20;i++){ | |
ds.insert(r.nextInt(100)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment