Java的排序算法

来源:互联网 发布:薛之谦唱功知乎 编辑:程序博客网 时间:2024/06/08 23:48
/** * Created by John on 2016/9/3. * 冒泡排序 */public class bubbleSort {    // 注意main函数中只能调用静态方法    // 思路很简单,小的数一点一点向前冒,最终有序    static int[] data = { 9, 2, 7, 19, 100, 97, 63, 208, 55, 78 };    public static void bubbleSort() {        int i, j, tmp;        for (i = 0; i < data.length - 1; i++) {            for (j = data.length - 1; j > i; j--) {                if (data[j] < data[j - 1]) { // 如果后一个数小于前一个数则交换位置                    tmp = data[j];                    data[j] = data[j - 1];                    data[j - 1] = tmp;                }            }        }    }    public static void main(String[] args) {        bubbleSort();        print();    }    static void print() { // 遍历输出排序后的数据        for (int i = 0; i < data.length; i++) {            System.out.print(data[i] + " ");        }    }

}

/** * Created by John on 2016/9/3. * 冒泡排序 */public class bubbleSort {    static int[] numbers = { 9, 2, 7, 19, 100, 97, 63, 208, 55, 78 };    public static void bubbleSort() {        int temp; // 记录临时中间值        int size = numbers.length; // 数组大小        for (int i = 0; i < size - 1; i++) {            for (int j = i + 1; j < size; j++) {                if (numbers[i] > numbers[j]) { // 交换两数的位置                    temp = numbers[i];                    numbers[i] = numbers[j];                    numbers[j] = temp;                }            }        }    }    public static void main(String[] args) {        bubbleSort();        print();    }    static void print() { // 遍历输出排序后的数据        for (int i = 0; i < numbers.length; i++) {            System.out.print(numbers[i] + " ");        }    }}

/** * Created by John on 2016/9/3. * 冒泡排序 */public class bubbleSort {    static int[] numbers = { 9, 2, 7, 19, 100, 97, 63, 208, 55, 78 };    public static void bubbleSort() {        int temp; // 记录临时中间值        int size = numbers.length; // 数组大小        for (int i = 0; i < size - 1; i++) {            for (int j = i + 1; j < size; j++) {                if (numbers[i] < numbers[j]) { // 交换两数的位置                    temp = numbers[i];                    numbers[i] = numbers[j];                    numbers[j] = temp;                }            }        }    }    public static void main(String[] args) {        bubbleSort();        print();    }    static void print() { // 逆序输出        for (int i = numbers.length - 1; i >= 0; i--) {            System.out.print(numbers[i] + " ");        }    }}

/** * Created by John on 2016/9/3. * 快速排序 */public class quickSort {    // 是一种比较快的排序,适合基本无序的数据    // 从数列中挑取一个元素做基准,重新排列,所有比基准小的放前面,比基准大的放后面(相同的数可以到任一边)    // 分成两拨后,继续递归的使用上述方法,最终有序    static class QuickSort {        public int data[];        private int partition(int array[], int low, int high) {            int key = array[low];            while (low < high) {                while (low < high && array[high] >= key)                    high--;                array[low] = array[high];                while (low < high && array[low] <= key)                    low++;                array[high] = array[low];            }            array[low] = key;            return low;        }        public int[] sort(int low, int high) {            if (low < high) {                int result = partition(data, low, high);                sort(low, result - 1);                sort(result + 1, high);            }            return data;        }    }    static void print(int data[]) {        for (int i = 0; i < data.length; i++) {            System.out.print(data[i] + " ");        }    }    public static void main(String[] args) {        int data[] = { 20, 3, 10, 9, 186, 99, 200, 96, 3000 };        QuickSort qs = new QuickSort();        qs.data = data;        qs.sort(0, data.length - 1);        print(data);    }}

/** * Created by John on 2016/9/3. * 快速排序 */public class kuaisu {        public static void quickSort(int[] numbers, int start, int end) {            if (start < end) {                int base = numbers[start]; // 选定的基准值(第一个数值作为基准值)                int temp; // 记录临时中间值                int i = start, j = end;                do {                    while ((numbers[i] < base) && (i < end))                        i++;                    while ((numbers[j] > base) && (j > start))                        j--;                    if (i <= j) {                        temp = numbers[i];                        numbers[i] = numbers[j];                        numbers[j] = temp;                        i++;                        j--;                    }                } while (i <= j);                if (start < j)                    quickSort(numbers, start, j);                if (end > i)                    quickSort(numbers, i, end);            }        }    static void print(int data[]) {        for (int i = 0; i < data.length; i++) {            System.out.print(data[i] + " ");        }    }    public static void main(String[] args) {        int data[] = { 20, 3, 10, 9, 186, 99, 200, 96, 3000 };        quickSort(data, 0, data.length - 1);        print(data);    }}
/** * Created by John on 2016/9/5. * 选择排序 */public class SelectSort {    // 选择排序是从待排序中选出最小的放在已经拍好的后面,这个算法比较耗时    // 通过循环,找出最小的的数的下标,赋值于k,即k永远保持排序数据中最小的数的下标,最后和当前位置i互换数据即可    static int data[] = { 9, 2, 7, 19, 100, 97, 63, 208, 55, 78 };    public static void selectSort() {        int i, j, k, tmp = 0;        for (i = 0; i < data.length - 1; i++) {            k = i;            for (j = i + 1; j < data.length; j++) {                if(data[j] < data[k])                    k = j;                if(k != i) {                    tmp = data[i];                    data[i] = data[k];                    data[k] = tmp;                }            }        }    }    public static void main(String[] args) {        selectSort();        print();    }    static void print() {        for (int i = 0; i < data.length; i++) {            System.out.print(data[i] + " ");        }    } }
/** * Created by John on 2016/9/5. * 选择排序 */public class SelectSort {    // 在未排序列种找到最小元素,存放到排序序列的起始位置    // 再从未剩余排序中继续寻找最小元素,然后放在排序序列末尾    // 以此类推,知道所有元素均排序完毕    static int numbers[] = { 9, 2, 7, 19, 100, 97, 63, 208, 55, 78 };    public static void selectSort() {        int size = numbers.length, temp;        for (int i = 0; i < size; i++) {            int k = i;            for (int j = size - 1; j > i; j--) {                if (numbers[j] < numbers[k]) k = j;            }            temp = numbers[i];            numbers[i] = numbers[k];            numbers[k] = temp;        }    }    public static void main(String[] args) {        selectSort();        print();    }    static void print() {        for (int i = 0; i < numbers.length; i++) {            System.out.print(numbers[i] + " ");        }    } }



0 0
原创粉丝点击