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. Spring Roo

    Spring Roo 是SpringSource新的开放源码技术,该技术主要面向企业中的Java开发者,使之更富有成效和愉快的进行开发工作,而不会牺牲工程完整或灵活性.无论你是一个新的Java开发人员 ...

  2. 推荐几款web站点JS(JQeury)图表(饼图,柱图,线图)

    一 Google Chart Tools 官网:https://developers.google.com/chart/ 谷歌图表工具提供了一个完美的方式形象化您的网站上的数据.从简单到复杂的层次结构 ...

  3. [转]ASP.NET MVC Spring.NET NHibernate 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  4. Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null

    在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null 这个错误,检查下renderT ...

  5. Ubuntu 关闭锁屏界面的 on-screen keyboard

    试了试屏幕键盘,在 系统设置里开启了,又关了,但是在屏幕解锁时总是出现 screen keyboard,老烦人了,不知到在哪里关闭了,系统设置里面都关了,网上搜了解决办法,原来在这里 把 show w ...

  6. UIToolbar swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  7. 硬件相关-ADC原理(未完成)

    一.模数转换的一般步骤: 1)采样和保持 为了把模拟信号转换成对应的数字信号,必须首先将模拟量每隔一定时间抽取一次样值,使时间上连续变化的模拟量变为一个时间上断续变化的模拟量,这个过程称为采样. 为了 ...

  8. 【bzoj1009】[HNOI2008]GT考试

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3018  Solved: 1856[Submit][Statu ...

  9. smarty模板技术

    一.什么是smarty?smarty是一个使用php写出来的模板php模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用php程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美 ...

  10. 利用excel数据透视表实现快速统计相关数据

    昨天ytkah在做数据报表时需要做一些具体统计数字:公司每天都有人申请铅笔.笔记本等一些文具用品,现在想要统计每天申请铅笔多少支.笔记本多少本,如下图所示,这个要如何实现呢? excel数据透视表怎么 ...