(Java)冒泡,选择,插入,希尔,归并排序与堆排序

(Java)冒泡,选择,插入,希尔,归并排序与堆排序,第1张

(Java)冒泡,选择,插入,希尔归并排序与堆排序

Java源代码:

冒泡排序

public class _01_bubblesort {

    void BubbleSort(int A[],int n){

        for (int pass = n-1; pass >= 0; pass--){

            for(int i = 0;i< pass -1; i++){

                if(A[i]>A[i+1]){

                    //交换元素
                    int temp = A[i];
                    A[i] = A[i+1];
                    A[i+1] = temp;

                }
            }
        }
    }



    void BubbleSortImproved(int A[], int n){

        int pass, i, temp, swapped = 1;

        for(pass = n - 1; pass >=0&&swapped==1;pass--){

            swapped=0;

            for(i = 0; i < pass -1;i++){

                if(A[i]>A[i+1]){

                    //交换元素
                    temp = A[i];
                    A[i] = A[i+1];
                    A[i+1] = temp;
                    swapped = 1;

                }
            }
        }
    }
}

选择排序

public class _02_selection {

    void Selection(int A[], int n){
        int i,j,min,temp;
        for(i = 0;i 

插入排序

public class _03_insertionsort {
    void InsertionSort(int A[], int n){
        int i,j,v;
        for(i = 2; i <= n-1; i++){
            v = A[i];
            j = i;
            while(A[j-1] > v && j >= 1){
                A[j] = A[j-1];
                j--;
            }
            A[j] = v;
        }
    }
}

希尔排序

public class _04_shellsort {
    void ShellSort(int A[], int array_size){
        int i,j,h,v;
        for(h = array_size/2;h >= 1; h/=2){
            {
                for (i = h; i < array_size;i++){
                    v = A[i];
                    j = i;
                    while(j>h && A[j-h]>v){
                        A[j] = A[j-h];
                        j -=h;
                    }
                    A[j] = v;
                }
            }

        }
    }
}

归并排序

public class _05_mergesort {

    void Merge(int A[], int temp[], int left, int mid, int right){
        int i, left_end, size, temp_pos;
        left_end = mid - 1;
        temp_pos = left;
        size = right - left + 1;
        while((left <= left_end)&&(mid<=right)){
            if(A[left] <= A[mid]){
                temp[temp_pos] = A[left];
                temp_pos = temp_pos + 1;
                left = left + 1;
            }
            else{
                temp[temp_pos] = A[mid];
                temp_pos = temp_pos + 1;
                mid = mid + 1;
            }
        }
        while(left <= left_end){
            temp[temp_pos] = A[left];
            left = left + 1;
            temp_pos = temp_pos + 1;
        }
        while(mid <= right){
            temp[temp_pos] = A[mid];
            mid = mid + 1;
            temp_pos = temp_pos + 1;
        }
        for(i = 0; i <= size; i++){
            A[right] = temp[right];
            right = right - 1;
        }
    }

    void Mergesort(int A[], int temp[], int left, int right){
        int mid;
        if(right > left){
            mid = (right + left)/2;
            Mergesort(A,temp,left,mid);
            Mergesort(A,temp,mid+1,right);
            Merge(A,temp,left,mid+1,right);

        }
    }
}

堆排序

public class _06_heapsort {
    public class Heap {
        public int[] array;
        public int count;
        public int capacity;
        public int heap_type;

        //        public Heap(int capacity, int heap_type)
//        public Parent(int capacity, int heap_type)
//        public int LeftChild(int i)
//        public int RightChild(int i)
//        public int GetMaximum(int i)
        //堆的创建
        public Heap(int capacity, int heap_type) {
            this.capacity = capacity;
            this.heap_type = heap_type;
            this.count = 0;
            this.array = new int[capacity];
        }

        public int Parent(int i) {
            if (i <= 0 || i >= this.count) {
                return -1;
            }
            return (i - 1) / 2;
        }

        public int LeftChild(int i) {
            int left = 2 * i + 1;
            if (left >= this.count) {
                return -1;
            }
            return left;
        }

        public int RightChild(int i) {
            int right = 2 * i + 2;
            if (right >= this.count) {
                return -1;
            }
            return right;
        }

        public int GetMaximum() {
            if (this.count == 0) {
                return -1;
            }
            return this.array[0];
        }

        public void PercolateDown(int i) {
            int l, r, max, temp;
            l = LeftChild(i);
            r = RightChild(i);
            if (l != -1 && this.array[l] > this.array[i]) {
                max = l;
            } else {
                max = i;
            }
            if (r != -1 && this.array[r] > this.array[max]) {
                max = r;
            }
            if (max != i) {
                temp = this.array[i];
                this.array[i] = this.array[max];
                this.array[max] = temp;
            }
            PercolateDown(max);
        }

        int DeleteMax() {
            if (this.count == 0) {
                return -1;
            }
            int data = this.array[0];
            this.array[0] = this.array[this.count - 1];
            this.count--;
            PercolateDown(0);
            return data;
        }

        void ResizeHeap() {
            int[] array_old = new int[this.capacity];
            System.arraycopy(this.array, 0, array_old, 0, this.count - 1);
            this.array = new int[this.capacity * 2];
            if (this.array == null) {
                System.out.println("Memory Error");
                return;
            }

            for (int i = 0; i < this.capacity; i++) {
                this.array[i] = array_old[i];
            }
            this.capacity *= 2;
            array_old = null;


        }

        void Insert(int data){
            int i;
            if(this.count == this.capacity){
                ResizeHeap();
            }
            this.count++;
            i = this.count-1;
            while (i>=0&&data > this.array[(i-1)/2]){
                this.array[i] = this.array[(i-1)/2];
                i = (i-1)/2;
            }
            this.array[i] = data;
        }

        void DestoryHeap(){
            this.count = 0;
            this.array = null;
        }


        void BuildHeap(Heap h,int A[],int n){
            if(h==null){
                return;
            }
            while(h.capacity>this.capacity){
                h.ResizeHeap();
            }
            for (int i = 0; i =0;i--){
                h.PercolateDown(i);
            }
        }

        void Heapsort(int A[],int n){
            Heap h = new Heap(n,0);
            int old_size, i, temp;
            BuildHeap(h,A,n);
            old_size = h.count;
            for (i = n -1 ;i>0;i--){
                temp = h.array[0];h.array[0]=h.array[h.count-1];
                h.count--;
                h.PercolateDown(i);
            }
            h.count = old_size;
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5672133.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存