Java 基于什么排序算法被认为是有效的?

Java 基于什么排序算法被认为是有效的?,java,algorithm,sorting,Java,Algorithm,Sorting,我已经实现了一个排序算法来对未排序的整数数组进行排序。如果我加入一个100000个元素的数组,返回大约需要10500密耳秒。我的算法是快还是慢?。下面是代码 public static void sort(int[] array) { int lastItem = array[array.length - 1]; int length = array.length; for (int j = 0; j < array.length; j++) {

我已经实现了一个排序算法来对未排序的整数数组进行排序。如果我加入一个100000个元素的数组,返回大约需要10500密耳秒。我的算法是快还是慢?。下面是代码

public static void sort(int[] array) {
    int lastItem = array[array.length - 1];
    int length = array.length;
    for (int j = 0; j < array.length; j++) {

        for (int i = 0; i < length; i++) {

            if (array[i] > lastItem) {
                array[length-1] = array[i];
                array[i] = lastItem;
                lastItem = array[length - 1];
            }
        }
        length--;
        if (length > 1) lastItem = array[length - 1];
    }
}

您的算法正在使用冒泡排序,冒泡排序采用^2。对于较大的输入,它可能会很慢。为什么不使用快速排序,它将在Onlogn中实现您想要的结果

下面是一些代码,请注意,最好选择pivot作为mid元素

/**
 * o(nlogn) - high probability otherwise o(n SQUARE)
 * 
 * 
 * Choose a pivot value. We take the value of the middle element 
 * as pivot value, but it can be any value, which is in range of 
 * sorted values, even if it doesn't present in the array.
 * 
 * Partition. Rearrange elements in such a way, that all elements 
 * which are lesser than the pivot go to the left part of the array 
 * and all elements greater than the pivot, go to the right part 
 * of the array. Values equal to the pivot can stay in any part 
 * of the array. Notice, that array may be divided in non-equal parts.
 * 
 * Sort both parts. Apply quicksort algorithm recursively to the left 
 * and the right parts.
 * 
 * @param input
 */
public void quickSort(int[] input, int start, int end){
    if( start < end ){
        int pindex = findParition(input, start, end);
        quickSort(input, start, pindex-1);
        quickSort(input, pindex+1, end);
    }
}

/**
 * findParition for quick sort
 * @param input
 * @param start
 * @param end
 * @return
 */
private int findParition(int[] input, int start, int end) {
    int pivot = input[end];
    int pindex = start;

    for( int i = start; i < end; i++){
        if( input[i] <= pivot ){
            int temp = input[pindex];
            input[pindex] = input[i];
            input[i] = temp;

            pindex++;
        }
    }

    int temp = input[pindex];
    input[pindex] = input[end];
    input[end] = temp;

    return pindex;
}
比较各种各样的东西


如果您谈论的是实现性能:请将其与同一阵列的ArraySort进行比较。看看哪一个更快。如果你在谈论算法复杂性:你的算法似乎有二次运行时。阵列大10倍,速度慢100倍。不太好。如果您使用的是java8,请阅读此链接有许多很好的示例,您应该计算算法的Big-O时间,然后将其与其他已知算法进行比较。那是最好的办法。一个数据点告诉您的信息很少。仍然在^2上,这不是很好。非常感谢。因为我不熟悉编程和其他东西。所以我知道的还不多。但从你们的回答来看。我想我现在该去哪里了。