简体   繁体   中英

How do you sort out an array in java without using a sorting algorithm, while ignoring other values?

int timeTakenInSeconds[] = {600, -1, 500, 430, 412, -1, 0, 0, 0};

I want to sort this array from smallest to biggest. Simple, the problem is I need to ignore -1 and 0. My idea was to use a bubble sort to sort the array and place it in a new array. But I'm finding trouble sorting this array while ignoring 0 and -1 .

Still a bit new to arrays, so I was wondering if anyone has suggestions?

I suggest you consider my compact solution (Java 8).

Arrays.stream(timeTakenInSeconds)        // turn the array into the stream
      .filter(i -> i != 0 && i != -1)    // throw away 0 and -1
      .sorted()                          // make a sorted order by default
      .toArray();                        // put into an array

The second part is similar to the first one. Then to merge 2 arrays you may use, for example, ArrayUtils.addAll(T[], T...) from Apache.

EDIT Updated to sort positive integers in ascending order and integers less than or equal to 0 in descending order.
Using an Integer comparator:

    int timeTakenInSeconds[] = {600, -1, 500, 430, 412, -1, 0, 0, 0};
    Comparator<Integer> comparator = Integer::compare;

    //split the positive and negative integers since they'll be sorted differently.
    //Ascending order is the easiest.
    int[] positiveInts = IntStream.of(timeTakenInSeconds).filter(i -> i > 0).sorted().toArray();
    int[] negativeInts = IntStream.of(timeTakenInSeconds).filter(i -> i <= 0).toArray();

    //And now for discending order for negative integers.
    List<Integer> list = IntStream.of(negativeInts).boxed().collect(Collectors.toList());
    Integer[] sortedNegative = list.stream().sorted(comparator.reversed()).toArray(Integer[]::new);

    //Now overwrite timeTakenInSeconds with the correct values.
    int i = 0;
    for (int val : positiveInts) {
        timeTakenInSeconds[i] = val;
        i++;
    }
    for (int j = 0; j < sortedNegative.length; j++) {
        timeTakenInSeconds[i] = sortedNegative[j];
        i++;
    }

Printing this with:

for (int j : timeTakenInSeconds) {
        System.out.print(j + " ");
}

Sample run:

run:
412 430 500 600 0 0 0 -1 -1 
BUILD SUCCESSFUL (total time: 0 seconds)

Filter, then apply your sort yourself:

int[] newArray = Arrays.stream(timeTakenInSeconds).filter(i -> i > 0).toArray();
customBubbleSort(newArray);

Here is a solution that sorts the array in place and doesn't require anything sophisticated from recent versions of Java.

public static void weirdSort(int arr[]) {
    int zeros = 0;
    int minusOnes = 0;
    int index = 0;
    for (int a : arr) {
        switch (a) {
            case 0:
                zeros++;
                break;
            case -1:
                minusOnes++;
                break;
            default:
                arr[index++] = a;
        }
    }
    for (int i = 0; i < zeros; i++)
        arr[index++] = 0;
    for (int i = 0; i < minusOnes; i++)
        arr[index++] = -1;
    Arrays.sort(arr, 0, arr.length - zeros - minusOnes);    // Replace with bubble sort
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM