public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        ArrayList<ArrayList<Integer>> resultIndexList = new ArrayList<ArrayList<Integer>>();
        ArrayList<ArrayList<Integer>> resultList = new ArrayList<ArrayList<Integer>>();
        if(S == null || S.length == 0){
            return resultList;
        }

        resultIndexList.add(new ArrayList<Integer>());
        ArrayList<ArrayList<Integer>> lastResultList = new ArrayList<ArrayList<Integer>>(resultIndexList);
        for(int i = 1; i <= S.length ; i++){
            ArrayList<ArrayList<Integer>> lastResultListCopy = new ArrayList<ArrayList<Integer>>();
            for(ArrayList<Integer> itemList : lastResultList){
                if(itemList.size() == 0){
                    for (int index = 0; index < S.length; index++) {
                        ArrayList<Integer> tempList = new ArrayList<Integer>(itemList);
                        tempList.add(index);
                        resultIndexList.add(tempList);
                        lastResultListCopy.add(tempList);
                    }
                } else {
                    int lastElement = itemList.get(itemList.size() - 1);
                    for (int index = lastElement + 1; index <= S.length - 1; index++){
                        ArrayList<Integer> tempList = new ArrayList<Integer>(itemList);
                        tempList.add(index);
                        resultIndexList.add(tempList);
                        lastResultListCopy.add(tempList);
                    }
                }
            }
            lastResultList = lastResultListCopy;
        }
        
        for(ArrayList<Integer> tempList : resultIndexList){
            ArrayList<Integer> valueList = new   ArrayList<Integer>();
            for(Integer index : tempList){
                valueList.add(S[index]);
            }
            Collections.sort(valueList);
            resultList.add(valueList);
        }

        return resultList;
    }