[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 ...
随机推荐
- ES6 箭头函数下的this指向和普通函数的this对比
首先在网上摘抄借鉴了一段代码, 然后再这段代码里面进行分析,通过比较ES6的箭头函数和普通函数的this指指向, 分析其中的不同之处.下面就是代码片段var name = "window&q ...
- 从1KW条数据中筛选出1W条最大的数
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- mongodb - 查看数据库状态
> use test switched to db test > db.stats() { "db" : "test", #数据库名 "c ...
- 多校第九场Arithmetic Sequence题解
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5400 题意:给定等差数列的差值d1,d2.问长度为n的数列中有多少个满足条件的子序列,条件为子序列中 ...
- web前端--移动端适配总结
转自:https://segmentfault.com/a/1190000011586301 作者:Devinnn meta标签到底做了什么事情 做过移动端适配的小伙伴一定有遇到过这行代码: < ...
- 实验c语言不同类型的指针互用(不推荐只是学习用)
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, world\n"); ]; i ...
- location alias与root
网站的根目录是:/alidata/www/webtest [root@M webtest]# tree /alidata/www/ /alidata/www/ ├── abc.html └── web ...
- makefile之foreach函数
#$(foreach <var>,<list>,<text>) #把参数<list>中的单词逐一取出放到参数<var>所指定的变量中,然后再 ...
- poj Ping pong LA 4329 (树状数组统计数目)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 879 Descript ...
- linux学习笔记14--命令which和whereis
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. ...