排序

1.选择排序
1)在线性表中找到最小元素,并和第一个元素交换。然后在剩下元素中找到最小元素,并和剩余的线性表中的第一个元素交换。直到线性表中仅剩一个元素为止。
2)第一次比较次数:n-1
第二次比较次数:n-2

T(n) = (n-1) + c + (n-1) + c + … + 2 + c + 1 + c = O(n^2)

 public static void selectSort(int[] list){
        for(int i = 0; i < list.length - 1; i++){
            int min = list[i];
            int minIndex = i;
            for(int j = i + 1; j < list.length; j++){
                if(min > list[j]){
                    min = list[j];
                    minIndex = j;
                }
            }
            if(minIndex != i){
                list[minIndex] = list[i];
                list[i] = min;
            }
        }
    }
    public static void main(String[] args){
        int[] list = {2, 9, 5, 4, 8, 1, 6};
        selectSort(list);
        for(int j = 0; j < list.length; j++){
            System.out.print(list[j] + " ");
        }
    }

2.插入排序
时间复杂度:
T(n) = (2+c) +(2 * 2 + c) + (2 * 3 + c) + … + (2 * (n-1) + c)
=2(1+2+…+n-1) + c(n-1) = O(n^2)
选择排序和插入排序有相同的时间复杂度

public class InsertSort {
    public static void insertSort(int[] list){
        for(int i = 1; i < list.length; i++){
            int currentElement = list[i];
            int k;
            for(k = i - 1; k >= 0 && list[k] > currentElement; k--){
                list[k + 1] = list[k];  //前大:后移
            }
            list[k + 1] = currentElement;  //插入到正确的位置
        }
    }
    public static void main(String[] args){
        int[] list = {1, 9, 4, 6, 5, -4};
        insertSort(list);
        for(int i = 0; i < list.length; i++){
            System.out.print(list[i] + " ");
        }
    }
}

3.冒泡排序
稳定的排序。
最好:因为第一次遍历的比较次数为n-1,所以最佳情况下,时间为O(n)。
最坏:最差情况下需要进行n-1次遍历。第一次遍历需要n - 1次比较;第二次遍历需要n-2次比较;依次进行,最后一次遍历需要1次比较。因此,比较总次数为:
(n-1)+(n-2)+…+2+1 = (n-1)n/2 = n*n/2 - n/2 = O(n^2)

public static void bubbleSort(int[] list){
        boolean needNextPass = true;
        for(int k = 1; k < list.length && needNextPass; k++){ //k:遍历次数
            needNextPass = false;
            for(int i = 0; i < list.length - k; i++){ //在第k次遍历时,不需要考虑最后k-i个元素了。因为它们已经有序
                if(list[i] > list[i + 1]){
                    int temp = list[i + 1];
                    list[i + 1] = list[i];
                    list[i] = temp;
                    needNextPass = true;
                }
            }
        }
    }

4.归并排序

package javabase.Sort;

/**
 * 归并排序
 * Created by Administrator on 2017/3/21.
 */
public class MergeSort {
    public static void mergeSort(int[] list){
        if(list.length > 1){
            int[] firstHalf = new int[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf);

            int secondHalfLength = list.length - list.length / 2;
            int[] secondHalf = new int[secondHalfLength];
            System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
            mergeSort(secondHalf);

            merge(firstHalf, secondHalf, list);
        }
    }

    public static void merge(int[] list1, int[] list2, int[] temp){
        int current1 = 0;
        int current2 = 0;
        int current3 = 0;

        while(current1 < list1.length && current2 < list2.length){
            if(list1[current1] < list2[current2]){
                temp[current3++] = list1[current1++];
            }else{
                temp[current3++] = list2[current2++];
            }
            while(current1 < list1.length){
                temp[current3++] = list1[current1++];
            }
            while(current2 < list2.length){
                temp[current3++] = list2[current2++];
            }
        }
    }
    public static void main(String[] args){
        int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
        mergeSort(list);
        for(int i = 0; i < list.length; i++){
            System.out.print(list[i] + " ");
        }
    }
}

T(n) = T(n/2) + T(n/2) + 2n - 1 = O(nlogn)
归并排序时间复杂度:O(nlogn),优于选择、插入、冒泡(它们都是O(n^2))
java.util.Arrays类中的sort()使用的就是归并排序算法的变体实现的。

5.快速排序

public class QuickSort {
    public static void quickSort(int[] list){
        quickSort(list, 0, list.length - 1);
    }

    private static void quickSort(int[] list, int first, int last){
        if(last > first){
            int pivotIndex = partition(list, first, last);
            quickSort(list, first, pivotIndex - 1);
            quickSort(list, pivotIndex + 1, last);
        }
    }

    private static int partition(int[] list, int first, int last){
        int pivot = list[first];
        int low = first + 1;  // 从前找
        int high = last;      // 从后找

        while(high > low){
            while(low <= high && list[low] <= pivot)  // 从左搜.找第一个大于主元对元素
                low++;
            while(low <= high && list[high] > pivot)  //从右搜.找第一个小于等于主元对元素
                high--;
            if(high > low){
                int temp = list[high];
                list[high] = list[low];
                list[low] = temp;
            }
        }
        while(high > first && list[high] >= pivot)   //high和low指向同一个时。即high=low。条件1防止high出界
            high--;

        //所有元素都检查过了
        // swop pivot with list[high]。返回主元下标
        if(pivot > list[high]){
            list[first] = list[high];
            list[high] = pivot;
            return high;
        }else{
            return first;
        }
    }

    public static void main(String[] args){
        int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
        quickSort(list);
        for(int i = 0; i < list.length; i++){
            System.out.print(list[i] + " ");
        }
    }
}

6.堆排序

package sort;
import java.util.ArrayList;

/**
 * Created on 2017/7/1.
 */
public class Heap<E extends Comparable> {
    private ArrayList<E> list = new java.util.ArrayList<E>();

    public Heap(){
    }
    public Heap(E[] objects){
        for(int i = 0; i < objects.length; i++){
            add(objects[i]);
        }
    }

    public void add(E newObject){
        list.add(newObject);
        int currentIndex = list.size() - 1; // the index of the last node

        while(currentIndex > 0){
            int parentIndex = (currentIndex - 1) / 2;
            //swap if the current object is greater then its parent
            if(list.get(currentIndex).compareTo(list.get(parentIndex)) > 0){
                E temp = list.get(currentIndex);
                list.set(currentIndex, list.get(parentIndex));
                list.set(parentIndex, temp);
            }else
                break;   //the tree is a heap now

            currentIndex = parentIndex;
        }
    }

    //remove the root from the heap
    public E remove(){
        if(list.size() == 0){
            return null;
        }

        E removeObject = list.get(0);
        list.set(0, list.get(list.size() - 1));
        list.remove(list.size() - 1);

        int currentIndex = 0;
        while(currentIndex < list.size()){
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;

            // find the maximum between two children
            if(leftChildIndex >= list.size()) break; // the tree is a heap
            int maxIndex = leftChildIndex;
            if(rightChildIndex < list.size()){
                if(list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0){
                    maxIndex = rightChildIndex;
                }
            }// 比较左右子节点大小,找到更大大

            //swap if the current node is less than the maximum
            if(list.get(currentIndex).compareTo(list.get(maxIndex)) < 0){
                E temp = list.get(maxIndex);
                list.set(maxIndex, list.get(currentIndex));
                list.set(currentIndex, temp);
                currentIndex = maxIndex;
            }else
                break;
        }
        return removeObject;
    }

    public int getSize(){
        return list.size();
    }
}
package sort;

/**
 * Created by on 2017/7/1.
 */
public class HeapSort {
    public static <E extends Comparable> void heapSort(E[] list){
        Heap<E> heap = new Heap<E>();

        //add elements to the heap
        for(int i = 0; i < list.length; i++){
            heap.add(list[i]);
        }

        //remove elements from the heap
        //以降序删除这些元素
        for(int i = list.length - 1; i >= 0; i--){
            list[i] = heap.remove();
        }
    }

    public static void main(String[] args){
        Integer[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
        heapSort(list);
        for(int i = 0; i < list.length; i++){
            System.out.print(list[i] + " ");
        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值