一、
函数:void Sec_Sort(int* head,int low,int high,int Step_L,int Bool)
参数解释
head:数组指针
[low, high]:需排序的数组范围
Step_L:需排序步长
Bool:等于1表示从小到大排序,不等于1从大到小排序
说明
扫描数组中i到Shigh范围,步长为Step_L
遇到更小的值则将temp指向其下标,内层循环完毕temp指向i到Shigh中的最值
将temp处的值与i处的值进行交换
Step_L=1时
第一步对下标low+{0,1,2,3,4,5…}进行选择排序
Step_L=2时
第一步对下标low+{0,2,4,6,8,10…}进行选择排序
Step_L=3时
第一步对下标low+{0,3,6,9,12,15…}进行选择排序
以此类推
此方法仅在内层循环结束需要交换一次,交换次数为第一层循环次数
#include <stdio.h>
#include <time.h>
//简单选择排序
void Sec_Sort(int* head,int low,int high,int Step_L,int Bool){
int Shigh=low+(((high-low+1)/Step_L)-1+(((high-low+1)%Step_L)!=0))*Step_L;//Step_L属于{low,low+Step_L,low+2*Step_L,...,Shigh}
int temp;//保存最值位置
int temp1;//交换
for(int i=low;i<=Shigh-Step_L;i+=Step_L){
temp=i;
for(int j=i+Step_L;j<=Shigh;j+=Step_L){
if((head[temp]>head[j])==Bool){
temp=j;
}
}
temp1=head[i];
head[i]=head[temp];
head[temp]=temp1;
}
}
int main(int argc, char **argv) {
printf("Hello, World!\n");
int s[5];
s[0]=2;
s[1]=5;
s[2]=1;
s[3]=4;
s[4]=3;
int i=0;
while(i<5)printf("%d ",s[i++]);
printf("\n---------\n");
Sec_Sort(s,0,4,1,1);
int j=0;
while(j<5)printf("%d ",s[j++]);
return 0;
}