# 几种常见算法 原

悠米海

``````#include <iostream>
using namespace std;

void paopao_sort(int arr[], int nSize)
{
for(int i=0;i<nSize;i++)
{
for(int j=0;j<nSize-1-i;j++)
{
if(arr[j] > arr[j+1])
{
int nTmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = nTmp;
}
}
}
}

void select_sort(int arr[], int nSize)
{
for(int i=0;i<nSize;i++)
{
for(int j=i;j<nSize;j++)
{
if(arr[i] > arr[j])
{
int nTmp = arr[i];
arr[i] = arr[j];
arr[j] = nTmp;
}
}
}
}

void insert_sort(int arr[], int nSize)
{
for(int i=0;i< nSize;i++)
{
int nTmp = arr[i];
int j = i;
for(;j>0 && arr[j-1] > nTmp;j--)
{
arr[j] = arr[j-1];
}
arr[j] = nTmp;
}
}

void quick_sort(int arr[], int low,int high)
{
if(low >= high)
return;
int first = low,last = high;
int key = arr[first];
while(first < last){
while(first < last && arr[last] >= key){
--last;
}
arr[first] = arr[last];
while(first < last && arr[first] <= key){
++first;
}
arr[last] = arr[first];
}
arr[first] = key;
quick_sort(arr, low, first -1);
quick_sort(arr, first+1, high);
}

void merge(int sour[], int tmp[], int st, int md, int ed)
{
if(st >= ed)
return;
int i=st,j=md+1,k=st;
while(i!=md+1 && j!=ed+1){
if(sour[i] > sour[j]){
tmp[k++] = sour[j++];
}else{
tmp[k++] = sour[i++];
}
}
while(i!=md+1){
tmp[k++] = sour[i++];
}
while(j!=ed+1){
tmp[k++] = sour[j++];
}
for(i=st;i<=ed;i++){
sour[i] = tmp[i];
}
}

void merge_sort(int sour[],int tmp[],int st, int ed){
if(st >= ed)
return;
int md = (st + ed)/2;
merge_sort(sour,tmp,st,md);
merge_sort(sour,tmp,md+1,ed);
merge(sour,tmp,st,md,ed);
}

int nChild, nTmp;
for(;i*2+1 < nLen;i = nChild){
nChild = i*2+1;
if(nChild < nLen - 1 && arr[nChild] < arr[nChild +1]){
nChild ++;
}
if(arr[nChild] > arr[i]){
nTmp = arr[nChild];
arr[nChild] = arr[i];
arr[i] = nTmp;
}else{
break;
}
}
}

void heap_sort(int arr[], int nLen)
{
for(int i=nLen/2-1;i>=0;i--){
}
int nTmp = 0;
for(int i=nLen-1;i>0;i--)
{
nTmp = arr[i];
arr[i] = arr[0];
arr[0] = nTmp;
}
}

void heapAdjustS(int arr[], int i, int nLen)
{
int nChild, nTmp;
for(;i*2+1 < nLen;i = nChild){
nChild = i*2+1;
if(nChild < nLen -1 && arr[nChild] > arr[nChild+1] ){
nChild ++;
}
if(arr[i] > arr[nChild]){
nTmp = arr[nChild];
arr[nChild] = arr[i];
arr[i] = nTmp;
}else{
break;
}
}
}

void heaps_sort(int arr[], int nLen)
{
for(int i=nLen/2-1;i>=0;i--){
}
int temp;
for(int i=nLen-1;i>0;i--)
{
temp=arr[i];
arr[i]=arr[0];
arr[0]=temp;
}
}

struct tree{
int		node;
tree*	left;
tree*	right;
};

{
if(root == NULL){
root = new tree;
root->node = key;
root->left = root->right = NULL;
return root;
}
if(root->node == key){
return root;
}else if(root->node > key){
}else{
}
}

void treeDel(tree* root, int arr[], int& index)
{
if(root == NULL)
return;
if(root->left != NULL){
treeDel(root->left, arr, index);
}
arr[index++] = root->node;
if(root->right != NULL){
treeDel(root->right, arr, index);
}
delete root;
}

void tree_sort(int arr[], int nLen)
{
tree* root = NULL;
for(int i=0;i<nLen;i++)
{
}
int nIndex = 0;
treeDel(root, arr, nIndex);
}

void arrCopy(int base[], int to[], int nLen)
{
for(int i=0;i<nLen;i++)
to[i] = base[i];
}

void arrShow(int arr[], int nLen)
{
for(int i=0;i<nLen;i++)
cout<<arr[i]<<",";
cout<<endl;
}

int main()
{
int base[] = {3,6,4,1,2,5,8,7,10,9};
int nLen = sizeof(base)/sizeof(int);
int* ptr = new int[nLen];

cout<<"原始数组："<<endl;
arrShow(base, nLen);

cout<<"冒泡："<<endl;
arrCopy(base, ptr, nLen);
paopao_sort(ptr, nLen);
arrShow(ptr, nLen);

cout<<"选择："<<endl;
arrCopy(base, ptr, nLen);
select_sort(ptr, nLen);
arrShow(ptr, nLen);

cout<<"插入："<<endl;
arrCopy(base, ptr, nLen);
insert_sort(ptr, nLen);
arrShow(ptr, nLen);

cout<<"快速："<<endl;
arrCopy(base, ptr, nLen);
quick_sort(ptr, 0, nLen-1);
arrShow(ptr, nLen);

cout<<"归并："<<endl;
arrCopy(base, ptr, nLen);
int* pTmp = new int[nLen];
merge_sort(ptr, pTmp, 0, nLen-1);
arrShow(ptr, nLen);
delete[] pTmp;

cout<<"最大堆："<<endl;
arrCopy(base, ptr, nLen);
heap_sort(ptr, nLen);
arrShow(ptr, nLen);

cout<<"最小堆："<<endl;
arrCopy(base, ptr, nLen);
heaps_sort(ptr, nLen);
arrShow(ptr, nLen);

cout<<"二叉树："<<endl;
arrCopy(base, ptr, nLen);
tree_sort(ptr, nLen);
arrShow(ptr, nLen);

delete[] ptr;
return 0;
}``````

### 悠米海

JVM有几种常见GC算法？常见的调节参数有哪些？

JVM有几种常见GC算法？常见的调节参数有哪些？ JVM调优或者trouble shoot的基本方法有哪些？

2、nsobjective和uiview的默认构造方法 3、block的工作原理，从内存来看可分哪几种 4、输入网址按下回车 5、消息队列 6、函数模板与模板函数 7、stdcall和cdecl 8、static的作用 9、常见内存...

Matrix4X4 ⋅ 2012/08/19 ⋅ 2

g2v13ah ⋅ 2017/11/02 ⋅ 0

Python中time模块和datetime模块的常用操作以及几种常用时间格式间的转换

OMCloud ⋅ 2017/07/14 ⋅ 0

PHP常见排序算法学习

moTzxx ⋅ 2017/10/27 ⋅ 0

sinat_23880167 ⋅ 2017/10/31 ⋅ 0

⋅ 2017/09/12 ⋅ 0

PHP常见排序算法整理学习

u011415782 ⋅ 2017/10/24 ⋅ 0

Java开发岗位面试题归类汇总

Mahout推荐算法之SlopOne

xiaomin0322 ⋅ 6分钟前 ⋅ 0

LVM讲解

LVM是什么 LVM是 Logical Volume Manager（逻辑卷管理）的简写，它是Linux环境下对磁盘分区进行管理的一种机制，Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地...

mysql更改密码、连接mysql、mysql常用命令

1. 更改mysql的root账户密码： mysql中root账户和系统root不是一个账户 1.1 更改环境变量PATH，增加mysql绝对路径 由于mysql安装目录为/usr/local/mysql/，所以系统不能直接使用mysql，需把/...

laoba ⋅ 17分钟前 ⋅ 0

1.03-Maven中使用ueditor富文本编辑器

1、BeanFactory 接口和 ApplicationContext 接口有什么区别？ ApplicationContext 接口继承BeanFactory接口 Spring核心工厂是BeanFactory BeanFactory采取延迟加载，第一次getBean时才会初始...

xml解析

GithubXD ⋅ 49分钟前 ⋅ 0

reuse stream

Although Java streams were designed to be operated only once, programmers still ask how to reuse a stream. From a simple web search, we can find many posts with this same issue ......

idoz ⋅ 49分钟前 ⋅ 0