快速排序:@H_404_5@
voID QuickSort(int s[],int l,int r)@H_404_5@
{@H_404_5@
if (l< r)@H_404_5@
{@H_404_5@
int i = l,j = r,x = s[l];@H_404_5@
while (i < j)@H_404_5@
{@H_404_5@
while(i < j && s[j]>= x) // 从右向左找第一个小于x的数@H_404_5@
j--;@H_404_5@
if(i < j)@H_404_5@
s[i++] = s[j];@H_404_5@
while(i < j && s[i]< x) // 从左向右找第一个大于等于x的数@H_404_5@
i++;@H_404_5@
if(i < j)@H_404_5@
s[j--] = s[i];@H_404_5@
}@H_404_5@
s[i] = x;@H_404_5@
quickSort(s,l,i - 1); // 递归调用@H_404_5@
quickSort(s,i + 1,r);@H_404_5@
}@H_404_5@
}@H_404_5@
选择排序:@H_404_5@
voID SelectSort(int s[],int n)@H_404_5@
{@H_404_5@
int i,j,k;@H_404_5@
for (i=0; i < n-1; i++)@H_404_5@
{@H_404_5@
k = i;@H_404_5@
for (j=i+1; j < n; j++)@H_404_5@
{@H_404_5@
if (a[j] < a[k]) k = j;@H_404_5@
}@H_404_5@
if (k != i) swap(s[k],s[i]);@H_404_5@
}@H_404_5@
}@H_404_5@
冒泡排序:@H_404_5@
voID BubbleSort(int s[],int n)@H_404_5@
{@H_404_5@
for (int i = 0; i < n; i++)@H_404_5@
{@H_404_5@
for (int j = 0; j < n - i - 1; j++)@H_404_5@
{@H_404_5@
if (s[j] > s[j + 1])@H_404_5@
{@H_404_5@
swap(s[j],s[j + 1]);@H_404_5@
}@H_404_5@
}@H_404_5@
}@H_404_5@
}@H_404_5@
希尔排序:@H_404_5@
voID shellsort(int s[],int n)@H_404_5@
{@H_404_5@
int gap; //gap为增量,为任意小于n的数@H_404_5@
for(gap = 3; gap >0; gap--)@H_404_5@
{@H_404_5@
for(int i=0; i for(int j = i+gap; j int temp = s[j],k = j-gap;@H_404_5@ while(k>=0&&s[k]>temp)@H_404_5@ {@H_404_5@ s[k+gap] = s[k];@H_404_5@ k = k-gap;@H_404_5@ }@H_404_5@ s[k+gap] = temp;@H_404_5@ }@H_404_5@ }@H_404_5@ }@H_404_5@ }@H_404_5@ 插入排序:@H_404_5@ voID InsertSort(int s[],int n)@H_404_5@ {@H_404_5@ for (int i = 1; i < n; i++) //默认第一个元素已经是有序的,因此从第二个元素开始@H_404_5@ {@H_404_5@ int j = i-1,temp=s[i];@H_404_5@ while (j >=0&&s[j] > temp) //前一个元素比待插元素大@H_404_5@ {@H_404_5@ s[j+1] = s[j]; //如果待插入元素比拍好元素大,则循环右移大的那部分元素@H_404_5@ //留出位置给待插元素@H_404_5@ j--;@H_404_5@ }@H_404_5@ s[j+1] = temp; //插入元素@H_404_5@ }@H_404_5@ }@H_404_5@ 归并排序:@H_404_5@ /*该函数将数组下标范围[l1,r1]和[l2,r2]的有序序列合并成一个有序序列*/@H_404_5@ voID merge(int nums[],int l1,int r1,int l2,int r2 ) {@H_404_5@ int i = l1; //左半部分起始位置@H_404_5@ int j = l2; //右半部分起始位置@H_404_5@ int n = (r1 - l1 + 1) + (r2 - l2 + 1); //要合并的元素个数@H_404_5@ vector int k = 0; //辅助数组其起始位置@H_404_5@ while (i <= r1&&j <= r2) { //挑选两部分中最小的元素放入辅助数组中@H_404_5@ if (nums[i] < nums[j])@H_404_5@ temp[k++] = nums[i++];@H_404_5@ else@H_404_5@ temp[k++] = nums[j++];@H_404_5@ }@H_404_5@ //如果还有剩余,直接放入到辅助数组中@H_404_5@ while (i <= r1)@H_404_5@ temp[k++] = nums[i++];@H_404_5@ while (j <= r2)@H_404_5@ temp[k++] = nums[j++];@H_404_5@ //更新原始数组元素@H_404_5@ for (int i = 0; i < n;i++)@H_404_5@ {@H_404_5@ nums[l1 + i] = temp[i];@H_404_5@ }@H_404_5@ }@H_404_5@ /*二路归并排序(递归实现)*/@H_404_5@ voID MergeSort(int nums[],int start,int end) {@H_404_5@ if (start < end) {@H_404_5@ int mID = (start + end)/2; //分割序列@H_404_5@ MergeSort(nums,start,mID); //对序列左半部分进行归并排序@H_404_5@ MergeSort(nums,mID + 1,end); //对序列右半部分进行归并排序@H_404_5@ merge(nums,mID,end); //合并已经有序的两个序列@H_404_5@ }@H_404_5@ }@H_404_5@ @H_404_5@ 堆排序:@H_404_5@ voID adjust(int arr[],int len,int index)@H_404_5@ {@H_404_5@ int left = 2 * index + 1;@H_404_5@ int right = 2 * index + 2;@H_404_5@ int maxIDx = index;@H_404_5@ if (left if (right if (maxIDx != index) // 如果maxIDx的值有更新@H_404_5@ {@H_404_5@ swap(arr[maxIDx],arr[index]);@H_404_5@ adjust(arr,len,maxIDx);// 递归调整其他不满足堆性质的部分@H_404_5@ }@H_404_5@ }@H_404_5@ voID heapSort(int arr[],int size)@H_404_5@ {@H_404_5@ for (int i = size / 2 - 1; i >= 0; i--) // 对每一个非叶结点进行堆调整(从最后一个非叶结点开始)@H_404_5@ {@H_404_5@ adjust(arr,size,i);@H_404_5@ }@H_404_5@ for (int i = size - 1; i >= 1; i--)@H_404_5@ {@H_404_5@ swap(arr[0],arr[i]); // 将当前最大的放置到数组末尾@H_404_5@ adjust(arr,i,0); // 将未完成排序的部分继续进行堆排序@H_404_5@ }@H_404_5@ }@H_404_5@ 总结 以上是内存溢出为你收集整理的所有常用简单排序算法的c++代码全部内容,希望文章能够帮你解决所有常用简单排序算法的c++代码所遇到的程序开发问题。 如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)