Algorithm-Day00

  这个关于算法的博客系列我将用英文(都是基础英文词汇)来写,文中若有英文语法搭配错误请大家指出。

  This is the source code:

 1 #include <iostream>
 2 void mySwap(int &a,int &b);
 3 int main(){
 4     int a[]={23,12,24,56,6,7,92,15,68,873,145};
 5     int length=sizeof(a)/sizeof(int);
 6     int maxIndex=length-1;
 7     for(int j=0;j<length-1;j++){
 8         for(int k=0;k<maxIndex;k++){
 9             if(a[k]>a[k+1]){
10                 mySwap(a[k],a[k+1]);
11             }
12         }
13         //maxIndex--;
14     }
15
16     for(int i=0;i<length;i++){
17         std::cout<<a[i]<<"   ";
18     }
19     std::cout<<std::endl;
20 }
21 void mySwap(int &a,int &b){
22     a=a-b;
23     b=a+b;
24     a=b-a;
25 }

  This file shows two algorithms:1.mySwap(int &a,int &b); 2.Bubble sort;

  You may write mySwap(int &a,int &b) in this way:

1 void mySwap(int &a,int &b){
2     int temp=a;
3     a=b;
4     b=a;
5 }

  This is the most normal way of swapping two numbers,but what if we could swap two numbers without having to declare a third variable(int this case it‘s ‘temp‘)?

  It only needs a math trick to realize it:

1 void mySwap(int &a,int &b){
2     a=a-b;
3     b=a+b;
4     a=b-a;
5 }

  In line 2,we assign the result of (a-b) to the value of a;

  In line 3,we set b to be (a+b).On the right side:a=a(orignal)-b(orignal),b=b(orignal).So ‘b=a+b‘ means ‘b=a(orignal)-b(orignal)+b(orignal)‘ which is equal to b=a(orignal);

  In line 4,we analyse the statement the same way.On the right side:b=a(orignal),a=a(orignal)-b(orignal).So ‘a=b-a‘ means a=a(orignal)-(a(orignal)-b(orignal)) which equals to ‘a=b(orignal)‘

  Problem solved!!!

  Now let‘s get into the Bubble sort.Bascially it has a framework like this:

1 for(   ){
2     for(   ){
3
4     }
5 }

  As you can see,it‘s made of two for loop---an inner loop and an outter loop;

  Let‘s analyse the inner loop first,it looks like this:

1 for(int k=0;k<maxIndex;k++){
2             if(a[k]>a[k+1]){
3                 mySwap(a[k],a[k+1]);
4             }
5         }

  Here I want talk a little bit about the basics of a for loop.When you write a for loop like this:

for(int i=0;i<m;i++){    }

  It‘s gonna run m times.So In this case:it‘s gonna run maxIndex times.Alright what this for loop are doing?

1 for(int j=0;j<length-1;j++){
2         for(int k=0;k<maxIndex;k++){
3             if(a[k]>a[k+1]){
4                 mySwap(a[k],a[k+1]);
5             }
6         }
7         //maxIndex--;
8     }

  For the first time:it takes a[0] and a[1] out of the array and excute the if statement.if a[0]>a[1],then we swap these two numbers,then we do ‘k++‘.So the second time we do the same thing with a[1] and a[2],the loop goes on and on,until k gets to (maxIndex-1).The job of this for loop is clear now,it finds the biggest number and put it to the very right location of the array!!

  What we are gonna do is to align this array form the min number to the max number,and if the inner loop only runs a single time,it can only assure us the max number is in the right location.What about the others?We have to excute the inner for loop for a certain amount of times.

  Here appears another question:Ok,I get it,the inner loop assures us the max number is in the very right place in the array.But What‘s the point if we excute it for a lot of times?The goal is to set all the numbers in the right places not just the max one.

  This is the answer:When we have excuted the inner for loop for the first time,the array changes:The max number is in the right.This matters to the others!!The inner loop excutes in a order form left to right!! It first takes out a[0] and a[1],and the takes out a[1] and a[2],and then a[2] and a[3]...etc.So the second times we excutes the inner loop,we can be sure that we find the max one in the ohter numbers and put it next to the max number we find the first time,the loop goes on and on,finally we get a array that align in a order from the min one to the max one!!

  But this is a little bit hard to understand,and a little less effectient(I‘ll left that for you to figure it out),and that‘s why I put this code in line7:

//maxIndex--;

  This line of code is inside the outter loop,and that means it will run the same times as the inner for loop.

  What it does is simple:It‘s more like cut out the last element in the array and so the next time we excute the array we can find the largest one and put it to the very right location  of the array.In this way,we can solve the problem both more understandable and effectient.

  

时间: 2024-11-08 17:26:44

Algorithm-Day00的相关文章

PLA Percentron Learning Algorithm #台大 Machine learning #

Percentron Learning Algorithm 于垃圾邮件的鉴别 这里肯定会预先给定一个关于垃圾邮件词汇的集合(keyword set),然后根据四组不通过的输入样本里面垃圾词汇出现的频率来鉴别是否是垃圾邮件.系统输出+1判定为垃圾邮件,否则不是.这里答案是第二组. 拿二维数据来做例子.我们要选取一条线来划分红色的叉叉,和蓝色的圈圈样本点(线性划分).怎么做呢?这里的困难之处就在于,其实可行的解可能存在无数条直线可以划分这些样本点.很难全部求解,或许实际生活中并不需要全部求解.于是,

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得

支付宝支付php的demo或sdk报错 Warning: openssl_sign() [function.openssl-sign]: Unknown signature algorithm. in

最近在做支付宝支付,在本地测试一切正常,上传到服务器就遇到报错: Warning: openssl_sign() [function.openssl-sign]: Unknown signature algorithm. in 后来查了查,是我的服务器上PHP环境支持openssl_sign()但却不支持 OPENSSL_ALGO_SHA256这样的参数,问了一下大佬,才发现这个参数是在php5.4.8以上版本才支持,低版本的是使用的SHA256,于是乎试了一下,搞定! 报错原因是支付宝的dem

Berlekamp-Massey Algorithm [for Team Problem 5525]

Input: 第一行为两个正整数n,m 第二行为n个整数a1..an 最后一行为一个正整数k Output: 为一个整数,代表方案数对1000000007取模的值 Sample Input 5 3 1 1 2 0 2 2 Sample Output 3 来自毛爷爷17年论文 Berlekamp-Massey Algorithm直接开算 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const

Strassen algorithm(O(n^lg7))

Let A, B be two square matrices over a ring R. We want to calculate the matrix product C as {\displaystyle \mathbf {C} =\mathbf {A} \mathbf {B} \qquad \mathbf {A} ,\mathbf {B} ,\mathbf {C} \in R^{2^{n}\times 2^{n}}} If the matrices A, B are not of ty

LabelRank(A Stabilized Label Propagation Algorithm for Community Detection in Networks)非重叠社区发现

最近在研究基于标签传播的社区分类,LabelRank算法基于标签传播和马尔科夫随机游走思路上改装的算法,引用率较高,打算将代码实现,便于加深理解. 一.概念 相关概念不再累述,详情见前两篇文章 二.算法思路 (1)Propagation (2)Inflation (3)Cut off (4)Explicit Conditional Update (5)Stop Criterion 三.A Stabilized Label Propagation Algorithm for Community D

(转)常用算法(Algorithm)的用法介绍

2算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. 2<algorithm>是所有STL头文件中最大的一个,其中常用到的功能范围涉及到比较.交换.查找.遍历操作.复制.修改.反转.排序.合并等等. 2<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作. 2<functional>中则定义了一些模板类,用以声明函数对象. 2STL提供

hihocoder1198 Memory Allocating Algorithm(链表~)

题意: 小Hi和小Ho最近在研究内存分配的机制,他们写了一个比较简单的内存.内存可以表示成M个连续的存储空间,下标为0..M-1: 每当有数据写入时,内存分配程序会从下标0开始向右找一块足够存放下该数据的区域,将该数据写入.比如写入一个长度为2的数据,因为是第一个数据,我们用1来表示: 之后继续依次写入长度为3的数据和长度为2的数据,则有: 当数据足够多后,我们可能会遇到剩下的空间不足以写下新的数据.这时内存程序会从最早的数据开始进行删除.假设我们现在写到第8个数据把内存写满了: 这时我们需要写

Algorithm - Introduction

Goal: Use Computer to solve problems step by step!!! What is Computer Science? Computer Science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process. What is Programming? Programming is the process

数据结构(DataStructure)与算法(Algorithm)、STL应用

catalogue 0. 引论 1. 数据结构的概念 2. 逻辑结构实例 2.1 堆栈 2.2 队列 2.3 树形结构 2.3.1 二叉树 3. 物理结构实例 3.1 链表 3.1.1 单向线性链表 3.1.2 单向循环链表 3.1.3 双向线性链表 3.1.4 双向循环链表 3.1.5 数组链表 3.1.6 链表数组 3.1.7 二维链表 3.2 顺序存储 4. 算法 4.1 查找算法 4.2 排序算法 0. 引论 0x1: 为什么要学习数据结构 N.沃思(Niklaus  Wirth)教授提