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