排序

	//冒泡

	for(int i = array.length -1; i > 0; i--){

			for(int j = 0; j < i; j++){

				if(array[j]>array[j+1]){

					int tem = 0;

					tem = array[j];

					array[j] = array[j+1];

					array[j+1] = tem;

				}

			}

		}

	//快速排序

	public static void quickSortByThree(int[] a, int low, int high) {

        if (low >= high)

            return;

        int mid = low + (high - low) / 2;

        if (a[mid] < a[low])

            Sort.swap(a, mid, low);//交换mid和low

        if (a[low] > a[high])

            Sort.swap(a, low, high);

        if (a[mid] > a[high])

            Sort.swap(a, mid, high);

        Sort.swap(a, mid, high - 1);

        int pivot = a[high - 1];

        int i, j;

        for (i = low, j = high - 1;;) {

            while (i < high - 1 && a[++i] < pivot)

                ;

            while (j > low && a[--j] > pivot)

                ;

            if (i >= j)

                break;

            Sort.swap(a, i, j);

        }

        Sort.swap(a, i, high - 1);

        quickSortByThree(a, low, i - 1);

        quickSortByThree(a, i + 1, high);

    }

    //插入排序

    public static void insertSort(int[] array) {

        int len = array.length;

        for (int i = 1; i < len; i++) {

            for (int j = i; j > 0; j--) {

                if (array[j] < array[j - 1]) {

                    Sort.swap(array, j, j - 1);//交换j和j-1

                } else

                    break;

            }

        }

    }

    //选择排序

    public static void selectSort(int[] array) {

        for (int i = 0; i < array.length - 1; i++) {

            int min = i;

            for (int j = i + 1; j < array.length; j++) {

                if (array[j] < array[min]) {

                    min = j;

                }

            }

            Sort.swap(array, i, min);//交换i和min

        }

    }

    //归并排序

    public static void mergeSort(int[] a, int[] tmp, int left, int right) {

        if (left < right) {

            int mid = left + (right - left) / 2;

            mergeSort(a, tmp, left, mid);// 左排序

            mergeSort(a, tmp, mid + 1, right);// 右排序

            merge(a, tmp, left, mid + 1, right);// 左右合并

        }

    }

public static void merge(int[] a, int[] tmp, int left, int rightPos,

            int right) {

        int leftEnd = rightPos - 1;

        int tmpPos = left;

        int num = right - left + 1;

        while (left <= leftEnd && rightPos <= right) {

            if (a[left] < a[rightPos]) {

                tmp[tmpPos++] = a[left++];

            } else {

                tmp[tmpPos++] = a[rightPos++];

            }

        }

        while (left <= leftEnd) {

            tmp[tmpPos++] = a[left++];

        }

        while (rightPos <= right) {

            tmp[tmpPos++] = a[rightPos++];

        }

        for (int i = 0; i < num; i++, right--) {

            a[right] = tmp[right];

        }

    }

    //折半排序

    public static void halfSort(int[] array) {

        int low, high, mid;

        int tmp, j;

        for (int i = 1; i < array.length; i++) {

            tmp = array[i];

            low = 0;

            high = i - 1;

            while (low <= high) {

                mid = low + (high - low) / 2;

                if (array[mid] > tmp)

                    high = mid - 1;

                else

                    low = mid + 1;

            }

            for (j = i - 1; j > high; j--) {

                array[j + 1] = array[j];

            }

            array[high + 1] = tmp;

        }

    }

    

  

你可能感兴趣的:(排序)