希尔排序学习笔记是参考这位博主的:
http://blog.csdn.net/feixiaoxing/article/details/6844826
希尔排序,。它的基本思想是:首先按照一个序列递减的方法逐渐进行排序。比如说有10个数据,我们按照序列5、3、1的顺序进行排序。首先是5,那么我们对1和6、2和7、3和8、4和9、5和10进行排列;第二轮是3,那么对数据1、4、7、10排列,再对2、5、8进行排列,以及3、6、9排列;第三轮就和冒泡排序一样了,以此对每个数据进行排列。它的优势就是让整个队列基本有序,减少数据移动的次数,从而降低算法的计算复杂度。
第一轮:0和5 1和6 2和7 3和8 4 和9 排序??void shell_sort(int array[], 10, 5)
{
int inner = 0;
int outer = 0;
int median = 0;
if(NULL == array || 0 == length)
return;
for(int index = 0; index <5; index ++){
if((length -1) < (index + s5)) //index=0 1 2 3 4往下执行 ,其他情况 continue;相当于break
continue;
else{
outer = index + 5;
while( (outer + 5) <= (length - 1))// 条件为假,下面不执行
outer += 5;
}
for(; outer >= (index + 5); outer -= 5){//因为当index=0 1 2 3 4, outer = index + 5; 所以执行1次
for(inner = index; inner <= 5; inner += 5){
if(array[inner] >= array[inner + 5]){
median = array[inner];
array[inner] = array[inner + 5];
array[inner + 5] = median;
}
}
}
}
} 第二轮是3,那么对数据1、4、7、10排列,再对2、5、8进行排列,以及3、6、9排列void shell_sort(int array[], int length, int step)
{
int inner = 0;
int outer = 0;
int median = 0;
if(NULL == array || 0 == length)
return;
for(int index = 0; index < 3; index ++){
if(9 < (index + 3))//index = 0 1 2 3 4 5
continue;
else{
outer = index + 3;
while( (outer + 3) <= (length - 1))
outer += 3; //一直到 outer=9
}
for(; outer >= (index + 3); outer -= 3){ // outer = 9;所以每次执行3次
for(inner = index; inner <= outer - 3; inner += 3){ //index=0 1 2
if(array[inner] >= array[inner + 3]){
median = array[inner];
array[inner] = array[inner + 3];
array[inner + 3] = median;
}
}
}
}
}
再把拆了:
for(; outer >= (index + 3); outer -= 3){ // outer = 9;所以每次执行3次
for(inner = index; inner <= outer - 3; inner += 3){ //index=0 1 2
if(array[inner] >= array[inner + 3]){
median = array[inner];
array[inner] = array[inner + 3];
array[inner + 3] = median;
}
}
}
1. outer = 9;
for(; outer >= (index + 3); outer -= 3){ // outer = 9;
for(inner = 0; inner <= 6; inner += 3){ //inner=0 3 6
if(array[inner] >= array[inner + 3]){ //
median = array[inner];
array[inner] = array[inner + 3];
array[inner + 3] = median;
}
}
}
//上面就是用了冒泡的思想一开始先是 0 和3 大的放后面,再是3 和6大的放后面,然后再是6和9
2. outer = 6;
for(; outer >= (index + 3); outer -= 3){ // outer = 6;
for(inner = 0; inner <= 3; inner += 3){ //inner=0 3
if(array[inner] >= array[inner + 3]){
median = array[inner];
array[inner] = array[inner + 3];
array[inner + 3] = median;
}
}
}
//上面就是用了冒泡的思想一开始先是 0 和3 大的放后面,再是3 和6大的放后面
3. outer = 3;
for(; outer >= (index + 3); outer -= 3){ // outer =3
for(inner = index; inner <= 0; inner += 3){ //index=0
if(array[inner] >= array[inner + 3]){
median = array[inner];
array[inner] = array[inner + 3];
array[inner + 3] = median;
}
}
}
//上面就是用了冒泡的思想一开始先是 0 和3 大的放后面, 第三轮就和冒泡排序一样了 void shell_sort(int array[], 10, 1)
{
int inner = 0;
int outer = 0;
int median = 0;
if(NULL == array || 0 == length)
return;
for(int index = 0; index < 1; index ++){//只执行一次
if((length -1) < (index + 1))
continue;
else{
outer = index + 1;
while( (outer + 1) <= (length - 1)) //一直到outer=9
outer += 1;
}
//下面就是传说中的冒泡了
for(; outer >= (index + 1); outer -= 1){
for(inner = index; inner <= outer - 1; inner += 1){
if(array[inner] >= array[inner + 1]){
median = array[inner];
array[inner] = array[inner + 1];
array[inner + 1] = median;
}
}
}
}
}
}
来源:CSDN
作者:4M小水管
链接:https://blog.csdn.net/ap105638/article/details/8883193