Java实现各种简单排序

Java实现各种简单排序

冒泡排序

package cn.lg.sort;

/**
 * 冒泡排序
 * Created by L on 2017/3/19.
 */
public class BubbleSortDemo {
    public static void main(String[] args) {
        int[] arr = {12, 43, 21, 353, 21, 1, 35, 6, 3, 23, 7, 98, 43, 32, 4};
        System.out.println("排序前长度:" + arr.length);
        bubbleSort(arr);
        for (int i :
                arr) {
            System.out.print(i + " ");
        }
        System.out.println("\n排序后长度:" + arr.length);
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flag = false;
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j] < arr[j - 1]) {
                    swap(arr, j, j - 1);
                    flag = true;
                }
            }
            if (flag == false) return;
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

}

选择排序

package cn.lg.sort;

/**
 * 选择排序
 * Created by L on 2017/3/19.
 */
public class SelectSortDemo {
    public static void main(String[] args) {
        int[] arr = {564, 432, 123, 765, 78, 23, 24, 98, 32, 87, 2, 45, 21, 890, 32, 4, 56, 2, 743, 32};
        System.out.println("排序前长度:" + arr.length);
        selectSort(arr);
        for (int i :
                arr) {
            System.out.print(i + " ");
        }
        System.out.println("\n排序后长度:" + arr.length);
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[min] > arr[j]) min = j;
            }
            swap(arr, i, min);
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}

插入排序

package cn.lg.sort;

/**
 * 插入排序
 * Created by L on 2017/3/19.
 */
public class InsertSortDemo {
    public static void main(String[] args) {
        int[] arr = {12, 43, 21, 353, 21, 1, 35, 6, 3, 23, 7, 98, 1, 43, 32, 4};
        System.out.println("排序前长度:" + arr.length);
        insertSort(arr);
        for (int i :
                arr) {
            System.out.print(i + " ");
        }
        System.out.println("\n排序后长度:" + arr.length);
    }

    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0; j--) {
                if (arr[j] < arr[j - 1]) {
                    swap(arr, j, j - 1);
                }
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

}

快速排序

package cn.lg.sort;

/**
 * 快速排序
 * Created by L on 2017/3/19.
 */
public class QuickSortDemo {
    public static void main(String[] args) {
        int[] arr = {54, 34, 365, 3, 1, 56, 8, 5, 4356, 43242,4356,467, 98, 45, 97, 67, 43, 342, 0, 345,1,0};
        System.out.println("排序前长度:" + arr.length);

        quickSort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.print(i + " ");
        }

        System.out.println("\n排序后长度:" + arr.length);
    }

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int mid = partition(arr, low, high);
            quickSort(arr, low, mid - 1);
            quickSort(arr, mid + 1, high);
        }
    }

    public static int partition(int[] arr, int low, int high) {
        int key = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= key) --high;
            arr[low] = arr[high];
            while (low < high && arr[low] <= key) ++low;
            arr[high] = arr[low];
        }
        arr[low] = key;
        return low;
    }


}

堆排序

package cn.lg.sort;

import java.util.Arrays;

/**
 * 大根堆
 * Created by L on 2017/3/19.
 */
public class HeapSortDemo {

    public static void main(String[] args) {
        int[] a = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50};
        heapSort(a);
        System.out.println(Arrays.toString(a));
    }

    public static void heapSort(int[] a) {
        int i;
        for (i = a.length / 2 - 1; i >= 0; i--) {// 构建一个大顶堆
            adjustHeap(a, i, a.length - 1);
        }
        for (i = a.length - 1; i >= 0; i--) {// 将堆顶记录和当前未经排序子序列的最后一个记录交换
            int temp = a[0];//把最后一个元素替换头结点
            a[0] = a[i];
            a[i] = temp;
            adjustHeap(a, 0, i - 1);// 将a中前i-1个记录重新调整为大顶堆
        }
    }

    public static void adjustHeap(int[] a, int i, int len) {
        int temp, j;
        temp = a[i];
        for (j = 2 * i; j < len; j *= 2) {// 沿关键字较大的孩子结点向下筛选
            if (j < len && a[j] < a[j + 1])
                ++j; // j为关键字中较大记录的下标
            if (temp >= a[j])
                break;
            a[i] = a[j];
            i = j;
        }
        a[i] = temp;
    }
}

希尔排序

package cn.lg.sort;

import java.util.Arrays;

/**
 * 希尔排序
 * Created by L on 2017/3/19.
 */
public class ShellSortDemo {
    public static void main(String[] args) {
        int[] data = new int[]{26, 53, 67, 48, 57, 13, 48, 32, 60, 50};
        shellSortSmallToBig(data);
        System.out.println(Arrays.toString(data));
    }

    public static void shellSortSmallToBig(int[] data) {
        int j = 0;
        int temp = 0;
        for (int increment = data.length / 2; increment > 0; increment /= 2) {
            System.out.println("increment:" + increment);
            for (int i = increment; i < data.length; i++) {
                // System.out.println("i:" + i);
                temp = data[i];
                for (j = i - increment; j >= 0; j -= increment) {
                    // System.out.println("j:" + j);
                    // System.out.println("temp:" + temp);
                    // System.out.println("data[" + j + "]:" + data[j]);
                    if (temp < data[j]) {
                        data[j + increment] = data[j];
                    } else {
                        break;
                    }
                }
                data[j + increment] = temp;
            }
            for (int i = 0; i < data.length; i++)
                System.out.print(data[i] + " ");
        }
    }
}

归并排序

package cn.lg.sort;

import java.util.Arrays;

/**
 * 归并排序
 * Created by L on 2017/3/19.
 */
public class MergeSortDemo {

    public static void main(String[] args) {
        int a[] = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50};
        mergeSort(a, 0, a.length - 1);
        System.out.println("排序结果:" + Arrays.toString(a));
    }

    public static void mergeSort(int[] a, int low, int high) {
        int mid = (low + high) / 2;
        if (low < high) {
            // 左边
            mergeSort(a, low, mid);
            // 右边
            mergeSort(a, mid + 1, high);
            // 左右归并
            merge(a, low, mid, high);
            System.out.println(Arrays.toString(a));
        }

    }

    public static void merge(int[] a, int low, int mid, int high) {
        int[] temp = new int[high - low + 1];
        int i = low;// 左指针
        int j = mid + 1;// 右指针
        int k = 0;
        // 把较小的数先移到新数组中
        while (i <= mid && j <= high) {
            if (a[i] < a[j]) {
                temp[k++] = a[i++];
            } else {
                temp[k++] = a[j++];
            }
        }
        // 把左边剩余的数移入数组
        while (i <= mid) {
            temp[k++] = a[i++];
        }
        // 把右边边剩余的数移入数组
        while (j <= high) {
            temp[k++] = a[j++];
        }
        // 把新数组中的数覆盖nums数组
        for (int k2 = 0; k2 < temp.length; k2++) {
            a[k2 + low] = temp[k2];
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值