[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 ...
随机推荐
- asp.net 查询本地excel 获取信息
string filepath = @"D:\test1.xls"; string sheetname = "Sheet5"; ...
- C# 共享内存(转)
以下是一个C#操作内存的一个类,只要将下面的类添加到相应的项目中,该项目就可以对内存进行直接操作! using System.Runtime.InteropServices; //添加如下命名空 ...
- AAAA block
[self AAAA:^(BOOL isSuccessed, id userInfo, NSString *errorMsg) { NSLog(@"AAAA: %d, userInfo: % ...
- Solr3.6.2和Solr4.9.0经常使用配置
tomcat 以tomcat 7为例,位置/work/apache-tomcat-7.0.55 Solr 3.6.2 基本配置 Solr 3.6.2.须要JDK 6/JDK7支持. 下载Solr 3. ...
- python 特征缺失值填充
python数据预处理之缺失值简单处理:https://blog.csdn.net/Amy_mm/article/details/79799629 该博客总结比较详细,感谢博主. 我们在进行模型训练时 ...
- Asp.net2.0里的SessionPageStatePersister
备注: ASP.NET 页可在处理和提供任何网页所必需的原本无状态 HTTP 请求与响应之间存储 Page 状态信息.此状态称为“视图状态”. ASP.NET 的默认持久性机制是使用 HiddenFi ...
- 使用forever运行nodejs应用
使用forever运行nodejs应用 何为forever forever可以看做是一个nodejs的守护进程,能够启动,停止,重启我们的app应用. 官方的说明是说: A simple CLI to ...
- mapper.xml文件
1. 概述 mybatis的真正强大在于它的映射语句.由于它的异常强大,映射器的XML文件就显得相对简单,如果拿它跟具有相同功能的JDBC代码进行对比,省掉将近95%的代码.mybatis是针对SQL ...
- 王立平-- Swift
Swift是供iOS和OS X应用编程的新编程语言,基于C和Objective-C.而却没有C的一些兼容约束.Swift採用了安全的编程模式和加入现代的功能来是的编程更加简单.灵活和有趣. 界面则基于 ...
- 李洪强和你一起学习前端之(4)HTML5介绍
1.1认识HTML5 html的版本: html4 Xhtml1.0 目前: html5是最高的版本 再怎么变化,无非是多了一些标签而已,但是不单单是提供了一些标签 比如: 开发网页游戏 我们可以开发 ...