[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 ...
随机推荐
- 【SpringMVC学习01】宏观上把握SpringMVC框架
springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...
- MySQL监控脚本
zabbix监控mysql时自定key用到的脚本 #!/usr/bin/env python #-*- coding: UTF-8 -*- from __future__ import print_f ...
- MooseFS基础和安装
一.MooseFS简介 1.介绍 MooseFS是一个具备冗余容错功能的分布式网络文件系统,它将数据分别存放在多个物理服务器单独磁盘或分区上,确保一份数据有多个备份副本.对于访问的客户端或者用户来说, ...
- Swift中UIView类方法(animateWithDuration)的使用
需求:利用Swift语言实现OC语言中UIView类方法 [UIView animateWithDuration:0.5 animations:^{ bgView.alpha= 1; }]; 在Swi ...
- linux内存排查工具valgrind
官网:http://valgrind.org/info/about.html 百科介绍:http://baike.baidu.com/link?url=ZdXzff0omzoPpE_yZUlNW9lJ ...
- 配置taglib的uri的2种方法
推荐方法1 1.把uri写在tld文件中,tld放在WEB-INF文件夹下,例如: <short-name>就对应了你在jsp中引用的时候<%@ taglib prefix=&quo ...
- 基于Java Mina框架的部标jt808服务器设计和开发
在开发部标GPS平台中,部标jt808GPS服务器是系统的核心关键,决定了部标平台的稳定性和行那个.Linux服务器是首选,为了跨平台,开发语言选择Java自不待言.需要购买jt808GPS服务器源码 ...
- ECMall 中URL体系的改造思路
EC系列的产品都已停止更新很久了,但其对中国中小电商企业的影响无疑是巨大的.很多公司,都是直接拿来即改,改了即用. 但他们都有个问题,代码是比较传统的开发模式过来的,尤其ecshop.ECMall系统 ...
- dubbo配置约束
此处主要记录dubbo配置的一些约束规则. 采用官网提供的原文,描述如下: 一.XML配置(官网原文) 以 timeout 为例: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费方优先 ...
- hive 字符集问题 报错 Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:For direct MetaStore DB connections,
学习hive 使用mysql作为元数据 hive创建数据库和切换数据库都是可以的 但是创建表就是出问题 百度之后发现 是编码问题 特别记录一下~~~ 1.报错前如图: 2.在mysql数据库中执行如 ...