[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 ...
随机推荐
- 转 理解与分析ios应用的崩溃报告
理解与分析ios应用的崩溃报告 源网址: http://developer.apple.com/library/ios/#technotes/tn2151/_index.html 当一个应用程序崩溃时 ...
- MongoDB - 内嵌角色
数据库用户角色(每个数据库都有的角色) 角色名 说明 read 对non-system集合的读取权限,以及以下system集合的读取权限:system.indexes,system.js,syst ...
- Python -面向对象(一 基本概念)
一 Python简单介绍 Python是一个可移植的面向对象的脚本语言. Python尽管是一个脚本语言,但也是一个全然面向对象的语言.由于它设计之初把易用性做为很重要的一个考量标准,所以用起来很简洁 ...
- thinkphp验证码出不来
import("ORG.Util.Image"); //图像操作类库 ob_end_clean(); $type = isset($_GET['type'])?$_GET['typ ...
- firefox配置
Firefox23取消了一个很人性化的功能,就是在GUI界面中禁用JavaScript,对于这点我很不能理解!JavaScript是所有网页木马 的源头,防人之心不可无,FireFox就这么确信能防住 ...
- urllib3学习
urllib3.connectionpool.connection_from_url(url, **kw) Given a url, return an ConnectionPool instance ...
- 算法提高 道路和航路 SPFA 算法
我简单的描述一下题目,题目中所说的有道路和航路: 1.公路是双向的,航路是单向的: 2.公路是正值,航路可正可负: 每一条公路i或者航路i表示成连接城镇Ai(1<=A_i<=T)和Bi(1 ...
- jdbc preparedstatement 调用存储过程的问题
preparedstatement 是可以执行正常的存储过程 executeQuery() 正常执行 在实际开发中遇到一种问题当 preparedstatement.setMaxRows 设置了这 ...
- jdbc.properties
#privilege database privilege.jdbc.driverClassName=com.mysql.jdbc.Driver privilege.jdbc.url=jdbc\:my ...
- IPC之SystemV
svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...