diff --git a/work/Sort.java b/work/Sort.java new file mode 100644 index 0000000000000000000000000000000000000000..5a6c4d0537de7493acc501353333457036d85958 --- /dev/null +++ b/work/Sort.java @@ -0,0 +1,152 @@ +public class Sort { + public static void main(String[] args) { + int[] arr = new int[10000000]; + for (int i = 0; i < 10000000; i++) { + arr[i] = (int) (Math.random() * 80000000); + } + + selectSort(arr); + int[] array = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8}; + int[] temp = new int[array.length]; + print(array); + MergeSort(array, 0, array.length - 1, temp); + print(array); + } + + //选择排序 + public static void selectSort(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + for (int j = i + 1; j < array.length; j++) { + if (array[i] > array[j]) { + int temp = array[j]; + array[j] = array[i]; + array[i] = temp; + } + } + } + } + + //冒泡排序 + public static void bubbleSortBetter(int[] array) { + int temp = 0; + for (int i = 0; i < array.length - 1; i++) { + int exchageCount = 0; + for (int j = 0; j < array.length - 1 - i; j++) { + if (array[j] > array[j + 1]) { + exchageCount++; + temp = array[j + 1]; + array[j + 1] = array[j]; + array[j] = temp; + } + } + if (exchageCount == 0) { + break; + } + } + } + + + //插入排序 + public static void insertSort(int[] array) { + for (int i = 1; i < array.length; i++) { + int insertVal = array[i]; + int insertIndex = i - 1; + + + while (insertIndex >= 0 && insertVal < array[insertIndex]) { + array[insertIndex + 1] = array[insertIndex]; + insertIndex--; + } + + array[insertIndex + 1] = insertVal; + } + } + + + + //快速排序 + public static void quickSort(int[] array, int leftIndex, int reightIndex) { + if (leftIndex >= reightIndex) { + return; + } + int left = leftIndex; + int reight = reightIndex; + int key = array[left]; + + while (left < reight) { + while (key <= array[reight] && left < reight) { + reight--; + } + array[left] = array[reight]; + while (key >= array[left] && left < reight) { + left++; + } + array[reight] = array[left]; + } + + array[left] = key; + quickSort(array, leftIndex, left - 1); + quickSort(array, reight + 1, reightIndex); + } + + + //归并排序 + public static void MergeSort(int[] array, int begin, int end, int[] temp) { + if (begin < end) { + int mid = (begin + end) / 2; + //递归 将序列不停的二分 + MergeSort(array, begin, mid, temp); + MergeSort(array, mid + 1, end, temp); + //合并 + merge(array, begin, mid, end, temp); + } + } + + public static void merge(int[] array, int begin, int mid, int end, int[] temp) { + + int i = begin; + int j = mid + 1; + int t = 0; + + while (i <= mid && j <= end) { + if (array[i] <= array[j]) { + temp[t] = array[i]; + t++; + i++; + } else { + temp[t] = array[j]; + t++; + j++; + } + } + + while (i <= mid) { + temp[t] = array[i]; + t++; + i++; + } + + while (j <= end) { + temp[t] = array[j]; + t++; + j++; + } + + t = 0; + //int tempLeft = begin; + while (begin <= end) { + array[begin] = temp[t]; + t++; + begin++; + } + + } + + + public static void print(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + " "); + } + System.out.println(); + } +}