0

. Basically what the following code does (suppose to) is, create a set of non-repeating random numbers, fill them up to an array which gets converted to a list, and sort them out. The problem is the nested for loops, i managed a work around but not even sure how it works. Secondely, i cant seem to sort it correctly, things repeat and out of bound errors pop up from time to time.

How the code works:

  1. Generate non-repeating random numbers
  2. Fill an array with them
  3. Use nested for loop to find the smallest value
  4. Insert that to a new array
  5. Remove it from the first array
  6. Repeat last 2 steps till the first array is empty and second array os filled in a sorted order

     import org.apache.commons.lang.ArrayUtils;
     import java.util.ArrayList;
     import java.util.Arrays;
     import java.util.*;
     import java.lang.*;
     import java.io.*;
    
     public class Sorter {
    
     public static void main(String[] args) {
    int[] process = fillArray(20,1,25);
    sorter(process,20);
    
    }
    
    public static int[] sorter(int array[],int size) {
    
    int[] useArray = array;
    
    Integer[] newArray = ArrayUtils.toObject(useArray);
    List<Integer> arrayList = new ArrayList(Arrays.asList(newArray));
    
    
    //System.out.println((arrayList));
    
    int counter = 1;
    int minval = 0;
    int diffsize = size - 1;
    int actualVal = 0;
    int storeArray[] = new int[size];
    int removeIndex =0;
    
    Integer[] newStore = ArrayUtils.toObject(storeArray);
    List<Integer> storeList = new ArrayList(Arrays.asList(newStore));
    
    System.out.println((arrayList));
    
    // Both loops messed up
    for (int i = 0; i < size+diffsize; i++) {
    
        for (int n = 0; n < size-1; n++) {
    
            if (arrayList.get(minval) < arrayList.get(counter)) {
                actualVal = arrayList.get(minval);
                 System.out.println((arrayList.get(minval)) + " Less than " + arrayList.get(counter));
                counter = counter + 1;
                removeIndex = minval;
            } else {
                actualVal = arrayList.get(counter);
                System.out.println((arrayList.get(counter)) + " Less than " + arrayList.get(minval));
                minval = counter;
                counter = counter + 1;
                removeIndex = counter;
            }
        }
    
       // System.out.println(actualVal);
        storeList.add(actualVal);
        arrayList.remove(actualVal); // need to remove the smallest value to repeat the sorting and get the next smallest value, but this is not removing it
        size = size - 1;
        counter = 1;
        minval = 0;
       // if (i + size == i) {
       //     storeList.set(i, arrayList.get(0));
       // }
       // System.out.println(removeIndex);
    
       // System.out.println(arrayList);
    }
    
    
         // System.out.println(storeList);
    
          int[] ints = new int[storeList.size()];
          int d = 0;
          for (Integer u : storeList) {
             ints[d++] = u;
          }
    
    
        return ints;
     }
    
    
      public static int randomNum(int lower,int upper){
        Random rand = new Random();
        int randomNum = lower + rand.nextInt((upper- lower) + 1);
        return randomNum;
     }
    
    
    
    
     public static int[] fillArray(int size,int lowerBound,int upperBound){
         int holdArray[] = new int[size];
    
         int rand = 0;
    
        for (int count =0;count < holdArray.length;count++){
          holdArray[count] = 0;
       }
    
        for (int count =0;count < holdArray.length;count++){
    
           rand = randomNum(lowerBound,upperBound);
           if (ArrayUtils.contains(holdArray, rand)) {
               while (ArrayUtils.contains(holdArray, rand)) {
                rand = randomNum(0, 20);
            }
        }
        holdArray[count] = rand;
    }
        // System.out.println(Arrays.toString(holdArray));
    
    //return holdArray;
    
    return holdArray;
    
    }
    
    
    
    }
    
user3227275
  • 176
  • 8
  • while (ArrayUtils.contains(holdArray, rand)) { rand = randomNum(0, 20); } You're not using your bounds. – JP Moresmau May 22 '15 at 18:17
  • 1
    Divide and conquer. Write a simple sort function first and test that. You don't want to use the built in sort methods so you can learn right? Read on bubble sort or something like that and implement that first. Then the rest will be easier. – JP Moresmau May 22 '15 at 18:19
  • I don’t get it. You have four different `Integer[]` arrays, three `int[]` arrays *and* two `ArrayList`s. Some of them are copies of the original array, some are filled with `null`s or zeros (none of them is empty). It’s not clear which you are actually reading during the sort, but it’s clear that appending to a non-empty list can’t produce the right result. – Holger May 22 '15 at 18:21

2 Answers2

1

Separate out the "find the smallest" and "insertion/deletion" from arrays into two methods and then use them.

This way the code will be more manageable. Below is a sample find_min method.

int find_min(int[] array, int start) {
    int min = Integer.MAX_VALUE;
    for(int i = start; i < array.length; ++i)
        if(array[i] < min)
            min = array[i] ;
    return min;
}

Now in the sorting routine, use the find_min to find the minimum element and insert it to new array and then delete the minimum element from the original array. However, this method does not return the index of the minimum element. So, I suggest you to modify it to return the index and the element as a pair of int values.

Your sorting routine will look something like this :

new_array := []
while(length(original_array) > 0)
    min, min_index := find_min(original_array)
    new_array.append(min)
    original_array.delete(min_index)

You can use something like this to return an int pair :

class IntPair {
    int min;
    int index;

    public IntPair(int x, int y) { this.min=x; this.index=y; }

    public int get_min() { return min; }
    public int get_min_index() { return index; }
}

Also, since you will de doing insertion and deletion, use ArrayList instead. It has methods to remove element at a particular index and append elements to it.

Note: Your approach outlined is close to Selection Sort algorithm in which we divide the array into two parts (sorted left part and unsorted right part) and repeatedly pick the smallest element from the right of the array and swap it with the left part's rightmost element.

min := array[0]
for(i := 0; i < array.length; ++i)
    for(j := i+1; j < array.length; ++j)
        if(array[j] < min)
            min = array[j]
            break
    swap(array[i], array[j])

In this case, you dont need two arrays and you dont need to delete or insert elements into an array either.

a_pradhan
  • 3,135
  • 1
  • 15
  • 23
1

Can you give a compelling reason to justify in converting from array to list? why not use only list or only array altogether? I use ArrayList in my answer below;

First of, the fillArray class. You do not need to fill with all zeros. Why even bother to fill it with a value that you will replace anyway?

public static ArrayList<Integer> fillArray(int size,int lowerBound,int upperBound){
    ArrayList<Integer> a = new ArrayList<Integer>(size);
    for (int count =0;count < size;count++){
       Integer rand = new Integer(randomNum(lowerBound,upperBound));
       a.add(rand);
    }
    return a;
}

Second, the sorting class. Your method as you said, searching for lowest value then do magic stuff and what not.

public static ArrayList<Integer> sorter(ArrayList<Integer> unsorted) {
ArrayList<Integer> sortedArray = new ArrayList<Integer>(unsorted.size());
while(!unsorted.isEmpty()) { //repeats until the unsorted list is empty
    int minval = unsorted.get(0);
    int removeIndex = 0;
    for(int i=1;i<unsorted.size();i++) 
        if (unsorted.get(i)<minval) {
            minval = unsorted.get(i);
            removeIndex = i;
        }
    sortedArray.add(minval);
    unsorted.remove(removeIndex);
    }
    return sortedArray;
}

main method to test it

public static void main(String[] args) {
    ArrayList<Integer> a = fillArray(20,1,25);
    System.out.println("unsorted array");
    for (Integer c : a)
        System.out.print(c + ";");
    ArrayList<Integer> b = sorter(a);
    System.out.println("\nnew unsorted array");
    for (Integer c : a)
        System.out.print(c + ";");
    System.out.println("\nsorted array");
    for (Integer c : b)
        System.out.print(c + ";");
}

this outputs

unsorted array
22;2;23;22;13;12;4;1;7;14;25;18;9;12;3;8;20;3;1;20;
new unsorted array

sorted array
1;1;2;3;3;4;7;8;9;12;12;13;14;18;20;20;22;22;23;25;
Isaac
  • 158
  • 10
  • Thanks, that cleared a lot of things. The reason why i chose lists was because , array sizes are fixed and can't expand, where else i can manupilate lists way easier. – user3227275 May 22 '15 at 19:21
  • okay, but ArrayList is pretty much not needed here. Both implementation of ArrayList and Array will be the same in your case since we already define the size of the array in the beginning when you call fillArray. – Isaac May 22 '15 at 19:29
  • So, in your opinion which is better in terms of performance,simplicity and functionality? – user3227275 May 23 '15 at 07:42
  • @user3227275 that is a common question, you can dozens of question here at stackoverflow about [ArrayList vs array](http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster), let alone google :). – Isaac May 23 '15 at 12:14