1° 选择排序算法

   核心思路如下图:

以字符串排序进行说明

#include <stdio.h>
#include <string.h>
#define SIZE 81
#define LIM 20
#define HALT " "
void stsrt(char *strings[], int num); int main(void)
{
char input[LIM][SIZE];
char *ptstr[LIM];
int ct = ;
int k; printf("Input up %d lines, and I will sort them.\n", LIM);
printf("To stop, press the Enter key at a line's start.\n");
while(ct < LIM && gets(input[ct]) != NULL && input[ct][] != '\0')
{
ptstr[ct] = input[ct];
ct++;
}
stsrt(ptstr, ct);
puts("\nHere's the sorted list: \n");
for(k = ; k < ct; k++)
puts(ptstr[k]); return ;
}
void stsrt(char *strings[], int num)
{
char *temp;
int top, seek; for(top = ; top < num - ; top++)
for(seek = top + ; seek < num; seek++)
if(strcmp(strings[top], strings[seek]) > )
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}

以int数组排序进行说明(以C语言为例)

#include <stdio.h>

void sort(int a[], int len);
int main(void)
{
int number[] = {, , , , , , , , , , , , };
int len = sizeof(number) / sizeof(number[]);
sort(number, len);
for(int i = ; i <len; i++)
printf("%d ", number[i]);
return ;
}
void sort(int a[], int len)
{
int temp;
for(int i = ; i < len - ; i++)
{
for(int j = i + ; j < len; j++)
if(a[i] > a[j]) // 小——>大 若a[i] < a[j]; 大——>小
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

(以JAVA语言为例)

public class ArrayDemo {

    public static void main(String[] args) {
int age[] = {31, 30, 18, 17, 8, 9, 1, 39};
sort(age);
print(age);
} public static void print(int temp[]) {
for(int i = 0; i < temp.length; i++)
System.out.print(temp[i] + " ");
} public static void sort(int temp[]) {
for(int i = 0, t = 0; i < temp.length - 1; i++)
for(int j = i + 1; j < temp.length; j++)
if(temp[j] < temp[i]) {
t = temp[j];
temp[j] = temp[i];
temp[i] = t;
}
}
}

核心思想:(查找和放置)选择剩余最大值的一个办法就是比较剩余数组的第一和第二个元素。如果第二个元素大,就交换这两个数据。想在比较第一个和第三个元素。如果第三个大,就交换这两个数据。每一次交换都把大的元素移到上面。继续这种方法,直到比较第一个和最后一个元素。完成以后,最大的数就在剩余数组的第一个元素中。此时第一个元素已经排好了序,但是数组中的其他元素还很混乱。

2° 冒泡排序算法

核心思路如下图:

(以JAVA语言为例)

public class OtherDemo {

    public static void main(String[] args) {
int[] arr = new int[] { 3, 1, 6, 5, 4 };
//排序前
print(arr);
bubbleSort(arr);
//排序后
print(arr);
} public static void print(int temp[]) {
for (int i = 0; i < temp.length; i++) {
if (i != temp.length - 1)
System.out.print(temp[i] + ", ");
else
System.out.println(temp[i]);
} } //冒泡排序
public static void bubbleSort(int temp[]) {
for (int i = 0; i < temp.length - 1; i++)
for (int j = 0; j < temp.length - i - 1; j++) //-x:让每一次比较的元素减少,-1:避免下标越界
if (temp[j] > temp[j+1]) {
int t = temp[j];
temp[j] = temp[j+1];
temp[j+1] = t;
}
} }

3° 二分搜索算法

核心思路如下图:

(以C语言为例)

#include <stdio.h>

int search(int key, int a[], int len);
int main(void)
{
int k = ;
int number[] = {, , , , , , , , , , , , };
int r = search(k, number, sizeof(number) / sizeof(number[]));
if(r > -)
printf("%d\n", number[r]);
return ;
}
// 二分搜索(排好序的数组)
int search(int key, int a[], int len)
{
int ret = -;
int left = ;
int right = len - ; while(right >= left) // 条件应该是right >= left,而不应是right > left
{
int mid = (left + right) / ;
if(a[mid] == key)
{
ret = mid;
break;
}
else if(a[mid] > key)
right = mid - ;
else
left = mid + ;
}
return ret;
}

(以JAVA语言为例)

 4° 一个统计单词数的小程序

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
// 统计单词数的函数
int count_word(char * str); int main(void)
{
char str[]; puts("请输入一个字符串:");
gets(str);
printf("统计的单词数是:%d", count_word(str)); return ;
}
int count_word(char * str)
{
bool inword = false;
int n_words = ; while(*str)
{
if(!isspace(*str) && !inword)
{
inword = true;
n_words++;
}
if(isspace(*str) && inword)
inword = false;
str++;
}
return n_words;
}

C之算法的更多相关文章

  1. B树——算法导论(25)

    B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...

  2. 分布式系列文章——Paxos算法原理与推导

    Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...

  3. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  4. 红黑树——算法导论(15)

    1. 什么是红黑树 (1) 简介     上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...

  5. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  6. 虚拟dom与diff算法 分析

    好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM

  7. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  8. 神经网络、logistic回归等分类算法简单实现

    最近在github上看到一个很有趣的项目,通过文本训练可以让计算机写出特定风格的文章,有人就专门写了一个小项目生成汪峰风格的歌词.看完后有一些自己的小想法,也想做一个玩儿一玩儿.用到的原理是深度学习里 ...

  9. 46张PPT讲述JVM体系结构、GC算法和调优

    本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...

  10. 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法

    若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...

随机推荐

  1. iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能

    1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distr ...

  2. UITextField的常用属性,Delegate,重绘

        一  属性 UITextField * myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50 ...

  3. EF6 在原有数据库中使用 CodeFirst 总复习(四、新建实体对象)

    在原有数据库中使用 CodeFirst ,除了第一次添加实体后要立即执行一次 Enable-Migrations add-migration Initial  -IgnoreChanges updat ...

  4. 网页设计师必备的10个CSS技巧

    CSS是网页设计师的基础,对CSS的了解能使他们能够设计出更加美观别致的网页.使用CSS技巧来巧妙地处理CSS是非常令设计师着迷的事情.在CSS的深海世界里有很多有意思的东西,你只需要找到最适合你的就 ...

  5. 使用xilinx ip core FIFO First- World First-Through (FWFT)模式的注意事项

    也许很多人知道xilinx ip core 中的fifo可以配成standard 模式和FWFT模式,并知道两者的区别是:standard模式下,当rd为高时,fifo会延时一个时钟输出数据(时序逻辑 ...

  6. 使用Log4j进行日志操作

    使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...

  7. Oracle VM VirtualBox 5.0 CentOS 6.4 共享文件夹

    首先在主机(win7)的硬盘建立需要共享文件夹 例如 D:\share_test 然后虚拟机光驱加载Oracle VM VirtualBox安装目录的iso  C:\Program Files\Ora ...

  8. SqlServer Split函数

    Create FUNCTION [dbo].[SplitToTable] ( @SplitString nvarchar(max), @Separator nvarchar(10)=' ' ) RET ...

  9. 关于High-Resolution Timer(了解)

    如果一个系统包含高精度性能计数器(HRPC,high-resolution performance counter)则此系统提供高精度定时器.你可以使用API函数QueryPerformanceFre ...

  10. YTKNetwork

    YTKNetwork 是猿题库 iOS 研发团队基于 AFNetworking 封装的 iOS 网络库,其实现了一套 High Level 的 API,提供了更高层次的网络访问抽象. YTKNetwo ...