码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法

时间:2016-07-24 01:50:10      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

稳定的排序
名称 时间复杂度 空间复杂度
冒泡排序 最差、平均为O(n^2),最好是O(n) 1
插入排序 最差、平均为O(n^2),最好是O(n) 1
归并排序 最差、平局和最好都是O(n*logn) O(n)
不稳定的排序
名称 时间复杂度 空间复杂度
选择排序 最差、平均都是O(n2) 1
希尔排序 O(n*logn) 1
堆排序 最差、平均和最好情况都是O(n*logn) 1
快速排序 平均是O(n*logn),最坏是O(logn) O(logn)

1.冒泡排序:

public class ArrayBub {
    private long[] a;
    private int nElems;
    public ArrayBub(int max){
        a=new long[max];
        nElems=0;
    }
    public void insert(long value){
        a[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(a[j]+" ");
        }
        System.out.println();
    }
    public void bubbleSort(){
        int out,in;
        for(out=nElems-1;out>1;out--){
            for(in=0;in<out;in++){
                if(a[in]>a[in+1]){
                    swap(in,in+1);
                }
            }
        }
    }
    private void swap(int one, int two) {
        long temp=a[one];
        a[one]=a[two];
        a[two]=temp;
    }
}
public class BubbleSortApp {
    public static void main(String[] args) {
        int maxSize=100;
        ArrayBub arr=new ArrayBub(maxSize);
        arr.insert(7);
        arr.insert(4);
        arr.insert(9);
        arr.insert(0);
        arr.insert(3);
        arr.insert(5);
        arr.insert(8);
        arr.insert(2);
        arr.display();
        arr.bubbleSort();
        arr.display();
        
        
        
        
    }
}

2.插入排序:

public class ArrayIns {
    private long[] a;
    private int nElems;
    public ArrayIns(int max){
        a=new long[max];
        nElems=0;
    }
    public void insert(long value){
        a[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(a[j]+" ");
        }
        System.out.println();
    }
    public void insertionSort(){
        int in,out;
        for(out=1;out<nElems;out++){
            long temp=a[out];
            in=out;
            while(in>0 && a[in-1]>=temp){
                a[in]=a[in-1];
                --in;
            }
            a[in]=temp;
        }
    }
}
public class InsertSortApp {
    public static void main(String[] args) {
        int maxSize=100;
        ArrayIns arr=new ArrayIns(maxSize);
        arr.insert(7);
        arr.insert(4);
        arr.insert(9);
        arr.insert(0);
        arr.insert(3);
        arr.insert(5);
        arr.insert(8);
        arr.insert(2);
        arr.display();
        arr.insertionSort();
        arr.display();

    }
}

3.归并排序:

public class DArray {
    private long[] theArray;
    private int nElems;
    public DArray(int max){
        theArray=new long[max];
        nElems=0;
    }
    public void insert(long value){
        theArray[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(theArray[j]+" ");
        }
        System.out.println();
    }
    public void mergeSort(){
        long[] workSpace=new long[nElems];
        recMergeSort(workSpace,0,nElems-1);
    }
    private void recMergeSort(long[] workSpace, int lowerBound, int upperBound) {
        if(lowerBound==upperBound){
            return;
        }else{
            int mid=(lowerBound+upperBound)/2;
            recMergeSort(workSpace,lowerBound,mid);
            recMergeSort(workSpace,mid+1,upperBound);
            merge(workSpace,lowerBound,mid+1,upperBound);
        }
    }
    private void merge(long[] workSpace, int lowPtr, int highPtr, int upperBound) {
        int j=0;
        int lowerBound=lowPtr;
        int mid=highPtr-1;
        int n=upperBound-lowerBound+1;
        while(lowPtr<=mid && highPtr<=upperBound){
            if(theArray[lowPtr]<theArray[highPtr]){
                workSpace[j++]=theArray[lowPtr++];
            }else{
                workSpace[j++]=theArray[highPtr++];
            }
        }
        while(lowPtr<=mid){
            workSpace[j++]=theArray[lowPtr++];
        }
        while(highPtr<=upperBound){
            workSpace[j++]=theArray[highPtr++];
        }
        for(j=0;j<n;j++){
            theArray[lowerBound+j]=workSpace[j];
        }
    }
    
    
}
public class MergeSortApp {
    public static void main(String[] args) {
        int maxSize=100;
        DArray arr=new DArray(maxSize);
        arr.insert(7);
        arr.insert(4);
        arr.insert(9);
        arr.insert(0);
        arr.insert(3);
        arr.insert(5);
        arr.insert(8);
        arr.insert(2);
        arr.display();
        arr.mergeSort();
        arr.display();
        
    }
}

4.选择排序:

public class ArraySel {
    private long[] a;
    private int nElems;
    public ArraySel(int max){
        a=new long[max];
        nElems=0;
    }
    public void insert(long value){
        a[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(a[j]+" ");
        }
        System.out.println();
    }
    public void selectionSort(){
        int out,in,min;
        for(out=0;out<nElems-1;out++){
            min=out;
            for(in=out+1;in<nElems;in++){
                if(a[in]<a[min]){
                    min=in;
                }
            }
            swap(out,min);
        }
    }
    private void swap(int one, int two) {
        long temp=a[one];
        a[one]=a[two];
        a[two]=temp;
    }
}
public class SelectSortApp {
    public static void main(String[] args) {
        int maxSize=100;
        ArraySel arr=new ArraySel(maxSize);
        arr.insert(7);
        arr.insert(4);
        arr.insert(9);
        arr.insert(0);
        arr.insert(3);
        arr.insert(5);
        arr.insert(8);
        arr.insert(2);
        arr.display();
        arr.selectionSort();
        arr.display();
        
    }
}

 5.希尔排序:

public class ArraySh {
    private long[] theArray;
    private int nElems;
    public ArraySh(int max){
        theArray=new long[max];
        nElems=0;
    }
    public void insert(long value){
        theArray[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(theArray[j]+" ");
        }
        System.out.println();
    }
    public void shellSort(){
        int inner,outer;
        long temp;
        int h=1;
        while(h<=nElems/3){
            h=h*3+1;
        }
        while(h>0){
            for(outer=h;outer<nElems;outer++){
                temp=theArray[outer];
                inner=outer;
                while(inner>h-1 && theArray[inner-h]>=temp){
                    theArray[inner]=theArray[inner-h];
                    inner-=h;
                }
                theArray[inner]=temp;
            }
            h=(h-1)/3;
        }
    }
}
public class ArraySh {
    private long[] theArray;
    private int nElems;
    public ArraySh(int max){
        theArray=new long[max];
        nElems=0;
    }
    public void insert(long value){
        theArray[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(theArray[j]+" ");
        }
        System.out.println();
    }
    public void shellSort(){
        int inner,outer;
        long temp;
        int h=1;
        while(h<=nElems/3){
            h=h*3+1;
        }
        while(h>0){
            for(outer=h;outer<nElems;outer++){
                temp=theArray[outer];
                inner=outer;
                while(inner>h-1 && theArray[inner-h]>=temp){
                    theArray[inner]=theArray[inner-h];
                    inner-=h;
                }
                theArray[inner]=temp;
            }
            h=(h-1)/3;
        }
    }
}

 6.堆排序:

class Node {
    private int iData;
    public Node(int key){
        iData=key;
    }
    public int getKey(){
        return iData;
    }
}

public class Heap{
    private Node[] heapArray;
    private int maxSize;
    private int currentSize;
    public Heap(int max){
        maxSize=max;
        currentSize=0;
        heapArray=new Node[maxSize];
    }
    public Node remove(){
        Node root=heapArray[0];
        heapArray[0]=heapArray[--currentSize];
        trickleDown(0);
        return root;
    }
    public  void trickleDown(int index) {
        int largerChild;
        Node top=heapArray[index];
        while(index<currentSize/2){
            int leftChild=2*index+1;
            int rightChild=leftChild+1;
            if(rightChild<currentSize && heapArray[leftChild].getKey()<heapArray[rightChild].getKey()){
                largerChild=rightChild;
            }else{
                largerChild=leftChild;
            }
            if(top.getKey()>=heapArray[largerChild].getKey()){
                break;
            }
            heapArray[index]=heapArray[largerChild];
            index=largerChild;
            
        }
        heapArray[index]=top;
    }
    public void displayHeap(){
        int nBlanks=32;
        int itemsPerRow=1;
        int column=0;
        int j=0;
        String dots=".............................";
        System.out.println(dots+dots);
        while(currentSize>0){
            if(column==0){
                for(int k=0;k<nBlanks;k++){
                    System.out.print(‘ ‘);
                }
            }
            System.out.print(heapArray[j].getKey());;
            if(++j==currentSize){
                break;
            }
            if(++column==itemsPerRow){
                nBlanks/=2;
                itemsPerRow*=2;
                column=0;
                System.out.println();
            }else{
                for(int k=0;k<nBlanks*2-2;k++){
                    System.out.print(‘ ‘);
                }
            }
        }
        System.out.println("\n"+dots+dots);
    }
    public void displayArray(){
        for(int j=0;j<maxSize;j++){
            System.out.print(heapArray[j].getKey()+" ");
        }
        System.out.println();
    }
    public void insertAt(int index,Node newNode){
        heapArray[index]=newNode;
    }
    public void incrementSize(){
        currentSize++;
    }
    
    
    
    
    
}
public class HeapSortApp {
    public static void main(String[] args) throws IOException {
        int size,j;
        System.out.println("Enter number of items: ");
        size=getInt();
        Heap theHeap=new Heap(size);
        for(j=0;j<size;j++){
            int random=(int) (java.lang.Math.random()*100);
            Node newNode=new Node(random);
            theHeap.insertAt(j, newNode);
            theHeap.incrementSize();
        }
        System.out.println("Random: ");
        theHeap.displayArray();
        for(j=size/2-1;j>=0;j--){
            theHeap.trickleDown(j);
        }
        System.out.println("Heap: ");
        theHeap.displayArray();
        theHeap.displayHeap();
        for(j=size-1;j>=0;j--){
            Node biggestNode=theHeap.remove();
            theHeap.insertAt(j, biggestNode);
        }
        System.out.println("Sorted: ");
        theHeap.displayArray();
    }
    public static String getString() throws IOException{
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        String s=br.readLine();
        return s;
    }
    public static int getInt() throws IOException{
        String s=getString();
        return Integer.parseInt(s);
    }
}

7.快速排序:

public class ArrayQui {
    private long[] theArray;
    private int nElems;
    public ArrayQui(int max){
        theArray=new long[max];
        nElems=0;
    }
    public void insert(long value){
        theArray[nElems]=value;
        nElems++;
    }
    public void display(){
        for(int j=0;j<nElems;j++){
            System.out.print(theArray[j]+" ");
        }
        System.out.println();
    }
    public void quickSort(){
        recQuickSort(0,nElems-1);
    }
    private void recQuickSort(int left, int right) {
        if(right-left<=0){
            return;
        }else{
            long pivot=theArray[right];
            int partition=partitionIt(left,right,pivot);
            recQuickSort(left,partition-1);
            recQuickSort(partition+1, right);
        }
    
    }
    private int partitionIt(int left, int right, long pivot) {
        int leftPtr=left-1;
        int rightPtr=right;
        while(true){
            while(theArray[++leftPtr]<pivot);
            while(rightPtr>0 && theArray[--rightPtr]>pivot);
            if(leftPtr>=rightPtr){
                break;
            }else{
                swap(leftPtr,rightPtr);
            }
        }
        swap(leftPtr,right);
        return leftPtr;
    }
    private void swap(int one, int two) {
        long temp=theArray[one];
        theArray[one]=theArray[two];
        theArray[two]=temp;
    }
    
    
}
public class QuickSortApp {
    public static void main(String[] args) {
        int maxSize=100;
        ArrayQui arr=new ArrayQui(maxSize);
        arr.insert(7);
        arr.insert(4);
        arr.insert(9);
        arr.insert(0);
        arr.insert(3);
        arr.insert(5);
        arr.insert(8);
        arr.insert(2);
        arr.display();
        arr.quickSort();
        arr.display();
        
    }
}

 

排序算法

标签:

原文地址:http://www.cnblogs.com/blog-yuesheng521/p/5691060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!