Data Structure Notes

Chapter-1 Sorting Algorithm

  • **Selection Sorting: **
/*
* Selection Sort
*/
template<typename T>
void selectionSort(T arr[], int n) {
for (int i = 0;i < n;i++) {
int minIndex = i;
for (int j = i + 1;j < n;j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
swap(arr[i], arr[minIndex]);
}
} // From both ends to exchange the elements in original array, it's a better solution optimize the previous Selection Sort.
template<typename T>
void OptimizedselectionSort(T arr[], int n) { int left = 0, right = n - 1;
while (left < right) {
int minIndex = left;
int maxIndex = right; // In each rounds must assure arr[minIndex] <= arr[maxIndex]
if (arr[minIndex] > arr[maxIndex])
swap(arr[minIndex], arr[maxIndex]); //Traversing the array to choose the match positon.
for (int i = left + 1; i < right; i++)
if (arr[i] < arr[minIndex])
minIndex = i;
else if (arr[i] > arr[maxIndex])
maxIndex = i; swap(arr[left], arr[minIndex]);
swap(arr[right], arr[maxIndex]); left++;
right--;
} return;
}
  • **Bubble Sorting: **
/*
* BubbleSort
*/
template<typename T>
void BubbleSort(T arr[], int n) { bool swapped; do {
swapped = false;
for (int i = 1; i < n; i++)
if (arr[i - 1] > arr[i]) {
swap(arr[i - 1], arr[i]);
swapped = true; } // 优化, 每一趟Bubble Sort都将最大的元素放在了最后的位置
// 所以下一次排序, 最后的元素可以不再考虑
n--; } while (swapped);
} // 我们的第二版bubbleSort,使用newn进行优化
template<typename T>
void OptimizedBubbleSort(T arr[], int n) { int newn; // 使用newn进行优化 do {
newn = 0;
for (int i = 1; i < n; i++)
if (arr[i - 1] > arr[i]) {
swap(arr[i - 1], arr[i]); // 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
newn = i;
}
n = newn;
} while (newn > 0);
}
  • **Shell Sorting: **
template<typename T>
void shellSort(T arr[], int n) { // 计算 increment sequence: 1, 4, 13, 40, 121, 364, 1093...
int h = 1;
while (h < n / 3)
h = 3 * h + 1; while (h >= 1) { // h-sort the array
for (int i = h; i < n; i++) { // 对 arr[i], arr[i-h], arr[i-2*h], arr[i-3*h]... 使用插入排序
T e = arr[i];
int j;
for (j = i; j >= h && e < arr[j - h]; j -= h)
arr[j] = arr[j - h];
arr[j] = e;
} h /= 3;
}
}
  • **Insert Sorting: **对于近乎有序的数组可以降到$ O(n)$的时间复杂度。
template<typename T>
void BinaryInsertionSort(T arr[], int n) {
int i, j, low, high, mid;
for (i = 1;i < n;i++) {
T e = arr[i]; //Binary Searching in the ordered range of array.
low = 0; high = i - 1;
while (low<= high)
{
mid = (low + high) / 2;
if (arr[mid] > e) high = mid - 1;
else low = mid + 1;
}
//Moving elements.
for (j = i - 1;j >= high + 1;--j) {
arr[j + 1] = arr[j];
}
arr[high + 1] = e;
}
} template<typename T>
void OptimizedInsertionSort(T arr[], int n) {
for (int i = 1;i < n;i++) { // Find right position without exchange frequently.
T e = arr[i];
int j;
for (j = i;j > 0 && arr[j - 1] > e;j--) {
arr[j] = arr[j - 1];
}
arr[j] = e;
}
}
  • **Merge Sorting: **

    • Tips1:Merge Sort Optimize in nearly ordered array
    void __mergeSort(T arr[], int l, int r) {
    if (l >= r) return; int mid = (l + r) / 2; // variable 'mid' may overflow
    __mergeSort(arr, l, mid);
    __mergeSort(arr, mid+1, r);
    if(arr[mid] > arr[mid+1]) // optimize in nearly ordered array.
    __merge(arr, l, mid, r);
    }
    • Tips2:When the sorting range of array in a short length, using InsertSort replace MergeSort can be more faster.
     template<typename T>
    void __mergeSort(T arr[], int l, int r) {
    //if (l >= r) return;
    if (r - l <= 15) { // The '15' is a constant represent the minmum judge range.
    InsertionSort(arr, l, r);
    return;
    }
    int mid = (l + r) / 2; // variable 'mid' may overflow
    __mergeSort(arr, l, mid);
    __mergeSort(arr, mid+1, r);
    if(arr[mid] > arr[mid+1]) // optimize in nearly ordered array.
    __merge(arr, l, mid, r);
    }
  • Botton to Up Merge Sorting : The algorithm can be usd in the LinkedList . The original MergeSort may preform better than this algorithm in normal situation.

    • Standard
    template<typename T>
    void mergeSortBottonToUp(T arr[], int n) {
    for(int size = 1; size <= n; size += size)
    // In order to assure exist two sperate array, setting (i+size < n) not (i < n)
    for (int i = 0; i + size < n ; i += size + size) {
    // merge arr[i ... i+size-1] and arr[i+size ... i+2*size-1]
    // In order to assure latter array isn't overflow so use min(i + size + size - 1, n-1) to choosing a right part.
    __merge(arr, i, i + size - 1, min(i + size + size - 1, n-1));
    }
    }
    • Optimization
    template <typename T>
    void mergeSortBU2(T arr[], int n){ // 对于小规模数组, 使用插入排序
    for( int i = 0 ; i < n ; i += 16 )
    insertionSort(arr,i,min(i+15,n-1)); // 一次性申请aux空间, 并将这个辅助空间以参数形式传递给完成归并排序的各个子函数
    T* aux = new T[n];
    for( int sz = 16; sz <= n ; sz += sz )
    for( int i = 0 ; i < n - sz ; i += sz+sz )
    // 对于arr[mid] <= arr[mid+1]的情况,不进行merge
    // 对于近乎有序的数组非常有效,但是对于一般情况,有一定的性能损失
    if( arr[i+sz-1] > arr[i+sz] )
    __merge2(arr, aux, i, i+sz-1, min(i+sz+sz-1,n-1) );
    delete[] aux; // 使用C++, new出来的空间不要忘记释放掉:)
    }
  • QuickSort (Divide-and-Conquer Algorithm)

    • Partition

    • Insert Sort Optimization

    	// sort the range of [l ... r]
    template <typename T>
    void __quickSort(T arr[], int l, int r) {
    //if (l >= r) return;
    if (r - l <= 15) {
    OptimizedInsertionSort(arr, l, r);
    return;
    }
    int p = __partition(arr, l, r);
    __quickSort(arr, l, p - 1);
    __quickSort(arr, p + 1, r);
    }
    • Optimization in the face of nearly ordered array

      Compare to MergeSort, the Sorting Tree generate by Quick Sort is more unbalanced.The worst situation the effience of quick sort can be deteriorate to $O(n^2)$

      Tradinational Method using the left element to be demarcating element. In order to solving the problem, we select the demarcating element randomly.


    template

    int __partition(T arr[], int l, int r) {

      swap(arr[l], arr[rand() % (r - l + 1) + l]);  // Add this process to randomly choose demarcating element.
    T v = arr[l]; //arr[l+i ... j] < v;arr[j+1 ... i] > v
    int j = l;
    for (int i = l + 1;i <= r;i++) {
    if (arr[i] < v) {
    swap(arr[j + 1], arr[i]);
    j++;
    }
    } swap(arr[l], arr[j]);
    return j;

    }

    template

    void quickSort(T arr[], int n) {

    srand(time(NULL)); // The partial of randomly select.

    __quickSort(arr, 0, n - 1);

    }


    - **Optimization in the face of many repeating Numbers. (*Dual Qucik Sort*)**
    When face many repeating numbers, the speration of array may unbalanced. In this situation, Quick Sort can be degraded to $O(n^2)$. **Solution :** ```cpp template <typename T>
    int __partition2(T arr[], int l, int r) {
    swap(arr[l], arr[rand() % (r - l + 1) + l]); // Add this process to randomly choose demarcating element.
    T v = arr[l]; //arr[l+i ... j] < v; arr[j+1 ... i] > v
    int i = l + 1, j = r;
    while (true) {
    //From front to behind to find a even bigger number.
    //From behind to front to find a even smaller number.
    while (i <= r&& arr[i] < v) i++;
    while (j >= l + 1 && arr[j] > v) j--;
    if (i > j) break;
    swap(arr[i], arr[j]);
    i++;
    j--;
    } swap(arr[l], arr[j]); return j;
    }
    • Optimization in the face of many repeating Numbers. (Qucik Sort 3 Ways)
    template <typename T>
    void __quickSort3(T arr[], int l, int r) {
    //if (l >= r) return;
    if (r - l <= 15) {
    OptimizedInsertionSort(arr, l, r);
    return;
    } // partition
    swap(arr[l], arr[rand() % (r - l + 1) + l]);
    T v = arr[l]; int lt = l; //arr[l+1 ... lt] < v
    int gt = r + 1; //arr[gt ... r] > v
    int i = l + 1; //arr[lt+1 ... i] == v
    while (i < gt) {
    if (arr[i] < v) {
    swap(arr[i], arr[lt + 1]);
    lt++;
    i++;
    }
    else if(arr[i] > v) {
    swap(arr[i], arr[gt - 1]);
    gt--;
    }
    else {// arr[i] == v
    i++;
    }
    } swap(arr[l], arr[lt]); __quickSort3(arr, l, lt - 1);
    __quickSort3(arr, gt, r);
    } template <typename T>
    void quickSort(T arr[], int n) {
    srand(time(NULL)); // The partial of randomly select.
    __quickSort3(arr, 0, n - 1);
    }

Data-Structure-Notes的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  10. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

随机推荐

  1. Vue的Key属性,v-for和v-if,v-if/v-show,v-pre不渲染,v-once只渲染一次

    key属性为什么要加 key -- api 解释 key的特殊属性主要用在vue的虚拟dom算法,如果不适用key,vue会使用一种最大限度减少动态元素并且尽可能的尝试修复/再利用相同类型元素的算法. ...

  2. JavaScript的filter方法

    var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() ...

  3. 域名解析前面的前缀* @ www 分别代表什么

    www 是指域名前带 www的,以百度为例,就是 www.baidu.com@ 是指前面不带任何主机名的,以百度为例,就是 baidu.com* 是指泛解析,是指除已添加的解析记录以外的所有主机都以此 ...

  4. Mstar 平台(648)唤醒之串口唤醒

    串口唤醒功能主要是从supernova 待机进入PM后,串口接收PC端口发送过来的特定字串,然后将主板唤醒的功能.与IR,KEYPAD,WOL,CEC,MHL 等等基本流程一致,触发源不一样而已. 待 ...

  5. shell 备份mysql

    shell脚本备份mysql,放在crontab中,可以作为每日测试用数据库备份 #!/bin/bash string_time=`date +%Y%m%d%H%M`; file_path=`date ...

  6. visual studio制作代码片段

    使用 Visual Studio 的代码片段功能,我们可以快速根据已有模板创建出大量常用的代码出来.ReSharper 已经自带了一份非常好用的代码片段工具,不过使用 ReSharper 创建出来的代 ...

  7. 一份ChatBot开源工程介绍(H5 + WX + KOA)

    vue-mpvue-ChatRobot https://github.com/fanqingsong/vue-mpvue-ChatRobot 前端 : Vue + Mpvue(支持移动端与小程序) ; ...

  8. c++内存管理5-虚拟内存4区结构图

    我们常说的32位系统为每个进程分配4G虚拟内存空间(而MMU负责把这些个4G虚拟内存映射到实际内存条的物理内存),其实只有0~3G才是真正完全属于进程本身,是我们所说的用户区:3~4G这1G是所有进程 ...

  9. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器功能简介---视频直播、直播鉴权(如何完美将EasyDSS过渡到新版)

    作为RTMP流媒体服务器,接受RTMP推流.进行实时的直播流分发是EasyDSS流媒体服务自身一大核心功能.写本篇博文的一个目的是向大家介绍EasyDSS新版的直播间.匿名直播.和虚拟直播的功能, 另 ...

  10. [Python] 项目的配置覆盖与合并

    参考来源: https://www.liaoxuefeng.com/wiki/1016959663602400/1018490750237280 代码稍微修改了一下 import os import ...