1. Bubble, merge and fast algorithm test
1.1. Bubble sorting
/**
* Bubble sort
*/
private void bubbleSort(int[] arr) {
for (int i = arr.length; i > 0; i--) {
for (int j = arr.length - 1; j > arr.length - i; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
1.2. Quick sorting
/**
* Quick sort
*/
private void qSort(int[] arr, int head, int tail) {
if (arr == null || head >= tail || arr.length == 0) {
return;
}
int i = head, j = tail, q = arr[(i + j) / 2];
while (i < j) {
while (arr[i] < q) {
++i;
}
while (arr[j] > q) {
--j;
}
if (i < j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
++i;
--j;
} else if (i == j) {
++i;
}
}
qSort(arr, head, j);
qSort(arr, i, tail);
}
1.3. Merge and sort
static void merge_sort_recursive(int[] arr, int[] result, int start, int end) {
if (start >= end)
return;
int len = end - start, mid = (len >> 1) + start;
int start1 = start, end1 = mid;
int start2 = mid + 1, end2 = end;
merge_sort_recursive(arr, result, start1, end1);
merge_sort_recursive(arr, result, start2, end2);
int k = start;
while (start1 <= end1 && start2 <= end2)
result[k++] = arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
while (start1 <= end1)
result[k++] = arr[start1++];
while (start2 <= end2)
result[k++] = arr[start2++];
for (k = start; k <= end; k++)
arr[k] = result[k];
}
public static void merge_sort(int[] arr) {
int len = arr.length;
int[] result = new int[len];
merge_sort_recursive(arr, result, 0, len - 1);
}
1.4. Test code
@Test
public void testSpeed(){
Random random = new Random();
int arrs[] = new int[30000];
for (int i = 0; i < arrs.length; i++) {
arrs[i] = random.nextInt(10000);
}
int[] arrs2 = arrs.clone();
int[] arrs3 = arrs.clone();
int[] arrs4 = arrs.clone();
long start = System.currentTimeMillis();
bubbleSort(arrs);
long end1 = System.currentTimeMillis();
log.info("Bubble sort time:{}ms",(end1-start));
qSort(arrs2,0,arrs2.length-1);
long end2 = System.currentTimeMillis();
log.info("Quick sort time:{}ms",(end2-end1));
merge_sort(arrs3);
long end3 = System.currentTimeMillis();
log.info("Merge sort time:{}ms",(end3-end2));
Arrays.sort(arrs4);
long end4 = System.currentTimeMillis();
log.info("java.util sort time:{}ms",(end4-end3));
}
1.5. Test results
- When the array length is 60000
15:35:28.609 [main] INFO com.zhiyis.framework.learn.sort.testSort - Bubble sort time:8497ms
15:35:28.645 [main] INFO com.zhiyis.framework.learn.sort.testSort - Quick sort time:40ms
15:35:28.669 [main] INFO com.zhiyis.framework.learn.sort.testSort - Merge sort time:24ms
15:35:28.696 [main] INFO com.zhiyis.framework.learn.sort.testSort - java.util sort time:27ms
- When the array length is 30000
15:34:48.389 [main] INFO com.zhiyis.framework.learn.sort.testSort - Bubble sort time:2727ms
15:34:48.403 [main] INFO com.zhiyis.framework.learn.sort.testSort - Quick sort time:18ms
15:34:48.418 [main] INFO com.zhiyis.framework.learn.sort.testSort - Merge sort time:15ms
15:34:48.428 [main] INFO com.zhiyis.framework.learn.sort.testSort - java.util sort time:10ms
- When the array length is 3000
15:33:52.156 [main] INFO com.zhiyis.framework.learn.sort.testSort - Bubble sort time:22ms
15:33:52.167 [main] INFO com.zhiyis.framework.learn.sort.testSort - Quick sort time:21ms
15:33:52.171 [main] INFO com.zhiyis.framework.learn.sort.testSort - Merge sort time:4ms
15:33:52.183 [main] INFO com.zhiyis.framework.learn.sort.testSort - java.util sort time:12ms
- When the array length is 300
15:33:34.345 [main] INFO com.zhiyis.framework.learn.sort.testSort - Bubble sort time:3ms
15:33:34.356 [main] INFO com.zhiyis.framework.learn.sort.testSort - Quick sort time:17ms
15:33:34.357 [main] INFO com.zhiyis.framework.learn.sort.testSort - Merge sort time:1ms
15:33:34.357 [main] INFO com.zhiyis.framework.learn.sort.testSort - java.util sort time:0ms
- When the array length is 30
15:33:07.920 [main] INFO com.zhiyis.framework.learn.sort.testSort - Bubble sort time:0ms
15:33:07.933 [main] INFO com.zhiyis.framework.learn.sort.testSort - Quick sort time:21ms
15:33:07.933 [main] INFO com.zhiyis.framework.learn.sort.testSort - Merge sort time:0ms
15:33:07.934 [main] INFO com.zhiyis.framework.learn.sort.testSort - java.util sort time:1ms
1.6. summary
- What are the variable control problems in my test
- The best time complexity of bubble sorting is O(N), the average and the worst O(N^2); the best time complexity of fast sorting is NLog, the average is NLog, the worst N^2; the best time complexity of merge sorting is NLog, the best time complexity and the worst time complexity are NLog. According to the time complexity of the algorithm, merging and sorting is the most stable
- According to the experimental results, when the number of sorting is relatively small, the bubbling sorting efficiency is also OK, but once the number of sorting is large, it is still fast and efficient. Of course, we often use the tool class provided to us by java by default. We don't need to write our own algorithm, and the efficiency is no worse than merge.