Skip to content

Commit 6800f24

Browse files
author
laileon
committed
merger algo
1 parent 26d56e9 commit 6800f24

15 files changed

+467
-1
lines changed

src/com/blankj/csutom/CircleNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blankj.csutom;
22

3-
import com.blankj.structure.ListNode;
3+
4+
import com.blankj.csutom.structure.ListNode;
45

56
public class CircleNode {
67

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.blankj.csutom.sort;
2+
3+
/**
4+
* Created by laileon on 2017/7/20.
5+
*/
6+
7+
//核心:冒泡,持续比较相邻元素,大的挪到后面,因此 {大的会逐步往后挪},故称之为冒泡。
8+
//时间:平均最坏O(n2),空间O(1)
9+
//冒泡是把最大依次往后,选择是把最小依次放前
10+
public class BubbleSort {
11+
public static void main(String[] args) {
12+
int[] unsortedArray = new int[]{5,6,2,7,9,0,1};
13+
bubblesort(unsortedArray);
14+
System.out.println("After sort: ");
15+
for (int item : unsortedArray){
16+
System.out.println(item + " ");
17+
}
18+
}
19+
20+
private static void bubblesort(int[] unsortedArray) {
21+
int len = unsortedArray.length;
22+
for (int i = 0; i < len; i++) {
23+
//运行一次后最大的被挪到最后
24+
for (int j = 1; j < len - i; j++) {
25+
if (unsortedArray[j - 1] > unsortedArray[j]){
26+
Utils.swap(j - 1, j, unsortedArray);
27+
}
28+
}
29+
}
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.blankj.csutom.sort;
2+
3+
/**
4+
* Created by laileon on 2017/7/20.
5+
*/
6+
//核心:通过构建有序序列,对于未排序序列,在已排序序列中从后向前扫描(对于单向链表则只能从前往后遍历),
7+
// 找到相应位置并插入。实现上通常使用in-place排序(需用到O(1)的额外空间)
8+
9+
// “核心:基于插入排序,使数组中任意间隔为h的元素都是有序的,即将全部元素分为h个区域使用插入排序。
10+
// 其实现可类似于插入排序但使用不同增量。更高效的原因是它权衡了子数组的规模和有序性。
11+
12+
public class InsertionSort {
13+
public static void main(String[] args) {
14+
int[] unsortedArray = new int[]{5,6,2,7,9,0,11};
15+
insertionSort(unsortedArray);
16+
System.out.println("After sort: ");
17+
for (int item : unsortedArray){
18+
System.out.println(item + " ");
19+
}
20+
}
21+
22+
private static void insertionSort(int[] unsortedArray) {
23+
int len = unsortedArray.length;
24+
for (int i = 0; i < len; i++) {
25+
//右边将作比较的数
26+
// int temp = unsortedArray[i];
27+
while (i > 0 && unsortedArray[i - 1] > unsortedArray[i]){
28+
Utils.swap(i - 1, i, unsortedArray);
29+
i --;
30+
}
31+
// unsortedArray[i] = temp;
32+
}
33+
}
34+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.blankj.csutom.sort;
2+
3+
//时间复杂度为O(N log N ), 使用了等长的辅助数组,空间复杂度为O(N)
4+
//将两个有序对数组归并成一个更大的有序数组。通常做法为递归排序,并将两个不同的有序数组归并到第三个数组中。
5+
// 先来看看动图,归并排序是一种典型的分治应用
6+
7+
8+
//“快排的实现我们可以发现其与『归并排序』的区别主要有如下两点:
9+
//
10+
//归并排序将数组分成两个子数组分别排序,并将有序的子数组归并以将整个数组排序。递归调用发生在处理整个数组之前。
11+
//快速排序将一个数组分成两个子数组并对这两个子数组独立地排序,两个子数组有序时整个数组也就有序了。递归调用发生在处理整个数组之后。”
12+
13+
import java.util.Arrays;
14+
15+
public class MergeSort {
16+
public static void merge(int[] a, int low, int mid, int high) {
17+
int[] temp = new int[high - low + 1];
18+
int i = low;// 左指针
19+
int j = mid + 1;// 右指针
20+
int k = 0;
21+
// 把较小的数先移到新数组中
22+
while (i <= mid && j <= high) {
23+
if (a[i] < a[j]) {
24+
temp[k++] = a[i++];
25+
} else {
26+
temp[k++] = a[j++];
27+
}
28+
}
29+
// 把左边剩余的数移入数组
30+
while (i <= mid) {
31+
temp[k++] = a[i++];
32+
}
33+
// 把右边边剩余的数移入数组
34+
while (j <= high) {
35+
temp[k++] = a[j++];
36+
}
37+
// 把新数组中的数覆盖nums数组
38+
for (int k2 = 0; k2 < temp.length; k2++) {
39+
a[k2 + low] = temp[k2];
40+
}
41+
}
42+
43+
public static void mergeSort(int[] a, int low, int high) {
44+
int mid = (low + high) / 2;
45+
if (low < high) {
46+
// 左边
47+
mergeSort(a, low, mid);
48+
// 右边
49+
mergeSort(a, mid + 1, high);
50+
// 左右归并
51+
merge(a, low, mid, high);
52+
System.out.println(Arrays.toString(a));
53+
}
54+
55+
}
56+
57+
public static void main(String[] args) {
58+
int a[] = { 51, 46, 20, 18, 65, 97, 82, 30, 77, 50 };
59+
mergeSort(a, 0, a.length - 1);
60+
System.out.println("排序结果:" + Arrays.toString(a));
61+
}
62+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.blankj.csutom.sort;
2+
3+
public class QuickSort {
4+
public static void main(String[] args) {
5+
int unsortedArray[] = new int[]{6, 5, 3, 1, 8, 7, 2, 4};
6+
quickSort(unsortedArray);
7+
System.out.println("After sort: ");
8+
for (int item : unsortedArray) {
9+
System.out.print(item + " ");
10+
}
11+
}
12+
13+
public static void quickSort2(int[] array, int l, int u) {
14+
for (int item : array) {
15+
System.out.print(item + " ");
16+
}
17+
System.out.println();
18+
if (l >= u) return;
19+
int pivot = array[l];
20+
int left = l + 1;
21+
int right = u;
22+
while (left <= right) {
23+
while (left <= right && array[left] < pivot) {
24+
left++;
25+
}
26+
while (left <= right && array[right] >= pivot) {
27+
right--;
28+
}
29+
if (left > right) break;
30+
// swap array[left] with array[right] while left <= right
31+
int temp = array[right];
32+
array[right] = array[left];
33+
array[left] = temp;
34+
}
35+
/* swap the smaller with pivot */
36+
int temp = array[right];
37+
array[right] = array[l];
38+
array[l] = temp;
39+
quickSort2(array, l, right - 1);
40+
quickSort2(array, right + 1, u);
41+
}
42+
43+
public static void quickSort(int[] array) {
44+
quickSort2(array, 0, array.length - 1);
45+
}
46+
}

src/com/blankj/csutom/sort/README

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
时间复杂度-执行时间(比较和交换次数)
2+
3+
空间复杂度-所消耗的额外内存空间
4+
使用小堆栈或表
5+
使用链表或指针、数组索引来代表数据
6+
排序数据的副本
7+
对具有重键的数据(同一组数按不同键多次排序)进行排序时,需要考虑排序方法的稳定性,在非稳定性排序算法中需要稳定性时可考虑加入小索引。
8+
9+
稳定性:如果排序后文件中拥有相同键的项的相对位置不变,这种排序方式是稳定的。
10+
11+
常见的排序算法根据是否需要比较可以分为如下几类:
12+
13+
Comparison Sorting:
14+
Bubble Sort
15+
Selection Sort
16+
Insertion Sort
17+
Shell Sort
18+
Merge Sort
19+
Quck Sort
20+
Heap Sort
21+
22+
Bucket Sort
23+
24+
Counting Sort
25+
26+
Radix Sort
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.blankj.csutom.sort;
2+
3+
/**
4+
* Created by laileon on 2017/7/20.
5+
*/
6+
//核心:不断地选择剩余元素中的最小者。
7+
//
8+
// 找到数组中最小元素并将其和数组第一个元素交换位置。
9+
// 在剩下的元素中找到最小元素并将其与数组第二个元素交换,直至整个数组排序。
10+
// 性质:
11+
//
12+
// 比较次数=(N-1)+(N-2)+(N-3)+...+2+1~N^2/2
13+
// 交换次数=N
14+
// 运行时间与输入无关
15+
16+
public class SelectionSort {
17+
public static void main(String[] args) {
18+
int[] unsortedArray = new int[]{5,6,2,7,9,0,11};
19+
selectionSort(unsortedArray);
20+
System.out.println("After sort: ");
21+
for (int item : unsortedArray){
22+
System.out.println(item + " ");
23+
}
24+
}
25+
26+
private static void selectionSort(int[] unsortedArray) {
27+
int len = unsortedArray.length;
28+
for (int i = 0; i < len; i++) {
29+
//每次最小的index经转换后一定为最前
30+
int min_index = i;
31+
for (int j = i + 1; j < len; j++) {
32+
if (unsortedArray[j] < unsortedArray[min_index]) {
33+
min_index = j;
34+
}
35+
}
36+
Utils.swap(min_index, i, unsortedArray);
37+
}
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.blankj.csutom.sort;
2+
3+
/**
4+
* Created by laileon on 2017/7/21.
5+
*/
6+
7+
//希尔排序对直接插入排序进行了简单的改进:它通过加大插入排序中元素之间的间隔,并在这些有间隔的元素中进行插入排序,从而使数据项大跨度地移动,
8+
// 当这些数据项排过一趟序之后,希尔排序算法减小数据项的间隔再进行排序,依次进行下去,进行这些排序时的数据项之间的间隔被称为增量,
9+
// 习惯上用字母h来表示这个增量。
10+
public class ShellSort {
11+
public static void main(String[] args) {
12+
int[] unsortedArray = new int[]{5, 6, 2, 7, 9, 0, 11, 26, 53, 67, 48, 57, 13, 48, 32, 60, 50};
13+
shellSort(unsortedArray);
14+
System.out.println("After sort: ");
15+
for (int item : unsortedArray) {
16+
System.out.println(item + " ");
17+
}
18+
}
19+
20+
private static void shellSort(int[] unsortedArray) {
21+
// 计算出最大的h值
22+
int h = 1;
23+
//常用的h序列由Knuth提出,该序列从1开始,通过如下公式产生:
24+
while (h <= unsortedArray.length / 3) {
25+
h = h * 3 + 1;
26+
}
27+
while (h > 0) {
28+
for (int i = h; i < unsortedArray.length; i += h) {
29+
if (unsortedArray[i] < unsortedArray[i - h]) {
30+
int tmp = unsortedArray[i];
31+
int j = i - h;
32+
while (j >= 0 && unsortedArray[j] > tmp) {
33+
unsortedArray[j + h] = unsortedArray[j];
34+
j -= h;
35+
}
36+
unsortedArray[j + h] = tmp;
37+
}
38+
}
39+
// 计算出下一个h值
40+
h = (h - 1) / 3;
41+
}
42+
}
43+
}

src/com/blankj/csutom/sort/Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.blankj.csutom.sort;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
int a = 5;
6+
int b = 10;
7+
a = b;
8+
System.out.println(a + " " + b);
9+
}
10+
}

src/com/blankj/csutom/sort/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.blankj.csutom.sort;
2+
3+
/**
4+
* Created by laileon on 2017/7/20.
5+
*/
6+
public class Utils {
7+
public static void swap(int i, int j, int[] unsortedArray){
8+
int temp = unsortedArray[i];
9+
unsortedArray[i] = unsortedArray[j];
10+
unsortedArray[j] = temp;
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.blankj.csutom.structure;
2+
3+
/**
4+
* Created by laileon on 2017/7/20.
5+
*/
6+
//双向链表
7+
public class DListNode {
8+
int val;
9+
DListNode prev, next;
10+
11+
public DListNode(int val, DListNode prev, DListNode next) {
12+
this.val = val;
13+
this.prev = null;
14+
this.next = null;
15+
}
16+
17+
//双向链表核心在于next和prev的交换。
18+
public DListNode reverseList(DListNode head) {
19+
DListNode curr = null;
20+
while (head != null) {
21+
curr = head;
22+
head = curr.next;
23+
curr.next = curr.prev;
24+
curr.prev = head;
25+
}
26+
return curr;
27+
}
28+
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.blankj.csutom.structure;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Created by laileon on 2017/7/20.
7+
*/
8+
public class Graph {
9+
//邻接矩阵
10+
int[][]g = new int[1][2];
11+
//邻接表
12+
class DirectGraphNode {
13+
int label;
14+
ArrayList<DirectGraphNode> neighbors;
15+
16+
public DirectGraphNode(int label) {
17+
this.label = label;
18+
neighbors = new ArrayList<DirectGraphNode>();
19+
}
20+
}
21+
22+
//无向图
23+
class UndirectGraphNode {
24+
int label;
25+
ArrayList<UndirectGraphNode> neighbors;
26+
27+
public UndirectGraphNode(int label) {
28+
this.label = label;
29+
neighbors = new ArrayList<UndirectGraphNode>();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)