it_worker365

   ::  ::  ::  ::  :: 管理
/**
 * Created by itworker365 on 5/10/2017.
 */
public class SortTest {
    public static void main (String[] args) {
        //期待结果,1234579
        int[] a = {2,3,1,5,4,9,7};
        quickSort(a, 0, a.length - 1);
        printArray(a);
    }

    //冒泡排序,相邻元素依次比较,每次将最大元素沉淀到后排位置
    public static void bubbleSort (int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            //length - i - 1  每次沉淀后位置已经正确,无需多余比较,可以添加哨兵,一次没有改变就可以停止
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j + 1] < a[j]) {
                    int temp = a[j + 1];
                    a[j + 1] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    //插入排序,将取出元素与前面已经排序的元素对比,插入到正确的位置
    public static void insertSort (int[] a) {
        for (int i = 1; i < a.length - 1; i++) {
            if (a[i] < a[i - 1]) {
                int curr = a[i];
                int j ;
                a[i] = a[i - 1];
                for (j = i - 1; j >= 0 && curr < a[j]; j--) {
                    a[j + 1] = a[j];
                }
                a[j + 1] = curr;
            }
        }
    }

    //选择排序,从队列当前位置向后找到最小元素,替换到当前位置
    public static void selectSort (int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int minPos = i;
            int min = a[i];
            int curr = a[i];
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < min) {
                    min = a[j];
                    minPos = j;
                }
            }
            a[i] = min;
            a[minPos] = curr;
        }
    }

    //归并排序,将数组逐级拆分到两个子序列排序,归并
    public static void mergeSort (int[] a, int left, int right) {
        if (left < right) {
            int mid = (left + right) / 2;
            mergeSort(a, left, mid);
            mergeSort(a, mid + 1, right);
            merge(a, 0, mid, a.length - 1);
        }
    }

    private static void merge (int [] a, int start, int mid, int end) {
        int [] temparray = new int[end - start + 1];
        int tempIndex = 0;
        int leftstart = start;
        int rightstart = mid + 1;
        while (leftstart <= mid && rightstart <= end) {
            if (a[leftstart] <= a[rightstart]) {
                temparray[tempIndex++] = a[leftstart++];
            } else {
                temparray[tempIndex++] = a[rightstart++];
            }
        }
        while (leftstart <= mid) {
            temparray[tempIndex++] = a[leftstart++];
        }
        while (rightstart <= end) {
            temparray[tempIndex++] = a[rightstart++];
        }

        for (int i = 0; i < a.length; i++) {
            a[i] = temparray[i];
        }
    }

    //快速排序,先按照首元素为中间,从最右指针开始遍历,遇见小于的再从左往右,找到第一个大于,交换。最后分为左小右大
    public static void quickSort (int[] a, int left, int right) {
        if (left < right) {
            int mid = getPartitioned(a, left, right);
            quickSort(a, left, mid);
            quickSort(a, mid + 1, right);
        }
    }

    public static int getPartitioned(int[] a, int left, int right) {
        int temp = a[left];
        while (left < right) {
            while (left < right && a[right] >= temp) {
                right--;
            }
            a[left] = a[right];
            while (left < right && a[left] <= temp) {
                left++;
            }
            a[right] = a[left];
        }
        a[left] = temp;
        return left;
    }
    //从队列中某个位置向后找到最小元素
    public static void selectMin (int[] a, int pos) {
        int minPos = pos;
        int min = a[pos];
        for (int i = pos; i < a.length - 1; i++) {
            if (a[i] < min) {
                min = a[i];
                minPos = i;
            }
        }
        System.out.println("min value: " + min + "  min pos: " + minPos);
    }

    private static void printArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

 

posted on 2017-05-10 14:25  it_worker365  阅读(140)  评论(0编辑  收藏  举报