直接插入算法
方法一:
#include <iostream>
using namespace std;
//直接插入排序
void insertSort(int R[],int n){
int i,j;
for (i=2; i<=n; i++) {
//若i>=有序区中所有的数据,则i不动
if (R[i] < R[i-1]) {
//否则当前记录复制给哨兵
R[0] = R[i];
//找到要插入的位置
for (j=i-1; R[j]>R[0]; j--) {
//记录后移
R[j+1] = R[j];
}
//插入到该位置
R[j+1] = R[0];
}
}
}
int main(int argc, const char *argv[]){
int R[] = {0,20,15,39,48,1,88,100,99};
int k=sizeof(R)/sizeof(R[0]);
insertSort(R,k-1);
int i;
for(int i=1;i<k;i++){
cout << R[i] << " ";
}
}
//直接插入算法,使用了数组R 中第一个位置为临时变量。
方法二
#include<iostream>
using namespace std;
int main()
{
int a[]={98,76,109,34,67,190,80,12,14,89,1};
int k=sizeof(a)/sizeof(a[0]);
int i,j;
for(i=1;i<k;i++)//循环从第2个元素开始
{
if(a[i]<a[i-1])
{
int temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;//此处就是a[j+1]=temp;
}
}
for(int f=0;f<k;f++)
{
cout<<a[f]<<" ";
}
return 0;
}
冒泡排序
#include <iostream>
using namespace std;
//直接插入排序
void bubbleSort(int R[],int n){
int i,j;
for (i=1; i<=n; i++) {
bool flag = 0;
for (j=1; j<=n-i; j++) {
if (R[j]>R[j+1]) {
R[0] = R[j];
R[j] = R[j+1];
R[j+1] = R[0];
flag = 1;
}
}
if (flag == 0) {
return;
}
}
}
int main(int argc, const char *argv[]){
int R[] = {0,20,15,39,48,1,88,100,99};
int k=sizeof(R)/sizeof(R[0]);
bubbleSort(R,k-1);
int i;
for(int i=1;i<k;i++){
cout << R[i] << " ";
}
}
归并排序
#include <stdlib.h>
#include <stdio.h>
void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex)
{
int i = startIndex, j=midIndex+1, k = startIndex;
while(i!=midIndex+1 && j!=endIndex+1)
{
if(sourceArr[i] > sourceArr[j])
tempArr[k++] = sourceArr[j++];
else
tempArr[k++] = sourceArr[i++];
}
while(i != midIndex+1)
tempArr[k++] = sourceArr[i++];
while(j != endIndex+1)
tempArr[k++] = sourceArr[j++];
for(i=startIndex; i<=endIndex; i++)
sourceArr[i] = tempArr[i];
}
//内部使用递归
void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex)
{
int midIndex;
if(startIndex < endIndex)
{
midIndex = startIndex + (endIndex-startIndex) / 2;//避免溢出int
MergeSort(sourceArr, tempArr, startIndex, midIndex);
MergeSort(sourceArr, tempArr, midIndex+1, endIndex);
Merge(sourceArr, tempArr, startIndex, midIndex, endIndex);
}
}
int main(int argc, char * argv[])
{
int a[] = {50, 10, 20, 30, 70, 40, 80, 60};
int A_Len = sizeof(a)/sizeof(a[0]);
int i, b[A_Len];
MergeSort(a, b, 0, 7);
for(i=0; i<8; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
基数排序
PASS
不稳定排序
直接选择排序
#include <iostream>
using namespace std;
void selectSort(int R[],int n){
int i,j,k;
int temp;
for (i=1; i<n; i++) {
k=i;
for (j=i+1; j<=n; j++) {
if (R[j]<R[k]) {
k = j;
}
}
if (k!=i) {
temp = R[k];
R[k] = R[i];
R[i] = temp;
}
}
}
int main(int argc,const char *argv[]){
int R[] = {1,5,4,3,2};
int i,j;
j = sizeof(R)/sizeof(R[0]);
cout << j << endl;
selectSort(R,j-1);
for(i=0;i<j;i++)
cout << R[i] << " ";
}
#include<iostream>
using namespace std;
//快速排序,一次排序
int Partition(int R[],int i,int j){
int x = R[i];
while (i<j) {
while (i<j && R[j]>=x) {
j--;
}
if (i<j) {
R[i] = R[j];
i++;
}
while (i<j && R[i]<=x) {
i++;
}
if (i<j) {
R[j] = R[i];
j--;
}
}
R[i] = x;
return i;
}
//快速排序
void quickSort(int R[],int low,int high){
int p;
if (low < high) {
p = Partition(R, low, high);
quickSort(R, low, p-1);
quickSort(R, p+1, high);
}
}
int main(int argc, const char * argv[]) {
int R[9] = {0,8,7,3,9,1,4,6,5};
quickSort(R, 1, 8);
for (int i=1; i<=8; i++) {
printf("%2d ",R[i]);
}
cout << endl;
return 0;
}