1. Insertion Sort
1> Straight Insertion Sort: O(n^2), if the records are already sorted, then O(n).
2> Binary Insertion Sort (Reduced comparison count)
3> 2-Way Insertion Sort (Reduced comparison count and move record time)
4> Shell's Sort
2. Swap Sort
1> Bubble Sort: O(n^2)
2> Quick Sort: O(n*lgn), If the array is already ordered, then Quick Sort degenerated to Bubble Sort.
3. Selection Sort
1> Simple Selection Sort: O(n^2)
2> Tree Selection Sort
4> Heap Sort: O(n*lgn)
4. Merging Sort
5. Radix Sort
1. Insertion Sort
1> Straight Insertion Sort
package edu.xmu.datastructure.sort.insertion; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class StraightInsertionSort { @Test public void insertSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> sortedIntList = StraightInsertionSort.insertSort(intList); assertEquals(expIntList, sortedIntList); } public static List<Integer> insertSort(List<Integer> intList) { List<Integer> sortedIntList = Lists.newArrayList(intList); for (int i = 1; i < sortedIntList.size(); i++) { int valueToBeInserted = sortedIntList.get(i); int indexToBeInserted = i; for (int j = i - 1; j >= 0; j--) { if (valueToBeInserted < sortedIntList.get(j)) { sortedIntList.set(j + 1, sortedIntList.get(j)); indexToBeInserted = j; } else { break; } } sortedIntList.set(indexToBeInserted, valueToBeInserted); } return sortedIntList; } }
2> Binary Insertion Sort
package edu.xmu.datastructure.sort.insertion; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class BinaryInsertionSort { @Test public void insertSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> sortedIntList = BinaryInsertionSort .binaryInsertSort(intList); assertEquals(expIntList, sortedIntList); } public static List<Integer> binaryInsertSort(List<Integer> intList) { List<Integer> sortedIntList = Lists.newArrayList(intList); for (int i = 1; i < sortedIntList.size(); i++) { int valueToBeInserted = sortedIntList.get(i); int indexToBeInserted = searchIndexToBeInserted(i, sortedIntList); // Move elements from [indexToBeInserted, i - 1] to // [indexToBeInserted + 1, i] for (int j = i - 1; j >= indexToBeInserted; j--) { if (valueToBeInserted < sortedIntList.get(j)) { sortedIntList.set(j + 1, sortedIntList.get(j)); } } // Insert element to indexToBeInserted position sortedIntList.set(indexToBeInserted, valueToBeInserted); } return sortedIntList; } public static int searchIndexToBeInserted(int hotIndex, List<Integer> sortedIntList) { int startIndex = 0; int endIndex = hotIndex - 1; while (startIndex <= endIndex) { int middle = (startIndex + endIndex) / 2; if (sortedIntList.get(hotIndex) < sortedIntList.get(middle)) { endIndex = middle - 1; } else { startIndex = middle + 1; } } return endIndex + 1; } @Test public void searchIndexToBeInsertedTest() { List<Integer> sortedIntList = Lists.newArrayList(1, 2, 3, 0); int hotIndex = 3; int indexToBeInserted = BinaryInsertionSort.searchIndexToBeInserted( hotIndex, sortedIntList); assertEquals(0, indexToBeInserted); sortedIntList = Lists.newArrayList(1, 2, 3, 4, 0); hotIndex = 4; indexToBeInserted = BinaryInsertionSort.searchIndexToBeInserted( hotIndex, sortedIntList); assertEquals(0, indexToBeInserted); sortedIntList = Lists.newArrayList(2, 4, 6, 8, 10); hotIndex = 4; indexToBeInserted = BinaryInsertionSort.searchIndexToBeInserted( hotIndex, sortedIntList); assertEquals(4, indexToBeInserted); sortedIntList = Lists.newArrayList(2, 4, 6, 8, 10, 5); hotIndex = 5; indexToBeInserted = BinaryInsertionSort.searchIndexToBeInserted( hotIndex, sortedIntList); assertEquals(2, indexToBeInserted); } }
3> Shell's Sort (When we create the deltaList=Lists.newArrayList(1), then Shell's Sort degenerated to Straight Insertion Sort).
package edu.xmu.datastructure.sort.insertion; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class ShellInsertionSort { @Test public void insertSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> deltaList = Lists.newArrayList(5, 3, 2, 1); for (int delta : deltaList) { ShellInsertionSort.shellInsertSort(intList, delta); } assertEquals(expIntList, intList); } public static void shellInsertSort(List<Integer> intList, int delta) { for (int i = delta; i < intList.size(); i++) { int valueToBeInserted = intList.get(i); int indexToBeInserted = i; for (int j = i - delta; j >= 0; j -= delta) { if (valueToBeInserted < intList.get(j)) { intList.set(j + delta, intList.get(j)); indexToBeInserted = j; } else { break; } } intList.set(indexToBeInserted, valueToBeInserted); } } }
2. Swap Sort
1> Bubble Sort
package edu.xmu.datastructure.sort.swap; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class BubbleSort { @Test public void bubbleSortTest() { List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); BubbleSort.bubbleSort(intList); assertEquals(expIntList, intList); } public static void bubbleSort(List<Integer> intList) { for (int i = 0; i < intList.size(); i++) { for (int j = 0; j < intList.size() - i - 1; j++) { int prevValue = intList.get(j); int currValue = intList.get(j + 1); // The biggest value bubbles to the end of the list if (prevValue > currValue) { intList.set(j, currValue); intList.set(j + 1, prevValue); } } } } }
2> Quick Sort
package edu.xmu.datastructure.sort.swap; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class QuickSort { @Test public void quickSortTest() { List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); QuickSort.quickSort(intList, 0, intList.size() - 1); assertEquals(expIntList, intList); } public static void quickSort(List<Integer> intList, int startIndex, int endIndex) { if (startIndex < endIndex) { int pivotIndex = splitList(intList, startIndex, endIndex); quickSort(intList, startIndex, pivotIndex - 1); quickSort(intList, pivotIndex + 1, endIndex); } } @Test public void splitListTest() { List<Integer> expectedIntList = Lists.newArrayList(27, 38, 13, 49, 76, 97, 65); List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); int newPivotIndex = QuickSort.splitList(intList, 0, 6); assertEquals(expectedIntList, intList); assertEquals(3, newPivotIndex); } public static int splitList(List<Integer> intList, int pivotIndex, int high) { int pivotValue = intList.get(pivotIndex); int low = pivotIndex; boolean shouldHighMove = true; while (low < high) { if (shouldHighMove) { if (intList.get(high) < pivotValue) { int lowValue = intList.get(low); int highValue = intList.get(high); intList.set(low, highValue); intList.set(high, lowValue); low++; shouldHighMove = false; } else { shouldHighMove = true; high--; } } else { if (intList.get(low) > pivotValue) { int lowValue = intList.get(low); int highValue = intList.get(high); intList.set(low, highValue); intList.set(high, lowValue); high--; shouldHighMove = true; } else { shouldHighMove = false; low++; } } } if (high != low) { throw new RuntimeException("Defacted Algorithm!"); } intList.set(low, pivotValue); return low; } }
3. Selection Sort
1> Simple Selection Sort
package edu.xmu.datastructure.sort.selection; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class SimpleSelectionSort { @Test public void insertSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); SimpleSelectionSort.selectionSort(intList); assertEquals(expIntList, intList); } public static void selectionSort(List<Integer> intList) { int minValue; int indexToBeSwapped; for (int i = 0; i < intList.size(); i++) { minValue = Integer.MAX_VALUE; indexToBeSwapped = 0; for (int j = i; j < intList.size(); j++) { int temp = intList.get(j); if (minValue > temp) { minValue = temp; indexToBeSwapped = j; } } int value = intList.get(i); intList.set(i, minValue); intList.set(indexToBeSwapped, value); } } }
Simple Selection Sort (Extract getMinValueIndex out, make code easier to read)
package edu.xmu.datastructure.sort.selection; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class SimpleSelectionSort { @Test public void insertSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); SimpleSelectionSort.selectionSort(intList); assertEquals(expIntList, intList); } public static void selectionSort(List<Integer> intList) { for (int i = 0; i < intList.size(); i++) { int minValueIndex = getMinValueIndex(i, intList); int minValue = intList.get(minValueIndex); int tempValue = intList.get(i); intList.set(i, minValue); intList.set(minValueIndex, tempValue); } } private static int getMinValueIndex(int startIndex, List<Integer> intList) { int minValue = Integer.MAX_VALUE; int minValueIndex = 0; for (int j = startIndex; j < intList.size(); j++) { int temp = intList.get(j); if (minValue > temp) { minValue = temp; minValueIndex = j; } } return minValueIndex; } }
2> Tree Selection Sort (Tournament Sort): It's just a transition selection sort algorithm between "Simple Selection Sort" and "Heap Sort"
package edu.xmu.datastructure.sort.selection; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class TreeSelectionSort { @Test public void selectionSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> orderedIntList = TreeSelectionSort .treeSelectionSort(intList); assertEquals(expIntList, orderedIntList); } public static List<Integer> treeSelectionSort(List<Integer> intList) { BinarySelectionTreeNode rootNode = buildTree(intList); List<Integer> orderedIntList = Lists.newArrayListWithCapacity(intList .size()); int minValue; for (int i = 0; i < intList.size(); i++) { rootNode.reElectMinValue(); minValue = rootNode.getValue(); orderedIntList.add(minValue); rootNode.removeMinValue(minValue); } return orderedIntList; } public static BinarySelectionTreeNode buildTree(List<Integer> intList) { List<BinarySelectionTreeNode> leafNodes = Lists .newArrayListWithExpectedSize(intList.size()); for (int value : intList) { BinarySelectionTreeNode leafNode = new BinarySelectionTreeNode(); leafNode.setValue(value); leafNodes.add(leafNode); } List<BinarySelectionTreeNode> rootNode = buildFromNodes(leafNodes); return rootNode.get(0); } private static List<BinarySelectionTreeNode> buildFromNodes( List<BinarySelectionTreeNode> nodes) { if (nodes.size() <= 1) { return nodes; } if (nodes.size() % 2 != 0) { nodes.add(new BinarySelectionTreeNode()); } List<BinarySelectionTreeNode> mergedNodes = Lists .newArrayListWithCapacity(nodes.size() / 2); for (int i = 0; i < nodes.size(); i += 2) { BinarySelectionTreeNode mergedNode = new BinarySelectionTreeNode(); BinarySelectionTreeNode lChildNode = nodes.get(i); BinarySelectionTreeNode rChildNode = nodes.get(i + 1); mergedNode.setlChild(lChildNode); mergedNode.setrChild(rChildNode); mergedNodes.add(mergedNode); } return buildFromNodes(mergedNodes); } public static class BinarySelectionTreeNode { private int value = Integer.MAX_VALUE; private BinarySelectionTreeNode lChild; private BinarySelectionTreeNode rChild; public void removeMinValue(int minValue) { if (null == lChild && null == rChild) { if (minValue == this.value) { this.value = Integer.MAX_VALUE; } } else { if (this.value == minValue) { this.value = Integer.MAX_VALUE; lChild.removeMinValue(minValue); rChild.removeMinValue(minValue); } } } public int reElectMinValue() { int minValue = Integer.MAX_VALUE; if (null == lChild && null == rChild) { minValue = this.value; } else { int lMinValue = lChild.reElectMinValue(); int rMinValue = rChild.reElectMinValue(); minValue = lMinValue < rMinValue ? lMinValue : rMinValue; } this.value = minValue; return minValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public BinarySelectionTreeNode getlChild() { return lChild; } public void setlChild(BinarySelectionTreeNode lChild) { this.lChild = lChild; } public BinarySelectionTreeNode getrChild() { return rChild; } public void setrChild(BinarySelectionTreeNode rChild) { this.rChild = rChild; } } }
3> Heap Sort
package edu.xmu.datastructure.sort.selection; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class HeapSort { @Test public void heapSortTest() { // 49 // 38 65 // 97 76 13 27 List<Integer> intList = Lists.newArrayList(Integer.MAX_VALUE, 49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(Integer.MAX_VALUE, 13, 27, 38, 49, 65, 76, 97); HeapSort.heapSort(intList); assertEquals(expIntList, intList); } public static void heapSort(List<Integer> intList) { for (int i = intList.size() / 2; i >= 1; i--) { adjustHeap(intList, i, intList.size() - 1); } for (int i = intList.size() - 1; i >= 1; i--) { int maxValue = intList.get(1); int tailValue = intList.get(i); intList.set(1, tailValue); intList.set(i, maxValue); adjustHeap(intList, 1, i - 1); } } // Max Heap private static void adjustHeap(List<Integer> intList, int startIndex, int endIndex) { int rootNodeValue = intList.get(startIndex); int currentIndex = startIndex; for (int i = startIndex * 2; i <= endIndex; i *= 2) { if (i + 1 > endIndex) { // For the benefit of obsolete index // When we put the max value at the end of array // Then [start, end - 1] // But here intList.get(i + 1) may unintentionally get the // already set as max and thus unreachable } else if (intList.get(i) < intList.get(i + 1)) { i += 1; } if (rootNodeValue > intList.get(i)) { // rootNode is bigger than its children break; } intList.set(currentIndex, intList.get(i)); currentIndex = i; } intList.set(currentIndex, rootNodeValue); } }
4. Merge Sort
package edu.xmu.datastructure.sort.merge; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; public class MergeSort { @Test public void mergeSortTest() { List<Integer> intList = Lists.newArrayList(49, 38, 65, 97, 76, 13, 27); List<Integer> expIntList = Lists.newArrayList(13, 27, 38, 49, 65, 76, 97); List<Integer> sortedIntList = MergeSort.mergeSort(intList, 0, intList.size() - 1); assertEquals(expIntList, sortedIntList); } public static List<Integer> mergeSort(List<Integer> intList, int startIndex, int endIndex) { List<Integer> mergedList; if (startIndex == endIndex) { mergedList = Lists.newArrayList(intList.get(startIndex)); } else { int middleIndex = (startIndex + endIndex) / 2; List<Integer> leftList = mergeSort(intList, startIndex, middleIndex); List<Integer> rightList = mergeSort(intList, middleIndex + 1, endIndex); mergedList = mergeSortedList(leftList, rightList); } return mergedList; } @Test public void mergeListTest() { List<Integer> leftList = Lists.newArrayList(1, 3, 5, 7); List<Integer> rightList = Lists.newArrayList(0, 2); List<Integer> expectedMergedList = Lists.newArrayList(0, 1, 2, 3, 5, 7); List<Integer> mergedList = MergeSort.mergeSortedList(leftList, rightList); assertEquals(expectedMergedList, mergedList); leftList = Lists.newArrayList(1, 3, 5, 7); rightList = Lists.newArrayList(0, 2, 4, 6, 8, 9); expectedMergedList = Lists.newArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); mergedList = MergeSort.mergeSortedList(leftList, rightList); assertEquals(expectedMergedList, mergedList); } /** * Merge two ascending sorted list into one single asc list * * @param leftList * @param rightList * @return */ public static List<Integer> mergeSortedList(List<Integer> leftList, List<Integer> rightList) { List<Integer> mergedList = Lists.newArrayList(); int leftSize = leftList.size(); int rightSize = rightList.size(); int leftIndex = 0; int rightIndex = 0; int leftValue = Integer.MAX_VALUE; int rightValue = Integer.MAX_VALUE; while (!(leftIndex == leftSize && rightIndex == rightSize)) { if (leftIndex < leftSize) { leftValue = leftList.get(leftIndex); } else { leftValue = Integer.MAX_VALUE; } if (rightIndex < rightSize) { rightValue = rightList.get(rightIndex); } else { rightValue = Integer.MAX_VALUE; } if (rightValue < leftValue) { mergedList.add(rightValue); rightIndex++; } else { mergedList.add(leftValue); leftIndex++; } } return mergedList; } }
Reference Links:
1> http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
2> "Data Structure(C version)" - Yan, Weimin
相关推荐
总的来说,“Sorting-algorithm:排序算法整合”项目是一个理想的资源,无论是对初学者还是经验丰富的程序员,都能从中受益,加深对排序算法和Java编程的理解。通过实际编写和运行这些代码,开发者能够更好地掌握数据...
java_algorithm this is a sorting algorithm demo. created by InterlliJ. that is the java main program. ! com.sample.sort包下 平均时间复杂度 O(n^2) 空间复杂度 O(1) BubbleSort 冒泡排序 SelectionSort 选择...
Sorting-Algorithm###java实现的常用排序算法/** * 1.插入排序算法 * @param int[] 未排序数组 * @return int[] 排完序数组 *插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排...
"JavaData:Java_basic_sorting_algorithm简单"这个主题聚焦于Java中的基础排序算法,这些算法对于理解计算机科学的基本原理至关重要。下面将详细讨论几种常见的Java排序算法。 1. 冒泡排序(Bubble Sort): 冒泡...
在Java编程语言中,我们可以用以下方式实现冒泡排序: ```java public class BubbleSort { public static void bubbleSort(char[] array) { int n = array.length; for (int i = 0; i ; i++) { for (int j = 0;...
Title: Problem Solving in Data Structures & Algorithms Using Java: The Ultimate Guide to Programming Author: Hemant Jain Length: 436 pages Edition: First Edition Language: English Publisher: ...
在提供的"bubble-sorting-algorithm-main"文件中,可能包含了冒泡排序算法的实现代码,可能有多种编程语言版本,如Python、C++、Java等。这些代码可以帮助我们更深入地理解冒泡排序的逻辑,并且可以作为学习和教学的...
例如,`Algorithm-java-master`目录下可能包含各个算法的实现文件,如`Sorting`目录下的`QuickSort.java`、`MergeSort.java`等,以及`Graph`目录下的`DijkstraShortestPath.java`、`FloydWarshall.java`等。...
Java中的Sorting Algorithms基准测试应用程序 该算法基准测试程序使用最大为10,000的整数的测试输入条件来测量算法的时间复杂度。 虽然不是非常面向对象,但我们由于将其构建为一个整体式代码块而获得了额外的荣誉...
package sorting func quickSort(array []int) []int { doSort(array, 0, len(array)-1) return array } func doSort(array []int, i int, j int) { if i < j { // 找基准 index := partition(array, i, j) ...
org.algorithm.sorting // 排序算法 │ 拼命更新!顶!d=====( ̄▽ ̄*)b 三、最后推荐 :分享学习可落地的技术博文 :Follow 下呗 :Follow 下呗 :如果您有什么问题,可以去这里发帖 四、我的公号 关注微信公众号,...
本资源“Java-common-sorting-algorithm-.zip”提供了一份关于Java编程的PDF文档,详细介绍了五类常用的排序算法以及二分法查找,这些都是Java程序员必须掌握的基本技能。 首先,我们来详细探讨这五种排序算法: 1...
Java排序算法 分支 管道 代码覆盖率 掌握 开发者 这是需要更多关注的古老项目。 算法需要重构,将来会完成。 请不要将其作为最终版本。...An algorithm is a set of instructions designed to perform a
算法-Java 注意:为此仓库创建了一个分支,在该分支中,我们试图将现有项目迁移到Java项目结构。 您可以切换到分支进行贡献。 请参考以获取更多信息。 您可以单击免费的在线开发环境Gitpod.io来运行和编辑算法或对...
排序算法可视化工具 描述 除了康威的生活游戏外,算法可视化器也是我一直想要创建的东西。 对于这个项目,我决定创建/可视化五个最常见的排序算法(气泡排序,插入排序,合并排序,快速排序和选择排序)。...
初级java笔试题排序算法分析 排序元组 七种排序算法的简单分析 介绍 1998 年,唐纳德·克努斯 (Donald Knuth) 在他的《计算机编程艺术》第 3 卷:排序和搜索中表示,“编程的几乎每个重要方面都出现在排序或搜索的...
一个简单的程序,用于可视化用Java编写的排序算法,并使用Swing进行图形处理 生成并运行 git clone https://github.com/Hopson97/Sort-Algorithm-Visualiser.git cd Sort-Algorithm-Visualiser gradle run 特征 ...
《Java语言程序设计》的网上补充材料。 Part I -- General Supplements 1 Glossary 2 Installing and Configuring JDK ... 2 Sorting Algorithm Video http://www.youtube.com/watch?v=INHF_5RIxTE
We learn how to write an algorithm, what asymptotic analysis and notation are and the definition of a greedy algorithm. We learn how data structures and algorithms mesh together, the different ...