几种常用的小算法
来源:互联网 发布:数据的维护与更新机制 编辑:程序博客网 时间:2024/06/05 06:09
#include <iostream>
using namespace std;
void choice_sort(int *arr, int len);
void quicksort(int *arr, int start, int pivot);
int main()
{
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3,2, 1};
quicksort(arr, 0, 9);
for(int i=0; i<10; i++) //不对称边界
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
void bubble_sort(int *arr, int len)
{
int temp;
for(int i=0; i<len-1; i++)
{
for(int j=0; j<len-1-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//插入排序想法是:一条if语句包含两种情况.
//每次求最小值,都要和数组所有其它的值进行一下比较.只能是这样做.
void insert_sort(int *arr, int len)
{
int temp;
int max;
//控制外层大循环的次数,不是吗?
for(int i=0; i<len-1; i++)
{
max = i;
for(int j= i+1; j<len; j++)
{
if(arr[max] > arr[j])
{
max = j;
}
}
if(max != i)
{
temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
}
}
}
void choice_sort(int *arr, int len)
{
//确定大循环次数,循环n-1次
int j=0;
int temp;
for(int i=0; i<len-1; i++)
{
j=i;
temp = arr[j+1];
while(j>=0 && temp<arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
int partition(int *arr, int start, int pivot)
{
int i= start;
int j = start;
int temp;
for(;j<pivot;j++)
{
if(arr[j]<arr[pivot])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
temp = arr[i];
arr[i] = arr[pivot];
arr[pivot] = temp;
return i; //..返回轴点位置
}
void quicksort(int *arr, int start, int pivot)
{
int q;
if(start<pivot)
{
q = partition(arr, start, pivot);
quicksort(arr, start, q-1);
quicksort(arr, q+1, pivot);
}
}
//二分查找
//每次将问题的规模减少一半.
int binary_search(int arr, int len, int target)
{
int top=0,bottom=len-1,mid=-1;
while(top<=bottom)
{
mid = (top+bottom)>>1;
if(mid > target)
{
bottom = mid-1;
} else if(mid < target)
{
top = mid+1;
}else
{
return mid;
}
}
return -1;
}
- 几种常用的小算法
- 几种常用的算法
- 几种常用的基本排序算法
- C#几种常用的排序算法
- C#几种常用的排序算法
- C#几种常用的排序算法:
- 几种常用的排序算法
- C#几种常用的排序算法
- 几种常用的算法简介
- 最短路的几种常用算法
- 几种常用的排序算法
- 分享几种常用算法的思想
- 几种常用的排序算法
- 几种常用算法的复杂度比较
- 几种常用的排序算法
- 几种常用的排序算法
- 几种常用的排序算法
- 常用的几种排序算法
- ISA Server Application Filter Object Model
- hibernate 传参数
- wince下USB总线驱动分析
- WM 获得手机信息
- 数据精度个数是从首个不为零的数开始计算
- 几种常用的小算法
- 博客已搬家到:http://www.cnblogs.com/netWild
- javascript 更具button 事件找到当前单元格中的控件并动态赋随机ID
- SQL2005 用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联
- vbs自动化提交工作项OA
- cmd /c和cmd /k
- trigger and cursor
- JNDI 在 J2EE 中的角色(转)
- WPF中的传递事件——出自《IT168》