一、排序

1.冒泡排序

 void BubbleSort(int array[],int n)
{
int i=;
int j=;
int temp=;
int flag = ;
for(i=;i<n - ;i++) /*外循环控制排序的总趟数*/
{
flag = ; /*本趟排序开始前,交换标志应为假*/
for(j=n-;j > i;j--) /*内循环控制一趟排序的进行*/
{
if(array[j] < array[j-] ) /*相邻元素进行比较,若逆序就交换*/
{
temp =array[j];
array[j] = array[j-];
array[j-] = temp;
flag = ; /*发生了交换,故将交换标志置为真*/
} }
if (flag == ) /*本趟排序未发生交换,提前终止算法*/
break;
}
}

冒泡排序--递归实现

  void SortByRecursion( int *array, int n )
{
int i;
if( == n)
{
return;
}
for(i = ; i < n - ; i++)
{
if(array[i] > array[i + ])
swap( &array[i], &array[i + ]);
}
SortByRecursion( array, n - );
}

2.插入排序

 //插入排序(非递归)
void InsertSort(int *pArr, int nLength)
{
if (pArr == NULL || nLength <= )
{
return;
} int key = ;
int j=;
for (int i=; i<nLength; i++)
{
if (pArr[i] < pArr[i-])//当前带插入的元素比有序序列的最后一个元素小
{
key = pArr[i];
for (j=i-; j>=&&key<pArr[j]; j--)
{
pArr[j+] = pArr[j];
}
pArr[j+] = key;
}
}
}

插入排序---递归实现

 //插入排序(递归)
void InsertSortRecursively(int *pArr, int index, int nLength)
{
if (index >= nLength)
{
return;
} int key = pArr[index];//记录当前待插入的元素
int i=;
for (i=index-; i>=&&key<pArr[i]; i--)
{
pArr[i+] = pArr[i];
}
pArr[i+] = key;
InsertSortRecursively(pArr, index+, nLength);
}

3.快速排序

 int PartSort(int arr[],int low, int high)
{
int key = arr[low];
while(low < high)
{
while(low < high && arr[high] >= key)
--high;
arr[low] = arr[high];
while(low < high && arr[low] <= key)
++low;
arr[high] = arr[low];
}
arr[low] = key;
return low;
}
void QuickSort(int arr[],int low, int high)
{
if(low < high)
{
int pos = PartSort(arr, low, high);
QuickSort(arr, pos+, high);
QuickSort(arr, low, pos-);
}
}

二、查找

1.折半查找

 int  HalfFind(int arr[], int count,int key)
{
int low = ;
int high = count - ;
while(low <= high)
{
int mid = (low + high)/;
if (arr[mid] == key)
{
return mid;
}
else if(arr[mid] > key)
{
high = mid - ;
}
else
{
low = mid + ;
}
}
}

C/C++ 排序&&查找算法(面试)的更多相关文章

  1. javascript排序 查找算法大全

    在pptv的实习结束了, 忙着找工作的事,顺便把数据结构的那本书重新复习了一遍.为了加深印象,特意把里面的常用的排序.查找算法用js写了一遍 具体的实例在我的github上,大家可以访问的: http ...

  2. 深入JDK源码之Arrays类中的排序查找算法(转)

    原文出处: 陶邦仁 binarySearch()方法 二分法查找算法,算法思想:当数据量很大适宜采用该方法.采用二分法查找时,数据需是排好序的. 基本思想:假设数据是按升序排序的,对于给定值x,从序列 ...

  3. Java常用的排序查找算法

    public static void main(String[] args) {      // bubbleSort(); // int[] a = {20,2,10,8,12,17,4,25,11 ...

  4. Java数据结构 遍历 排序 查找 算法实现

    请查看:http://blog.csdn.net/zhanghao_hulk/article/details/35372571#t13

  5. PHP的排序算法跟查找算法

    排序算法: (1)冒泡排序 $arr = array(15,8,20,50,37,85,10,5,11,4); //冒泡排序 function maoPao($arr){ for($i = 0; $i ...

  6. C# 基础排序与查找算法

    排序算法: class Sort { static void swap<T>(ref T a, ref T b) { T tmp = a; a = b; b = tmp; } #regio ...

  7. PHP数组基本排序算法和查找算法

    关于PHP中的基础算法,小结一下,也算是本博客的第一篇文章1.2种排序算法冒泡排序:例子:个人见解 5 6 2 3 7 9 第一趟 5 6 2 3 7 9 5 2 6 3 7 9 5 2 3 6 7 ...

  8. php 中的查找算法 和 排序方法(多字段排序)

    一.查找算法 1.顺序查找(一个一个查,效率低,不用多说) 2.二分查找 /* php 二分查找 在$a数组里查找$x的位置 $a必须是一个以升序排序后的数组 */ function binsearc ...

  9. python实现折半查找算法&&归并排序算法

    今天依旧是学算法,前几天在搞bbs项目,界面也很丑,评论功能好像也有BUG.现在不搞了,得学下算法和数据结构,笔试过不了,连面试的机会都没有…… 今天学了折半查找算法,折半查找是蛮简单的,但是归并排序 ...

随机推荐

  1. Java多线程编程详解

    转自:http://programming.iteye.com/blog/158568 线程的同步 由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Ja ...

  2. topcoder SRM 593 DIV2 RaiseThisBarn

    #include <vector> #include <string> #include <list> #include <map> #include ...

  3. JavaScript事件大全3

    //无模式的提示框 //屏蔽按键 <html> <head>    <meta http-equiv="Content-Type" content=& ...

  4. CreateFeatureClass COM异常

    private static IFeatureClass CreatStnShp(string shp) { //打开工作空间 IWorkspaceFactory wsfactory = new Sh ...

  5. Html - SPA页面收集(有图)

    场景,左图,又字段的布局 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  6. SqlParameter 基本用法

    因为通过SQL 语句的方式,有时候存在脚本注入的危险,所以在大多数情况下不建议用拼接SQL语句字符串方式,希望通过SqlParameter实现来实现对数据的操 作,针对SqlParameter的方式我 ...

  7. [APAC]手动截取当前活动窗口,并且按规则命名(1/2)

    Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...

  8. 分布式架构高可用架构篇_01_zookeeper集群的安装、配置、高可用测试

    参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...

  9. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. c3p0配置 initialPoolSize 和minPoolSize 可以设为0吗?设0有坏处吗?

    c3p0配置 initialPoolSize 和minPoolSize 可以设为0吗?设0有坏处吗? c3p0配置 initialPoolSize 和minPoolSize 可以设为0吗?设0有坏处吗 ...