经典排序算法

来源:互联网 发布:sql中查询的功能节点 编辑:程序博客网 时间:2024/06/04 01:38

1、简单选择排序

public static void selectSort(int[] a) {for(int i = 0; i < a.length; i++) {int min = i;for(int j = i + 1; j < a.length; j++) {if(a[min] > a[j]) {min = j;}}if(min != i) {int temp = a[min];a[min] = a[i];a[i] = temp;}}}

2、快速排序

    public static void quickSort(int[] array, int low, int high) {        int i = low;        int j = high;        if (low >= high) {            return;        }        int key = array[low];        while (low < high) {            while (key <= array[high] && low < high) {                high--;            }            array[low] = array[high];            while (key >= array[low] && low < high) {                low++;            }            array[high] = array[low];        }        array[low] = key;        quickSort(array, i, low - 1);        quickSort(array, low + 1, j);    }

3、插入排序

public class Solution{public static void main(String[] args) {int[] a = {1, 3, 2, 7, 4, 2, 4, 5};printArray(a);insertSort(a);printArray(a);}public static void insertSort(int[] a) {int i, j, temp;for(i = 1; i < a.length; i++){temp = a[i];for(j = i - 1; j >= 0 && a[j] > temp; j--) {a[j + 1]  = a[j];}a[j + 1] = temp;}}public static void printArray(int[] a) {StringBuilder builder = new StringBuilder();for(int i = 0; i < a.length; i++) {builder.append(a[i] + " ");}System.out.println(builder.toString());}}

4、折半插入排序

public static void binaryInsertSort(int[] a) {int i , j, low, high;for(i = 1; i < a.length; i++) {low = 0;high = i -1;while(low <= high) {int mid = (low + high) / 2;if (a[mid] < a[i]) {low = mid + 1;} else {high = mid - 1;}}int temp = a[i];for (j = i -1; j >= high + 1; j--) {a[j + 1] = a[j];}a[high + 1] = temp;}}

5、希尔排序

public static void shellSort(int[] a) {int i,j;for(int dk = a.length/2; dk >= 1; dk = dk/2) {for(i = dk; i < a.length; i+= dk) {int temp = a[i];for(j = i - dk; j >0 && a[j] > a[i]; j -= dk) {a[j + dk] = a[j];}a[j + dk] = temp;}}}

6、将所有的负数移到正数前面

public static void sort(int[] a) {int i = 0;int j = a.length - 1;while (i < j) {while(i < j && a[i] <= 0) {i++;}while(i < j && a[j] >= 0) {j--;}int temp = a[j];a[j] = a[i];a[i] = temp; }}

7、冒泡排序

public static void bubbleSort(int[] a) {int i,j;int n = a.length;bool flag = false;for (i = 0; i < n - 1; i++) {for(j = n -1; j > i; j--) {if(a[j] < a[j - 1]) {int temp = a[j];a[j] = a[j - 1];a[j - 1] = temp;flag = true;}}if(!flag) {return ;}}}

8、单链表倒置

    public static ListNode reverseList(ListNode node) {        if (node == null || node.next == null) {            return node;        }        ListNode head = node;        ListNode now = node.next;        ListNode pre = node;        while (now != null) {            ListNode next = now.next;            pre.next = next;            now.next = head;            head = now;            now = next;        }        return head;    }    private static class ListNode {        public ListNode(int data) {            this.data = data;        }        int data;        ListNode next;    }

9、二路归并

    public static void mergeSort(int[] a, int low, int high) {        if(low < high) {            int mid = (low + high) / 2;            mergeSort(a, low, mid);            mergeSort(a, mid + 1, high);            merge(a, low, mid, high);        }    }    private static void merge(int[] a, int low, int mid, int high) {        int[] b = new int[high + 1];        for(int i = low; i <= high; i++) {            b[i] = a[i];        }        int i, j, k;        for(i = low, j = mid + 1, k = low; i <= mid && j <= high; k++) {            if(b[i] <= b[j]) {                a[k] = b[i++];            } else {                a[k] = b[j++];            }        }        while (i <= mid) {            a[k++] = b[i++];        }        while (j <= high) {            a[k++] = b[j++];        }    }

10、单链表归并排序

    public static LNode mergeSort(LNode head) {        if (head == null || head.next == null) {            return head;        }        LNode midNode = getMidNode(head);        LNode half = midNode.next;        midNode.next = null;        return merge(mergeSort(head), mergeSort(half));    }    private static LNode getMidNode(LNode head){        LNode slow = head, fast = head;        while (fast.next != null && fast.next.next != null) {            slow = slow.next;            fast = fast.next;        }        return slow;    }    private static LNode merge(LNode head, LNode half) {        LNode result = new LNode(0);        LNode curNode = new LNode(0);        result.next = curNode;        while (head != null && half != null) {            if (head.data <= half.data) {                curNode.next = head;                head = head.next;            } else {                curNode.next = half;                half = half.next;            }            curNode = curNode.next;        }        if (head == null) {            curNode.next = half;        } else {            curNode.next = head;        }        return result.next.next;    }    public static class LNode{        public LNode(int data) {            this.data = data;        }        int data;        LNode next;    }
11、堆排序(大根堆)
public class Main {    public static void main(String [] args) {        int[] a = {1,3,4,6,8,0,2,5};        heapSort(a);    }    public static void heapSort(int[] a) {        buildMaxHeap(a);        int length = a.length;        for(int i = length - 1; i >= 0; i--) {            System.out.print(a[0]);            a[0] = a[i];            adjustDown(a, 0, i);        }    }    private static void buildMaxHeap(int[] a) {        int length = a.length;        for(int i = (length - 1)/2; i >= 0; i--) {            adjustDown(a, i, length);        }    }    private static void adjustDown(int[] a, int k, int length) {        int key = a[k];        for(int i = 2*k + 1; i <= length - 1; i = 2*i + 1) {            if(i < length - 1 && a[i] < a[i + 1]) {                i++;            }            if(a[k] >= a[i]) {                break;            } else {                a[k] = a[i];                a[i] = key;                k = i;            }        }        a[k] = key;    }}

原创粉丝点击