Classical Classification:Bubble Sorting

来源:互联网 发布:知乎日报吐槽合集 编辑:程序博客网 时间:2024/06/05 05:13

BRIEF INTRODUCTION

The bubble sorting method is very old and very simple to understand.By keeping picking the max(min) number from the array and place it to the end(beginning) of the array,we can accomplish the target that sorting a chaos array from min to max(from max to min).

DIFFICULTY

The theory is quite easy.However,it’s kind of hard to trans it into c code,especially into a standard c function.

We usually store all the chaos data as a long array.basing the theory,we have to go through the array once and once again with loop structure.So we have got to know how much elements we store in the array.

1. CALCULATE THE NUMBER OF ELEMENTS OF A ARRAY

CALCULATE THE LENGTH OF A VARIABLE

SIZEOF(a)
// we can use it to calculate the length of a variable,even a array variable.
// if it’s just a normal variable like an int-type one,for example named ‘a’
// we can just get its length by passing its name as the parameter ‘a’ and typing like sizeof(a)
// but if it’s an array-type one,for example named ‘b[n]’
// we can get its length by passing its address as the parameter ‘b’(‘&b[0]’) and typing like sizeof(b)
// for convenience,we’d better define it as a macro at the beginning of the source file.

We can calculate the whole length of the array first.And then calculate the length of its first element.We divide the whole length by the first element’s length(which equals with other single element’s length) and get the number of elements of the array.

 

The standard code

#define GET_ARRAY_LENGTH(array,len){len=(sizeof(array)/sizeof(array[0]));}

2. SWITCH TWO NUMBERS

I have got to say this is the easiest part,I have got to mention it a little though.

Every variable is just like a can,which can only store one value at one time.So if you want to reload a new value,you have to drop the old one,but which is we want to give to the other one.So we just find an another empty can to temporarily store the old value. Easy,isn’t it?!

 

The standard code

//I assume I am gonna switch a & bint temp;a = temp;a = b;b = temp;

3. PASS AN ARRAY AS THE PARAMETER

In fact,if you do the above steps in main function straight in order,you can definitely sort the chaos numbers.However,if you want to write a function to save your time and make your code more neat,you may meet a problem.You may find that you get the wrong length of the input array.

You may be very confused but don’t worry.It’s true we can’t calculate the input array’s length in a sub-function.However,we can calculate its length before the sub-function is used,and pass the length as a parameter to the function so that we don’t have to worry about the length stuff.

EXAMPLE CODE

#include <stdio.h>#include <string.h>#define GET_ARRAY_LEN(array,len){len=sizeof(array)/sizeof(array[0]);}void Sort_Min_to_Max(int p[],const int len);int main(int argc,char **argv){    int a[5]={1,0,2,5,4};    int len;     GET_ARRAY_LEN(a,len);    Sort_Min_to_Max(a,len);   return 0;}void Sort_Min_to_Max(int p[],const int len){    int i,j,temp;    int *q = p;    for(i=0;i<len;i++)        for(j=0;j<len-i-1;j++)        {            if(*(q+j)>*(q+j+1))             {              temp = *(q+j+1);              *(q+j+1) = *(q+j);              *(q+j) = temp;            }        }    for(i=0;i<len;i++)        printf("%d\n",*(p+i));}
原创粉丝点击