Sorts the given random access List using the Comparator . - Java java.util

Java examples for java.util:List Sort

Description

Sorts the given random access List using the Comparator .

Demo Code

/*//from w  ww  .jav  a2  s . c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.RandomAccess;

public class Main{
    public static void main(String[] argv){
        List list = java.util.Arrays.asList("asdf","java2s.com");
        introSort(list);
    }
    /**
     * Sorts the given random access {@link List} using the {@link Comparator}.
     * The list must implement {@link RandomAccess}. This method uses the intro sort
     * algorithm, but falls back to insertion sort for small lists.
     * @throws IllegalArgumentException if list is e.g. a linked list without random access.
     */
    public static <T> void introSort(List<T> list,
            Comparator<? super T> comp) {
        final int size = list.size();
        if (size <= 1)
            return;
        new ListIntroSorter<>(list, comp).sort(0, size);
    }
    /**
     * Sorts the given random access {@link List} in natural order.
     * The list must implement {@link RandomAccess}. This method uses the intro sort
     * algorithm, but falls back to insertion sort for small lists.
     * @throws IllegalArgumentException if list is e.g. a linked list without random access.
     */
    public static <T extends Comparable<? super T>> void introSort(
            List<T> list) {
        final int size = list.size();
        if (size <= 1)
            return;
        introSort(list, Comparator.naturalOrder());
    }
}

Related Tutorials