• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

选择稳定版本和不稳定版本进行排序


Recommended Posts

选择排序

非稳定版本与稳定版本

在排序的过程中,选择一个较大的数(从大到小排序),然后放入数组中的指定位置;此时,数据可以直接与数组中的指定位置进行交换,但相同值的数据可能会改变顺序,这种情况称为“不稳定性”。所谓“稳定”和“不稳定”,可以通过下图来理解。

不稳定排序算法按数字排序时,会打乱同值的原始色序。

[[7], [2], [4], [5], [2], [5]]

[[7], [5], [5], [4], [2], [2]]

原来前两个在后面,然后他们的位置就变了。

稳定排序算法在按数字排序时,会保持原来的颜色序列值不变,如下图,2和2的相对位置不变。

[[7], [2], [4], [5], [2], [5]]

[[7], [5], [5], [4], [2], [2]]

代码示例(C实现)

//不稳定版本

void move _ selected _ unstable _ version(int * arr,int i,int m)

{

如果(m!=i) //如果选中的数字已经在I位置(当前要放置的位置),就不用移动了

{

int tmp=arr[m];

arr[m]=arr

arr=tmp;

}

}

//稳定版,为了把arr[m]移到I,中间的元素都需要右移。

void move _ selected _ stable _ version(int * arr,int i,int m)

{

int j,tmp

如果(m!=i) //如果选中的数字已经在I位置(当前要放置的位置),就不用移动了

{

tmp=arr[m];

for(j=m;j I;- j)

{

arr[j]=arr[j-1];

}

arr=tmp;

}

}

//选择排序

void select_sort(int *array,int size)

{

int max

int pos

int i,j;

for(I=0;一号尺码;I )//循环一次找到一个更大的数,然后和数组的第n个数据交换位置(n从0开始)

{

max=array

pos=I;

for(j=I;j=大小-1;j)

{

if(array[j] max)

{

max=array[j];

pos=j;

}

}

move _ selected _ unstable _ version(array,I,pos);//不稳定版本

//move _ selected _ stable _ version(array,I,pos);//稳定版本

}

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now