第 41 天: 顺序查找与折半查找

  1. 顺序查找使用岗哨可以节约一半的时间. 为此, 第 0 个位置不可以放有意义的数据, 即有效数据只有 length - 1 个.
  2. 顺序查找时间复杂度为 O ( n ) O(n)O(n).
  3. 折半查找时间复杂度为 O ( log ⁡ n ) O(\log n)O(logn).
  4. 书上为简化起见, 只关注键. 这里使用键值对来表示一条完整的数据. 实际应用中可以把 content 改成任何想要的数据类型
package datastructure.search;/*** Data array for searching and sorting algorithms.* * @author Fan Min minfanphd@163.com.*/
public class DataArray {/*** An inner class for data nodes. The text book usually use an int value to* represent the data. I would like to use a key-value pair instead.*/class DataNode {/*** The key.*/int key;/*** The data content.*/String content;/************************ The first constructor.**********************/DataNode(int paraKey, String paraContent) {key = paraKey;content = paraContent;}// Of the second constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {return "(" + key + ", " + content + ") ";}// Of toString}// Of class DataNode/*** The data array.*/DataNode[] data;/*** The length of the data array.*/int length;/************************ The first constructor.* * @param paraKeyArray     The array of the keys.* @param paraContentArray The array of contents.**********************/public DataArray(int[] paraKeyArray, String[] paraContentArray) {length = paraKeyArray.length;data = new DataNode[length];for (int i = 0; i < length; i++) {data[i] = new DataNode(paraKeyArray[i], paraContentArray[i]);} // Of for i}// Of the first constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "I am a data array with " + length + " items.\r\n";for (int i = 0; i < length; i++) {resultString += data[i] + " ";} // Of for ireturn resultString;}// Of toString/************************ Sequential search. Attention: It is assume that the index 0 is NOT used.* * @param paraKey The given key.* @return The content of the key.**********************/public String sequentialSearch(int paraKey) {data[0].key = paraKey;int i;// Note that we do not judge i >= 0 since data[0].key = paraKey.// In this way the runtime is saved about 1/2.for (i = length - 1; data[i].key != paraKey; i--);return data[i].content;}// Of sequentialSearch/************************ Test the method.**********************/public static void sequentialSearchTest() {int[] tempUnsortedKeys = { -1, 5, 3, 6, 10, 7, 1, 9 };String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);System.out.println("Search result of 10 is: " + tempDataArray.sequentialSearch(10));System.out.println("Search result of 5 is: " + tempDataArray.sequentialSearch(5));System.out.println("Search result of 4 is: " + tempDataArray.sequentialSearch(4));}// Of sequentialSearchTest/************************ Binary search. Attention: It is assume that keys are sorted in ascending* order.* * @param paraKey The given key.* @return The content of the key.**********************/public String binarySearch(int paraKey) {int tempLeft = 0;int tempRight = length - 1;int tempMiddle = (tempLeft + tempRight) / 2;while (tempLeft <= tempRight) {tempMiddle = (tempLeft + tempRight) / 2;if (data[tempMiddle].key == paraKey) {return data[tempMiddle].content;} else if (data[tempMiddle].key <= paraKey) {tempLeft = tempMiddle + 1;} else {tempRight = tempMiddle - 1;}} // Of while// Not found.return "null";}// Of binarySearch/************************ Test the method.**********************/public static void binarySearchTest() {int[] tempSortedKeys = { 1, 3, 5, 6, 7, 9, 10 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempSortedKeys, tempContents);System.out.println(tempDataArray);System.out.println("Search result of 10 is: " + tempDataArray.binarySearch(10));System.out.println("Search result of 5 is: " + tempDataArray.binarySearch(5));System.out.println("Search result of 4 is: " + tempDataArray.binarySearch(4));}// Of binarySearchTest/************************ The entrance of the program.* * @param args Not used now.**********************/public static void main(String args[]) {System.out.println("\r\n-------sequentialSearchTest-------");sequentialSearchTest();System.out.println("\r\n-------binarySearchTest-------");binarySearchTest();}// Of main}// Of class DataArray

运行结果:

第42天:哈希表

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

 /************************ The second constructor. For Hash code only. It is assumed that* paraKeyArray.length <= paraLength.* * @param paraKeyArray     The array of the keys.* @param paraContentArray The array of contents.* @param paraLength       The space for the Hash table.**********************/public DataArray(int[] paraKeyArray, String[] paraContentArray, int paraLength) {// Step 1. Initialize.length = paraLength;data = new DataNode[length];for (int i = 0; i < length; i++) {data[i] = null;} // Of for i// Step 2. Fill the data.int tempPosition;for (int i = 0; i < paraKeyArray.length; i++) {// Hash.tempPosition = paraKeyArray[i] % paraLength;// Find an empty positionwhile (data[tempPosition] != null) {tempPosition = (tempPosition + 1) % paraLength;System.out.println("Collision, move forward for key " + paraKeyArray[i]);} // Of whiledata[tempPosition] = new DataNode(paraKeyArray[i], paraContentArray[i]);} // Of for i}// Of the second constructor/************************ Hash search.* * @param paraKey The given key.* @return The content of the key.**********************/public String hashSearch(int paraKey) {int tempPosition = paraKey % length;while (data[tempPosition] != null) {if (data[tempPosition].key == paraKey) {return data[tempPosition].content;} // Of ifSystem.out.println("Not this one for " + paraKey);tempPosition = (tempPosition + 1) % length;} // Of whilereturn "null";}// Of hashSearch/************************ Test the method.**********************/public static void hashSearchTest() {int[] tempUnsortedKeys = { 16, 33, 38, 69, 57, 95, 86 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents, 19);System.out.println(tempDataArray);System.out.println("Search result of 95 is: " + tempDataArray.hashSearch(95));System.out.println("Search result of 38 is: " + tempDataArray.hashSearch(38));System.out.println("Search result of 57 is: " + tempDataArray.hashSearch(57));System.out.println("Search result of 4 is: " + tempDataArray.hashSearch(4));}// Of hashSearchTest/************************ The entrance of the program.* * @param args Not used now.**********************/public static void main(String args[]) {System.out.println("\r\n-------sequentialSearchTest-------");sequentialSearchTest();System.out.println("\r\n-------binarySearchTest-------");binarySearchTest();System.out.println("\r\n-------hashSearchTest-------");hashSearchTest();}// Of main

第 43 天: 插入排序

  1. 插入排序是简单直接的排序方式之一. 代码非常短.
  2. 每次保证前 i 个数据是有序的.
  3. 先做简单的事情 (第 1 轮最多有 1 次移动), 再做麻烦的事情 (最后一轮最多有 n − 1 n - 1n−1 次移动).
  4. 下标 0 的数据为岗哨, 与 41 天内容同理. 比其它排序方式多用一个空间.
  5. 又见 this.
  6. tempNode 只分配了引用 (指针) 的空间, 并未 new.
        /************************ Insertion sort. data[0] does not store a valid data. data[0].key should* be smaller than any valid key.**********************/public void insertionSort() {DataNode tempNode;int j;for (int i = 2; i < length; i++) {tempNode = data[i];//Find the position to insert.//At the same time, move other nodes.for (j = i - 1; data[j].key > tempNode.key; j--) {data[j + 1] = data[j];} // Of for j//Insert.data[j + 1] = tempNode;System.out.println("Round " + (i - 1));System.out.println(this);} // Of for i}// Of insertionSort/************************ Test the method.**********************/public static void insertionSortTest() {int[] tempUnsortedKeys = { -100, 5, 3, 6, 10, 7, 1, 9 };String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.insertionSort();System.out.println("Result\r\n" + tempDataArray);}// Of insertionSortTest
    

第 44 天: 希尔排序

希尔排序(Shell’s Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至 1 时,整个文件恰被分成一组,算法便终止。

 /************************ Shell sort. We do not use sentries here because too many of them are needed.**********************/public void shellSort() {DataNode tempNode;int[] tempJumpArray = { 5, 3, 1 };int tempJump;int p;for (int i = 0; i < tempJumpArray.length; i++) {tempJump = tempJumpArray[i];for (int j = 0; j < tempJump; j++) {for (int k = j + tempJump; k < length; k += tempJump) {tempNode = data[k];// Find the position to insert.// At the same time, move other nodes.for (p = k - tempJump; p >= 0; p -= tempJump) {if (data[p].key > tempNode.key) {data[p + tempJump] = data[p];} else {break;} // Of if} // Of for p// Insert.data[p + tempJump] = tempNode;} // Of for k} // Of for jSystem.out.println("Round " + i);System.out.println(this);} // Of for i}// Of shellSort/************************ Test the method.**********************/public static void shellSortTest() {int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9, 12, 8, 4 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while", "throw", "until", "do" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.shellSort();System.out.println("Result\r\n" + tempDataArray);}// Of shellSortTest

排序结果

第 45 天: 冒泡排序

 /************************ Bubble sort.**********************/public void bubbleSort() {boolean tempSwapped;DataNode tempNode;for (int i = length - 1; i > 1; i--) {tempSwapped = false;for (int j = 0; j < i; j++) {if (data[j].key > data[j + 1].key) {// Swap.tempNode = data[j + 1];data[j + 1] = data[j];data[j] = tempNode;tempSwapped = true;} // Of if} // Of for j// No swap in this round. The data are already sorted.if (!tempSwapped) {System.out.println("Premature");break;} // Of ifSystem.out.println("Round " + (length - i));System.out.println(this);} // Of for i}// Of bubbleSort/************************ Test the method.**********************/public static void bubbleSortTest() {int[] tempUnsortedKeys = { 1, 3, 6, 10, 7, 5, 9 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.bubbleSort();System.out.println("Result\r\n" + tempDataArray);}// Of bubbleSortTest

运行结果:

第 46 天: 快速排序

快速排序,就是给基准数据找其正确索引位置的过程

 /************************ Quick sort recursively.* * @param paraStart The start index.* @param paraEnd   The end index.**********************/public void quickSortRecursive(int paraStart, int paraEnd) {// Nothing to sort.if (paraStart >= paraEnd) {return;} // Of ifint tempPivot = data[paraEnd].key;DataNode tempNodeForSwap;int tempLeft = paraStart;int tempRight = paraEnd - 1;// Find the position for the pivot.// At the same time move smaller elements to the left and bigger one to the// right.while (true) {while ((data[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {tempLeft++;} // Of whilewhile ((data[tempRight].key > tempPivot) && (tempLeft < tempRight)) {tempRight--;} // Of whileif (tempLeft < tempRight) {// Swap.System.out.println("Swapping " + tempLeft + " and " + tempRight);tempNodeForSwap = data[tempLeft];data[tempLeft] = data[tempRight];data[tempRight] = tempNodeForSwap;} else {break;} // Of if} // Of while// Swapif (data[tempLeft].key > tempPivot) {tempNodeForSwap = data[paraEnd];data[paraEnd] = data[tempLeft];data[tempLeft] = tempNodeForSwap;} else {tempLeft++;} // Of ifSystem.out.print("From " + paraStart + " to " + paraEnd + ": ");System.out.println(this);quickSortRecursive(paraStart, tempLeft - 1);quickSortRecursive(tempLeft + 1, paraEnd);}// Of quickSortRecursive/************************ Quick sort.**********************/public void quickSort() {quickSortRecursive(0, length - 1);}// Of quickSort/************************ Test the method.**********************/public static void quickSortTest() {int[] tempUnsortedKeys = { 1, 3, 12, 10, 5, 7, 9 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.quickSort();System.out.println("Result\r\n" + tempDataArray);}// Of quickSortTest

第 47 天: 选择排序

 /************************ Selection sort. All data are valid.**********************/public void selectionSort() {DataNode tempNode;int tempIndexForSmallest;for (int i = 0; i < length - 1; i++) {// Initialize.tempNode = data[i];tempIndexForSmallest = i;for (int j = i + 1; j < length; j++) {if (data[j].key < tempNode.key) {tempNode = data[j];tempIndexForSmallest = j;} // Of if} // Of for j// Change the selected one with the current one.data[tempIndexForSmallest] = data[i];data[i] = tempNode;} // Of for i}// Of selectionSort/************************ Test the method.**********************/public static void selectionSortTest() {int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.selectionSort();System.out.println("Result\r\n" + tempDataArray);}// Of selectionSortTest

运行结果

第 48 天: 堆排序
堆排序可能是排序算法中最难的. 用到了二叉树.
建初始堆比较费劲.
调整堆的时间复杂度为 O ( log ⁡ n ) O(\log n)O(logn), 所以总体时间复杂度只有 O ( n log ⁡ n ) O(n \log n)O(nlogn).
空间复杂度只有 O ( 1 ) O(1)O(1).

 /************************ Heap sort. Maybe the most difficult sorting algorithm.**********************/public void heapSort() {DataNode tempNode;// Step 1. Construct the initial heap.for (int i = length / 2 - 1; i >= 0; i--) {adjustHeap(i, length);} // Of for iSystem.out.println("The initial heap: " + this + "\r\n");// Step 2. Swap and reconstruct.for (int i = length - 1; i > 0; i--) {tempNode = data[0];data[0] = data[i];data[i] = tempNode;adjustHeap(0, i);System.out.println("Round " + (length - i) + ": " + this);} // Of for i}// Of heapSort/************************ Adjust the heap.* * @param paraStart  The start of the index.* @param paraLength The length of the adjusted sequence.**********************/public void adjustHeap(int paraStart, int paraLength) {DataNode tempNode = data[paraStart];int tempParent = paraStart;int tempKey = data[paraStart].key;for (int tempChild = paraStart * 2 + 1; tempChild < paraLength; tempChild = tempChild * 2 + 1) {// The right child is bigger.if (tempChild + 1 < paraLength) {if (data[tempChild].key < data[tempChild + 1].key) {tempChild++;} // Of if} // Of ifSystem.out.println("The parent position is " + tempParent + " and the child is " + tempChild);if (tempKey < data[tempChild].key) {// The child is bigger.data[tempParent] = data[tempChild];System.out.println("Move " + data[tempChild].key + " to position " + tempParent);tempParent = tempChild;} else {break;} // Of if} // Of for tempChilddata[tempParent] = tempNode;System.out.println("Adjust " + paraStart + " to " + paraLength + ": " + this);}// Of adjustHeap/************************ Test the method.**********************/public static void heapSortTest() {int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.heapSort();System.out.println("Result\r\n" + tempDataArray);}// Of heapSortTest

第 49 天: 归并排序
log ⁡ n \log nlogn 轮, 每轮 O ( n ) O(n)O(n) 次拷贝. 因此时间复杂度为 O ( n log ⁡ n ) O(n \log n)O(nlogn).
空间复杂度为 O ( n ) O(n)O(n). 只需要一行辅助空间.
全都是在拷贝引用, 而不是数据本身. 这是 Java 的特性.
里面的两重循环总共只有 O ( n ) O(n)O(n). 这里是分成了若干个小组.
归并两个有序小组的时候, 用了三个并列的循环.
涉及分组后尾巴的各种情况, 所以需要相应的 if 语句

 /************************ Merge sort. Results are stored in the member variable data.**********************/public void mergeSort() {// Step 1. Allocate space.int tempRow; // The current rowint tempGroups; // Number of groupsint tempActualRow; // Only 0 or 1int tempNextRow = 0;int tempGroupNumber;int tempFirstStart, tempSecondStart, tempSecondEnd;int tempFirstIndex, tempSecondIndex;int tempNumCopied;for (int i = 0; i < length; i++) {System.out.print(data[i]);} // Of for iSystem.out.println();DataNode[][] tempMatrix = new DataNode[2][length];// Step 2. Copy data.for (int i = 0; i < length; i++) {tempMatrix[0][i] = data[i];} // Of for i// Step 3. Merge. log n roundstempRow = -1;for (int tempSize = 1; tempSize <= length; tempSize *= 2) {// Reuse the space of the two rows.tempRow++;System.out.println("Current row = " + tempRow);tempActualRow = tempRow % 2;tempNextRow = (tempRow + 1) % 2;tempGroups = length / (tempSize * 2);if (length % (tempSize * 2) != 0) {tempGroups++;} // Of ifSystem.out.println("tempSize = " + tempSize + ", numGroups = " + tempGroups);for (tempGroupNumber = 0; tempGroupNumber < tempGroups; tempGroupNumber++) {tempFirstStart = tempGroupNumber * tempSize * 2;tempSecondStart = tempGroupNumber * tempSize * 2 + tempSize;if (tempSecondStart > length - 1) {// Copy the first part.for (int i = tempFirstStart; i < length; i++) {tempMatrix[tempNextRow][i] = tempMatrix[tempActualRow][i];} // Of for icontinue;} // Of iftempSecondEnd = tempGroupNumber * tempSize * 2 + tempSize * 2 - 1;if (tempSecondEnd > length - 1) {tempSecondEnd = length - 1;} // Of ifSystem.out.println("Trying to merge [" + tempFirstStart + ", " + (tempSecondStart - 1)+ "] with [" + tempSecondStart + ", " + tempSecondEnd + "]");tempFirstIndex = tempFirstStart;tempSecondIndex = tempSecondStart;tempNumCopied = 0;while ((tempFirstIndex <= tempSecondStart - 1)&& (tempSecondIndex <= tempSecondEnd)) {if (tempMatrix[tempActualRow][tempFirstIndex].key <= tempMatrix[tempActualRow][tempSecondIndex].key) {tempMatrix[tempNextRow][tempFirstStart+ tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];tempFirstIndex++;System.out.println("copying " + tempMatrix[tempActualRow][tempFirstIndex]);} else {tempMatrix[tempNextRow][tempFirstStart+ tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];System.out.println("copying " + tempMatrix[tempActualRow][tempSecondIndex]);tempSecondIndex++;} // Of iftempNumCopied++;} // Of whilewhile (tempFirstIndex <= tempSecondStart - 1) {tempMatrix[tempNextRow][tempFirstStart+ tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];tempFirstIndex++;tempNumCopied++;} // Of whilewhile (tempSecondIndex <= tempSecondEnd) {tempMatrix[tempNextRow][tempFirstStart+ tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];tempSecondIndex++;tempNumCopied++;} // Of while} // Of for groupNumberSystem.out.println("Round " + tempRow);for (int i = 0; i < length; i++) {System.out.print(tempMatrix[tempNextRow][i] + " ");} // Of for jSystem.out.println();} // Of for tempStepSizedata = tempMatrix[tempNextRow];}// Of mergeSort/************************ Test the method.**********************/public static void mergeSortTest() {int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);System.out.println(tempDataArray);tempDataArray.mergeSort();System.out.println(tempDataArray);}// Of mergeSortTest

50:结论

1.顺序查找
顺序查找,按照字面意思,就是按照排列顺序,依次将查找关键词key与元数据每一项进行对比,如果找到就结束。 适用所有数据,但是对于过大的数据,不太实用,时间复杂度较高 ,时间复杂度O(n),空间复杂度O(1)
2.折半查找
假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
适用范围数据有序,时间复杂度O(log n),空间复杂度O(1)
3.哈希表查找
根据键值方式(Key value)进行查找,通过散列函数,定位数据元素。首先需要建立hash表。时间复杂度在不冲突的情况下O(1),最极端情况为O(n),空间复杂度O(1)
直接插入排序:
每次取一个记录插入到已经排好序的有序表中,得到一个新的有序表。在插入过程中为了防止下标出界,需要在r[0]处加入一个监视哨。该算法的时间复杂度为O(n2),双层嵌套循环;空间复杂度为O(1),因为只需要一个额外的空间存储监视哨。
希尔排序:
希尔排序是由直接插入排序发展而来,将待排序列分割成若干个子序列,并分别进行直接插入排序,(子序列是由相隔某个增量的记录组成的,而非分段),最后进行一次直接插入排序。注意:增量序列的值应该没有除1以外的公因子,且最后一个增量值必须为1.该算法的时间复杂度小于O(n2),与选取的增量值相关。
冒泡排序:
每一轮排序都从第一位开始将相邻的两位进行比较,如果t-1位比t位的值大,则将其交换顺序。每轮排序后,都会将未排序序列的最大数移到最后一位。当开始时序列即为从小到大有序序列,只需要一次就可以完成排序,时间复杂度为O(n); 当序列为逆序排列时,所需排序次数为n-1次,每次比较的次数为n-i次,时间复杂度为O(n2);算法的平均时间复杂度为O(n2)
快速排序:
第一趟快排将列表分为两部分,一部分的关键字均比另一部分关键字小,然后再分别对两部分进行排序。算法首先选定一点作为枢纽,然后分别设置两个指针low和high,high从尾部开始,找到小于枢纽的点,将其与枢纽交换;然后low从头部开始找到大于枢纽的点将其与枢纽交换。依次排序后以枢纽为中心分为两部分,且左边均比枢纽小,右边均比枢纽大。快速排序的平均时间复杂度为所有内部排序中最优的,但是在序列有序的情况下,其性能不能达到O(n)。空间复杂度为O(logn),因为每次快排包括递归过程中的枢纽值,都需要存储在一个栈中。
归并排序:
是建立在归并操作上的一种有效,稳定的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。归并排序进行 log n 轮循环,每轮O(n) 次拷贝,因此时间复杂度为 O(nlog ⁡n)。
堆排序:
堆排序是利用堆(二叉树)这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。

第41-50天:查找与排序相关推荐

  1. 剑桥offer(41~50)

    41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...

  2. 数据结构实验之查找与排序

    查找与排序 顺序查找 冒泡排序与折半查找 简单选择排序 直接插入排序 快速排序 顺序查找 一:顺序查找 顺序查找:从表的一端开始,依次将记录的关键字和给定值进行比较,若某个记录的关键字和给定值相等,则 ...

  3. 查找和排序算法的学生成绩分析实验

    基于查找和排序算法的学生成绩分析实验 一.实验内容 二.实验原理 三.实验代码记录 四.实验结果 一.实验内容 编写程序将自己学号后面的8位同学的学号.姓名以及数学.英语和数据结构的成绩信息保存到学生 ...

  4. BST插入(建立)、删除、查找和排序

    实验要求: 设计BST 的左右链存储结构,并实现BST插入(建立).删除.查找和排序算法. 实现折半查找算法. 实验比较:设计并产生实验测试数据,考察比较两种查找方法的时间性能,并与理论结果进行比较. ...

  5. 数据结构——查找:折半查找、二叉查找(排序)树、平衡二叉树

    七大查找算法:https://www.cnblogs.com/zhang-qc/p/8745153.html 学习的地址 https://www.bilibili.com/video/av278314 ...

  6. Python__数据结构与算法——查找与排序

    查找和排序是最基本的算法,在很多脚本中都会用到查找和排序.尽管 Python 提供的用于查找和排序的函数能够满足绝大多数需求,但还是有必要了解最基本的查找和排序算法,以便在有特殊需求的情况下,可以自己 ...

  7. 实验四 查找和排序算法实现

    广州大学学生实验报告 开课实验室:计算机科学与工程实验(电子楼418A) 学院 计算机科学与网络工程学院 实验课程 数据结构实验 实验项目 实验四 查找和排序算法实现 一.实验目的: 1.各种排序算法 ...

  8. 时间排序_你懂使用C ++ STL在线性时间内查找未排序数组的中位数吗

    也许你现在还在愁着学习C++?您不知道该怎么使用C++ST在线性时间内查找未排序数组的中位数,那么本文就用几组代码教您查找. 给定具有N个元素的未排序数组 arr [],任务是找出线性时间复杂度的数组 ...

  9. 数据结构与算法--查找与排序另类用法-旋转数组中的最小数字

    查找与排序 查找 查找与排序都在程序设计中常被用到的算法.查找相对而言简单,一般都是顺序查找,二分查找,哈希表查找,和二叉排序树查找.其中二分查找是我必须熟悉的一种. 哈希表和二叉排序树主要点在于他的 ...

最新文章

  1. OUYA游戏开发快速入门教程第1章了解OUYA及其设备
  2. 这家剑桥校友创办的苏州AI独角兽,再获4.1亿投资,将在国内IPO
  3. win10如何下载适合自己python版本的pygame?
  4. joa-framework 工作流高速开发框架(jeecg官方工作流版本号) 公布
  5. [文章备份]源代码制图工具Understand最新可用注册码
  6. php ftp 创建文件夹失败,phpftp上传多个文件时失败
  7. 图像处理之图像特征匹配
  8. sharepoint2010问卷调查(3)-实现问卷的开始和结束时间(采用自定义字段类型)...
  9. Java多线程(线程同步)
  10. CGAL4.4+VC2008编译
  11. 从零开始学前端:jQuery官网 --- 今天你学习了吗?(CSS:Day26)
  12. SpringSecurity 跨域问题 Cors
  13. DOTNET零碎要点---vb.net获取combox的选中值,删除Datagridview,选中值,处理提示框...
  14. 有100个GMAIL的邀请,需要的来信就可以啦。
  15. php安全篇值过滤用户输入的人参数
  16. HDU6135 拓展KMP模板
  17. 马尔科夫随机场 matlab,matlab马尔可夫随机场
  18. KVM地址翻译流程及EPT页表的建立过程
  19. python基于神经机器翻译技术的翻译网站实现
  20. Tikhonov regularization 吉洪诺夫 正则化

热门文章

  1. CANoe回放.asc文件(保姆级教学)
  2. 民生银行牛新庄:单账户成本从2.2元降到8分,分布式架构重构银行IT
  3. c语言中0xof423什么意思,C语言-相关博文 - 电子工程世界-论坛
  4. 公积金提取 -- 小黑日常超详细教程
  5. 数字图像处理 博客目录索引
  6. 降噪音质好的耳机推荐,2021年降噪耳机排行!
  7. 【读书2】【2014】基于MATLAB的雷达信号处理基础(第二版)——雷达截面的复合模型(2)
  8. 华为笔记本14和14s的区别
  9. 在职考研复旦应用统计专业经验分享
  10. Excel如何合并多个列的数据并插入一些符号?