diff --git a/bin/sort/BubbleSort.class b/bin/sort/BubbleSort.class new file mode 100644 index 00000000..31b8fe66 Binary files /dev/null and b/bin/sort/BubbleSort.class differ diff --git a/bin/sort/Bubble_sort.class b/bin/sort/Bubble_sort.class deleted file mode 100644 index 1d8b19ca..00000000 Binary files a/bin/sort/Bubble_sort.class and /dev/null differ diff --git a/bin/sort/HeapSort.class b/bin/sort/HeapSort.class new file mode 100644 index 00000000..2307d99f Binary files /dev/null and b/bin/sort/HeapSort.class differ diff --git a/bin/sort/Heap_sort.class b/bin/sort/Heap_sort.class deleted file mode 100644 index 1038bb04..00000000 Binary files a/bin/sort/Heap_sort.class and /dev/null differ diff --git a/bin/sort/InsertionSort.class b/bin/sort/InsertionSort.class new file mode 100644 index 00000000..1d268764 Binary files /dev/null and b/bin/sort/InsertionSort.class differ diff --git a/bin/sort/Insertion_sort.class b/bin/sort/Insertion_sort.class deleted file mode 100644 index 2e5e8d7f..00000000 Binary files a/bin/sort/Insertion_sort.class and /dev/null differ diff --git a/bin/sort/Quick_sort.class b/bin/sort/QuickSort.class similarity index 77% rename from bin/sort/Quick_sort.class rename to bin/sort/QuickSort.class index 0581503c..ca7e080d 100644 Binary files a/bin/sort/Quick_sort.class and b/bin/sort/QuickSort.class differ diff --git a/bin/sort/Selection_sort.class b/bin/sort/SelectionSort.class similarity index 50% rename from bin/sort/Selection_sort.class rename to bin/sort/SelectionSort.class index 43b00ae2..10898fc0 100644 Binary files a/bin/sort/Selection_sort.class and b/bin/sort/SelectionSort.class differ diff --git a/bin/sort/SortTest.class b/bin/sort/SortTest.class new file mode 100644 index 00000000..002fd215 Binary files /dev/null and b/bin/sort/SortTest.class differ diff --git a/bin/sort/Sort_test.class b/bin/sort/Sort_test.class deleted file mode 100644 index 96ed6598..00000000 Binary files a/bin/sort/Sort_test.class and /dev/null differ diff --git a/data_structure/sort/Bubble_sort.java b/data_structure/sort/BubbleSort.java similarity index 96% rename from data_structure/sort/Bubble_sort.java rename to data_structure/sort/BubbleSort.java index f185e6b7..1a8ed0be 100644 --- a/data_structure/sort/Bubble_sort.java +++ b/data_structure/sort/BubbleSort.java @@ -8,7 +8,7 @@ *@version 1.0 */ -public class Bubble_sort { +public class BubbleSort { public int [] Bubble_sort(int [] sort_num) {//参数:int类型数组,对数组进行排序 diff --git a/data_structure/sort/HeapSort.java b/data_structure/sort/HeapSort.java new file mode 100644 index 00000000..d35fe539 --- /dev/null +++ b/data_structure/sort/HeapSort.java @@ -0,0 +1,68 @@ +package sort; + +/** + *@author liujun + *@date: 2018-7-19 上午11:55:48 + *@author—Email:ljfirst@mail.ustc.edu.cn + *@description:堆排序,初始化建堆的时间复杂度为O(n), + *排序重建堆的时间复杂度为nlog(n),所以总的时间复杂度为O(n+nlogn)=O(nlogn)。 + *思路: + *1)初试化建堆,堆顶即最大元素。 + *2)交换堆顶和数组末尾元素,然后针对剩余的n-1个元素,对堆顶元素进行调整即可, + *3)重复2),直到所有元素有序。 + *如何将数组调整为大顶堆 + * 1、初始化建堆只需要对二叉树的非叶子节点调用,从数组二分之n处分别跟左右孩子对比, + *发生交换,交换后的孩子,进一步跟它的左右孩子发生对比,直到数组对比达到边界, + *至此,二分之n这个二叉树有序(大顶堆)。O(n) + * 2、不断缩小范围,自底向上,从右到左,从二分之n循环到顶(1),至此,整个数组/二叉树有序(大顶堆)。 + *@version 1.0 + */ + +public class HeapSort { + + public void heapSort(int [] heap) { + //输入检查 + if (heap == null || heap.length <= 1) { + return; + } + + //初试化建堆 + for (int i = (heap.length - 1) / 2; i >= 0 ; i--) { + heapify(heap, i, heap.length - 1); + } + + //交换堆顶和数组末尾元素,循环整堆,注意边界值 + for (int i = heap.length - 1; i > 0; i--) { + int temp = heap[0]; + heap[0] = heap[i]; + heap[i] = temp; + heapify(heap, 0, i-1); + } + } + + //整堆函数 + public void heapify(int[] heap, int parent, int border){ + +// for(int i = parent; i >= 0; i--){ + + //左孩子,最大值标记 + int flag = parent * 2 + 1; + //越界判断 + if(flag > border){ + return ; + } + //如果右孩子存在 + if(flag + 1 <= border){ + //左右孩子对比,找最大值 + flag = heap[flag] > heap[flag + 1] ? flag : flag + 1; + } + //对比父节点和孩子结点,找最大值,发生交换,并递归其最大值孩子结点 + if(heap[flag] > heap[parent]){ + int temp = heap[flag]; + heap[flag] = heap[parent]; + heap[parent] = temp; + heapify(heap, flag, border); + } +// } + } +} diff --git a/data_structure/sort/Heap_sort.java b/data_structure/sort/Heap_sort.java deleted file mode 100644 index 40e6db08..00000000 --- a/data_structure/sort/Heap_sort.java +++ /dev/null @@ -1,13 +0,0 @@ -package sort; - -/** - *@author liujun - *@date: 2018-7-19 上午11:55:48 - *@author—Email:ljfirst@mail.ustc.edu.cn - *@description:堆排序 - *@version 1.0 - */ - -public class Heap_sort { - -} diff --git a/data_structure/sort/Insertion_sort.java b/data_structure/sort/InsertionSort.java similarity index 95% rename from data_structure/sort/Insertion_sort.java rename to data_structure/sort/InsertionSort.java index 726d13a6..520235dc 100644 --- a/data_structure/sort/Insertion_sort.java +++ b/data_structure/sort/InsertionSort.java @@ -8,7 +8,7 @@ *@version 1.0 */ -public class Insertion_sort { +public class InsertionSort { public int [] Insertion_sort(int [] sort_num) { // TODO Auto-generated constructor stub diff --git a/data_structure/sort/Quick_sort.java b/data_structure/sort/QuickSort.java similarity index 98% rename from data_structure/sort/Quick_sort.java rename to data_structure/sort/QuickSort.java index cd6c572e..46763585 100644 --- a/data_structure/sort/Quick_sort.java +++ b/data_structure/sort/QuickSort.java @@ -27,7 +27,7 @@ *@version 1.0 */ -public class Quick_sort { +public class QuickSort { //单向快排: public void Quick_sort_Simplex(int [] sort_num, int left, int right) { diff --git a/data_structure/sort/Selection_sort.java b/data_structure/sort/SelectionSort.java similarity index 95% rename from data_structure/sort/Selection_sort.java rename to data_structure/sort/SelectionSort.java index 9f4d62ab..3fbd5dc2 100644 --- a/data_structure/sort/Selection_sort.java +++ b/data_structure/sort/SelectionSort.java @@ -8,7 +8,7 @@ *@version 1.0 */ -public class Selection_sort { +public class SelectionSort { public int [] Selection_sort(int [] sort_num) { // TODO Auto-generated constructor stub diff --git a/data_structure/sort/Sort_test.java b/data_structure/sort/SortTest.java similarity index 79% rename from data_structure/sort/Sort_test.java rename to data_structure/sort/SortTest.java index 8fbb80d7..e2398b54 100644 --- a/data_structure/sort/Sort_test.java +++ b/data_structure/sort/SortTest.java @@ -6,7 +6,7 @@ *@description: *@version 1.0 */ -public class Sort_test { +public class SortTest { public static void main(String[] args) { @@ -26,10 +26,13 @@ public static void main(String[] args) { //sort_num = ins.Insertion_sort(sort_num); //快速排序测试 - Quick_sort quick_s = new Quick_sort(); + //QuickSort quick_s = new QuickSort(); //quick_s.Quick_sort_Simplex(sort_num, 0, sort_num.length-1); - quick_s.Quick_sort_duplexing(sort_num, 0, sort_num.length-1); + //quick_s.Quick_sort_duplexing(sort_num, 0, sort_num.length-1); + //堆排序 + HeapSort hp = new HeapSort(); + hp.heapSort(sort_num); //输出数组 for (int i = 0; i < sort_num.length; i++) { System.out.print(sort_num[i]+" ");