[Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merge sort and quick sort. I then implement them in C++. All the function takes in a vector<int>& type and directly operates on the input. To use the following code, you need to add the following code to your headers.
#include <iostream>
#include <vector>
#include <ctime> // for randomization in quicksort using namespace std;
Insertion Sort:
// Insertion sort
void insertionSort(vector<int>& arr) {
for (int j = ; j < (signed)arr.size(); j++)
for (int i = j - ; i >= && arr[i] > arr[i + ]; i--)
swap(arr[i], arr[i + ]);
}
Bubble Sort:
// Bubble sort
void bubbleSort(vector<int>& arr) {
for (int i = ; i < (signed)arr.size() - ; i++)
for (int j = i + ; j < (signed)arr.size(); j++)
if (arr[i] > arr[j]) swap(arr[i], arr[j]);
}
Merge Sort:
// Merge sort
void merge(vector<int>& arr, int left, int mid, int right) {
if (left >= right) return;
vector<int> larr(arr.begin() + left, arr.begin() + mid + );
vector<int> rarr(arr.begin() + mid + , arr.begin() + right + );
int i = , j = , pos = left;
while(i < (signed)larr.size() && j < (signed)rarr.size()) {
if (larr[i] > rarr[j]) arr[pos++] = rarr[j++];
else arr[pos++] = larr[i++];
}
while (i < (signed)larr.size()) arr[pos++] = larr[i++];
while (j < (signed)rarr.size()) arr[pos++] = rarr[j++];
} void mergeSortHelper(vector<int>& arr, int left, int right) {
if (left >= right) return;
int mid = (left + right) / ;
mergeSortHelper(arr, left, mid);
mergeSortHelper(arr, mid + , right);
merge(arr, left, mid, right);
} void mergeSort(vector<int>& arr) {
mergeSortHelper(arr, , arr.size() - );
}
Quicksort:
// Quicksort
int partition(vector<int>& arr, int left, int right) {
// Introduce randomization
srand((unsigned)time());
int rndIdx = rand() % (right - left + ) + left;
swap(arr[rndIdx], arr[left]);
int l = left + , r = right;
while (l <= r) {
if (arr[l] > arr[left] && arr[r] < arr[left])
swap(arr[l], arr[r]);
if (arr[l] <= arr[left]) l++;
if (arr[r] >= arr[left]) r--;
}
swap(arr[left], arr[r]);
return r;
} void quickSortHelper(vector<int> &arr, int left, int right) {
if (left >= right) return;
int pivot = partition(arr, left, right);
quickSortHelper(arr, left, pivot - );
quickSortHelper(arr, pivot + , right);
} void quickSort(vector<int>& arr) {
quickSortHelper(arr, , arr.size() - );
}
Welcome for any question, comment and suggestion about the code!
[Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)的更多相关文章
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- Sort list by merge sort
使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript
Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...
- Insertion Sort and Merge Sort
Insertion Sort(插入排序) 思路:for 循环遍历数组中的每一个数 用while将每次遍历到的数于左侧的数进行对比,将小的排到左边 void InsertionSort(int*A, i ...
- Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...
- JavaScript 排序算法(JavaScript sorting algorithms)
JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- 873D. Merge Sort
Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a w ...
随机推荐
- [转]PHP运行出现Notice : Use of undefined constant 的完美解决方案
Notice: Use of undefined constant title - assumed 'title' in F:\wamp\www\load_myweb.php on line 22No ...
- javaweb项目运行时错误
1.Debug模式下进入ThreadPoolExecutor.class的解决方式 进入window-->preferences-->java-->debug-->去掉Susp ...
- CCRenderTexture画点出现十字架"歪解"
在泰然论坛发个了帖子,没有人解答,内容如下: 遇到奇葩问题了,cocos2dx 2.2 CCRenderTexture* tex=CCRenderTexture::create(, ); tex-&g ...
- ajax 传递参数中文乱码解决办法
/********Start***********/ /*获取地址栏参数*/ function getRequest(){ var url = location.search; //获取url中&qu ...
- jQuery 实战读书笔记之第六章:事件本质
理解浏览器事件模型 understandEventModel.html 代码: <!DOCTYPE HTML> <html> <head> <title> ...
- GraphicsMagick +im4java高并发处理大型网站图片工具-图片剪切、遮蔽、水印添加之环境搭建
环境: centos 6.5 GraphicsMagick 下载安装 准备环镜: 需要依赖zlib图片操作函数库 下载地址:http://www.zlib.net/ 编译安装 .tar.gz cd z ...
- php安装redis扩展初始化失败解决办法
错误信息如下: PHP Warning: PHP Startup: redis: Unable to initialize module Module compiled with module API ...
- linux系统中-E,-S,-c的区别和作用(怎么讲代码转化为机器识别的语言)
1707 许多初学者都有比较大的疑惑,电脑是怎么识别我们写的代码并进行处理的呢?其实这个问题对我们初学者来说是很重要的,只有了解机器的运行原理,我们才能真正地学号留下.那么今天我就以此为题为大家略讲一 ...
- Alluxio部署(集群模式)
下载(pre-build for Hadoop 2.7) http://www.alluxio.org/download` 解压 tar -xvf alluxio-1.3.0-hadoop2.7-bi ...
- centos7.2 安装 Elasticsearch5.2
打算上全文检索,就找到了找个产品,开始研究下…… 1.官网地址: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/install ...