diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..beec571aa7a222c66aff8f6b3e34a8e78512f4cf --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../:\Users\Rrank\IdeaProjects\lec08-sort\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/lec08-sort.iml b/.idea/lec08-sort.iml new file mode 100644 index 0000000000000000000000000000000000000000..d6ebd4805981b8400db3e3291c74a743fef9a824 --- /dev/null +++ b/.idea/lec08-sort.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..e208459b8afde5f7980720efd6bbb97f7ae24541 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..fe5d9d007710ecde148b4e98516f5f4d3e0e2697 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 0000000000000000000000000000000000000000..9de0949c2fd6bc022f678f49d2cc131b61173777 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,21 @@ + + + public class BubbleSort { + public static void sort(int[] array) { + for (int i = 0; i < array.length; i++) { + for (int n = 0; n < array.length - 1 - i; n++) { + if (array[n] > array[n + 1]) { + swap(array, n, n + 1); + } + } + } + } + + private static void swap(int[] array, int minIndex, int index) { + int temp = array[index]; + array[index] = array[minIndex]; + array[minIndex] = temp; + } + + } + diff --git a/InsertionSort.java b/InsertionSort.java new file mode 100644 index 0000000000000000000000000000000000000000..4db8f640808925b749456069378b31052c34370f --- /dev/null +++ b/InsertionSort.java @@ -0,0 +1,15 @@ +public class InsertionSort { + public static void sort(int[] array) { + for (int i = 1; i < array.length; i++) { + for (int n = i; n > 0 && array[n] < array[n - 1]; n--) { + swap(array, n, n - 1); + } + } + } + + private static void swap(int[] array, int minIndex, int maxIndex) { + int temp = array[minIndex]; + array[minIndex] = array[maxIndex]; + array[maxIndex] = temp; + } +} diff --git a/MergeSort.java b/MergeSort.java new file mode 100644 index 0000000000000000000000000000000000000000..39b4a35767c5bb0280920a10c2b4b93532b3a12c --- /dev/null +++ b/MergeSort.java @@ -0,0 +1,41 @@ +public class MergeSort { + public static int[] sort(int[] array) { + return sort(array, 0, array.length-1); + } + + private static int[] sort(int[] array, int left, int right) { + if (left == right) { + int temp = array[left]; + int[] newNum = {temp}; + return newNum; + } + + int mid = left + (right - left) / 2; + int[] leftArr = sort(array, left, mid); + int[] rightArr = sort(array, mid + 1, right); + int[] newNum = new int[leftArr.length + rightArr.length]; + + int m = 0, i = 0, j = 0; + while (i < leftArr.length && j < rightArr.length) { + if (leftArr[i] < rightArr[j]) { + newNum[m] = leftArr[i]; + i++; + } else { + newNum[m] = rightArr[j]; + j++; + } + m++; + } + while (i < leftArr.length) { + newNum[m] = leftArr[i]; + m++; + i++; + } + while (j < rightArr.length) { + newNum[m] = rightArr[j]; + m++; + j++; + } + return newNum; + } +} \ No newline at end of file diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 0000000000000000000000000000000000000000..721ac6a6b1c2cca23ea79a30db4b3d126e638142 --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,29 @@ +public class QuickSort { + public static void sort(int[] array) { + sort(array, 0, array.length - 1); + } + + public static void sort(int[] array, int start, int end) { + if (start < end) { + int i = start, j = end, x = array[start]; + while (i < j) { + while (i < j && array[j] >= x) { + j--; + } + if (i < j) { + array[i++] = array[j]; + } + + while (i < j && array[i] < x) { + i++; + } + if (i < j) { + array[j--] = array[i]; + } + } + array[i] = x; + sort(array, start, i - 1); + sort(array, i + 1, end); + } + } +} \ No newline at end of file diff --git a/SelectionSort.java b/SelectionSort.java new file mode 100644 index 0000000000000000000000000000000000000000..72d13cca960166b1d2661b259ad9a9d735b4d18d --- /dev/null +++ b/SelectionSort.java @@ -0,0 +1,19 @@ +public class SelectionSort { + public static void sort(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + int minIndex = i; + for (int n = i + 1; n < array.length; n++) { + if (array[minIndex] > array[n]) { + minIndex = n; + } + } + swap(array, minIndex, i); + } + } + + private static void swap(int[] array, int minIndex, int index) { + int temp = array[index]; + array[index] = array[minIndex]; + array[minIndex] = temp; + } +} \ No newline at end of file diff --git a/ShellsSort.java b/ShellsSort.java new file mode 100644 index 0000000000000000000000000000000000000000..b8f0b8225930a8783689fa8c1a1293dc73ed112d --- /dev/null +++ b/ShellsSort.java @@ -0,0 +1,19 @@ +public class ShellsSort { + public static void sort(int[] array) { + int gap = array.length; + do { + gap /= 2; + for (int i = 0; i < gap; i++) { + for (int j = i + gap; j < array.length; j += gap) { + int k = j - gap; + while (k >= 0 && array[k] > array[k + gap]) { + int temp = array[k]; + array[k] = array[k + gap]; + array[k + gap] = temp; + k -= gap; + } + } + } + } while (gap != 1); + } +} diff --git a/Test.java b/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..d871542752802176d6586a330806651772d2a674 --- /dev/null +++ b/Test.java @@ -0,0 +1,54 @@ +import java.util.Random; + +public class Test { + public static void main(String[] args) { + int[] array = new int[100000]; + Random random = new Random(); + for (int i = 0; i < array.length; i++) { + array[i] = random.nextInt(); + } + + int[] b = new int[100000]; + System.arraycopy(array, 0, b, 0, array.length); + int[] i = new int[100000]; + System.arraycopy(array, 0, i, 0, array.length); + int[] m = new int[100000]; + System.arraycopy(array, 0, m, 0, array.length); + int[] q = new int[100000]; + System.arraycopy(array, 0, q, 0, array.length); + int[] se = new int[100000]; + System.arraycopy(array, 0, se, 0, array.length); + int[] sh = new int[100000]; + System.arraycopy(array, 0, sh, 0, array.length); + + long startTime = System.currentTimeMillis(); + BubbleSort.sort(b); + long endTime = System.currentTimeMillis(); + System.out.println("Bubble Sort运行时间:" + (endTime - startTime) + "ms"); + + startTime = System.currentTimeMillis(); + InsertionSort.sort(i); + endTime = System.currentTimeMillis(); + System.out.println("Insertion Sort运行时间:" + (endTime - startTime) + "ms"); + + startTime = System.currentTimeMillis(); + MergeSort.sort(m); + endTime = System.currentTimeMillis(); + System.out.println("Merge Sort运行时间:" + (endTime - startTime) + "ms"); + + startTime = System.currentTimeMillis(); + QuickSort.sort(q); + endTime = System.currentTimeMillis(); + System.out.println("Quick Sort运行时间:" + (endTime - startTime) + "ms"); + + startTime = System.currentTimeMillis(); + SelectionSort.sort(se); + endTime = System.currentTimeMillis(); + System.out.println("Selection Sort运行时间:" + (endTime - startTime) + "ms"); + + startTime = System.currentTimeMillis(); + ShellsSort.sort(sh); + endTime = System.currentTimeMillis(); + System.out.println("Shell's Sort运行时间:" + (endTime - startTime) + "ms"); + } +} \ No newline at end of file