开发者

Need help sorting an integer array through code

I've been working on code to sort integer in an array list, but my code is only returning only 2 numbers can anybody help me understand what I'm doing wrong here. How do I get all the numbers to be displayed? The numbers in my array

  • ArrayList testNumbers = new ArrayList();
  • testNumbers.add(48);
  • testNumbers.add(3);
  • testNumbers.add(23);
  • testNumbers.add(99);

[48,3,23,99]. Any help would be greatly appreciated.

public开发者_运维技巧 ArrayList<Integer> listSort(ArrayList<Integer> numbers) {

    // create variable to store max number
    int maxNumber = 0;

    // creates an array that will store the sorted numbers
    ArrayList<Integer> sortedIntArray = new ArrayList<Integer>();

    // loops through each number in the numbers arraylist
    for (int i = 0; i < numbers.size(); i++) {

        if (numbers.get(i) > maxNumber) {
            // set the number to the new max number
            maxNumber = numbers.get(i);

            // add current max number to sorted array
            sortedIntArray.add(maxNumber);

            // remove the max number from numbers array
            numbers.remove(numbers.get(i));

        }
    }



    return sortedIntArray;
}

//the sortedIntArray returns [48,99]


Use java.util.Collections.sort(List<T> list). Or are you required to implement your own sorting algorithm?


If you take a careful look at your code, it doesn't do anything if the number you are currently looking at is not bigger than maxNumber.

It adds in 48 because 48 > 0. Then it discards 3 and 23 because they are less than 48. then it adds 99 because it is greater than 48.


I believe you want to archive a classical Selection Sort.

Maybe try good old wikipedia: http://en.wikipedia.org/wiki/Selection_sort

public ArrayList<Integer> listSort(ArrayList<Integer> numbers) {

    // create variable to store max number
    int maxNumber = 0;

    // creates an array that will store the sorted numbers
    ArrayList<Integer> sortedIntArray = new ArrayList<Integer>();

    // loops through each number in the numbers arraylist
    for (int i = numbers.size(); i > 0; i--) {
        maxNumber = numbers.get(0);
        for(int j = 0; j < numbers.size(); j++) {
            if (numbers.get(j) > maxNumber) {
                // set the number to the new max number
                maxNumber = numbers.get(i);
            }
        }
        // add current max number to sorted array
        sortedIntArray.add(maxNumber);

        // remove the max number from numbers array
        numbers.remove(numbers.get(i));

        // add current max number to sorted array
        sortedIntArray.add(maxNumber);
    }
    return sortedIntArray;
}


Basically you're only adding numbers that are greater than the max number you've seen. You also need to account for numbers that are smaller, and store them appropriately.


You are only adding to sortedIntArray when the array element you're looking at is bigger than the biggest found so far. 48 gets added because you haven't found any so far. Then 99 gets added because it's larger than 48.


You need to pass through the array more than once. As it stands, what you're doing is finding every number larger than all the previous numbers in the array. If you're really just looking for a selection sort (not a great idea, it's pretty slow) you want:

int size = numbers.size();
for (int i = 0; i < size; i++)
{
    int maxNumber = 0;
    for (int j = 0; j < numbers.size(); j++)
    {
        if (numbers.get(j) > numbers.get(maxNumber))
            maxNumber = j;
    }
    SortedIntArray.add(numbers.get(maxNumber));
    numbers.remove(maxNumber);
}

This also returns your array from largest to smallest, which isn't necessarily what you want. Also, by removing from numbers, you're changing the array so it won't be available to you once you're done (unless you pass a clone). In general, you need to rethink your set-up.


you mixed several sorting strats

public ArrayList<Integer> listSort(ArrayList<Integer> numbers) {

    // create variable to store max number
    int maxNumber = 0;

    // creates an array that will store the sorted numbers
    ArrayList<Integer> sortedIntArray = new ArrayList<Integer>();

    // loops through each number in the numbers arraylist
    while (!numbers.isEmpty()) {
        int i;
        for(i=0;i<number.size();i++){//double loop to get the current max numbers
            if (numbers.get(i) > maxNumber) {
                // set the number to the new max number
                maxNumber = numbers.get(i);
            }
        }
        // add current max number to sorted array
        sortedIntArray.add(maxNumber);
        // remove the max number from numbers array
        number.listIterator(i).remove();
        maxNumber=0;//reset maxNumber
        }
    }



    return sortedIntArray;
}


I'm guessing you are asking this for a homework assignment. If you aren't just use any managed languages built in sort method as it will be faster than anything you write.


You are removing entries as you process them. This means the size is shrinking when you insert entries. After you add two entries, you have removed two entries leaving two entries so the loop stops.


try this algorithm it works with classes.. you can plug a list of class' into it as well

package drawFramePackage;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class QuicksortAlgorithm {
    ArrayList<AffineTransform> affs;
    ListIterator<AffineTransform> li;
    Integer count, count2;
    /**
     * @param args
     */
    public static void main(String[] args) {
        new QuicksortAlgorithm();
    }
    public QuicksortAlgorithm(){
        count = new Integer(0);
        count2 = new Integer(1);
        affs = new ArrayList<AffineTransform>();
        for (int i = 0; i <= 128; i++){
            affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
        }
        affs = arrangeNumbers(affs);
        printNumbers();
    }
    public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
        while (list.size() > 1 && count != list.size() - 1){
            if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
                list.add(count, list.get(count2));
                list.remove(count2 + 1);
            }
            if (count2 == list.size() - 1){
                count++;
                count2 = count + 1;
            }
            else{
            count2++;
            }
        }
        return list;
    }
    public void printNumbers(){
        li = affs.listIterator();
        while (li.hasNext()){
            System.out.println(li.next());
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜