C之算法
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之算法的更多相关文章
- B树——算法导论(25)
B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...
- 分布式系列文章——Paxos算法原理与推导
Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...
- 【Machine Learning】KNN算法虹膜图片识别
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
- 红黑树——算法导论(15)
1. 什么是红黑树 (1) 简介 上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- 虚拟dom与diff算法 分析
好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- 神经网络、logistic回归等分类算法简单实现
最近在github上看到一个很有趣的项目,通过文本训练可以让计算机写出特定风格的文章,有人就专门写了一个小项目生成汪峰风格的歌词.看完后有一些自己的小想法,也想做一个玩儿一玩儿.用到的原理是深度学习里 ...
- 46张PPT讲述JVM体系结构、GC算法和调优
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
随机推荐
- 条款24:若所有的函数参数可能都需要发生类型转换才能使用,请采用non-member函数
假设有一个有理数类Rational,有一个计算有理数乘法的成员函数operator*,示例如下: #include <iostream> class Rational { public: ...
- 条款7:为多态基类声明virtual析构函数
C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...
- [转]Ubuntu下配置NFS服务
[转]Ubuntu下配置NFS服务 http://blog.163.com/liu8821031%40126/blog/static/111782570200921021253516/ Table ...
- 58.xilinx原语DCM,PLL的使用
DCM_BASE 基本数字时钟管理模块的缩写,是相伴和频率可配置的数字锁相环电路,常用于FPGA系统中复杂的时钟管理.如需要频率和相位动态配置,则可以选用DCM_ADV原语,如需要相位动态偏移,可使用 ...
- 按键精灵实现自动退出的MsgBox消息框
要实现自动倒计时退出的消息框,代码如下: Set wsh = CreateObject("WScript.Shell") wsh.popup "设置完毕,3秒后自动退出! ...
- PE格式的理解(待补充)
PE文件格式 一.基本结构 1.DOS头一般到节区头成为PE头部分,其下称为PE体.文件的内容一般可分为代码(.text).数据(.data).资源(.rsrc),分别保存. 2.PE头与各节区的尾部 ...
- 寻找“水王”--c++
一.题目与设计思路 三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖数目超过了帖子数目的一 ...
- [转载]Nginx如何处理一个请求
http://nginx.org/cn/docs/http/request_processing.html 对我的扫盲文章 基于名字的虚拟主机 Nginx首先选定由哪一个虚拟主机来处理请求.让我们从一 ...
- Jquery_联系电话正则表达式
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 一个Linq
public class CalendaerCollectItem { public int ID { get; set; } public string Name { get; set; } pub ...