直接插入排序
#include<stdio.h>
#include<iostream>
using namespace std;
void InsertSort(int A[],int n){
int i,j,temp;
for(i=1;i<n;i++){
if(A[i]<A[i-1]){
temp=A[i];
for(j=i-1;j>=0&&A[j]>temp;j--){
A[j+1]=A[j];
}
A[j+1]=temp;
}
}
}
int main(){
int A[10]={54,78,35,12,99,3,65,44,32,27};
int n=sizeof(A)/sizeof(A[0]);
for(int i=0;i<n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
InsertSort(A,n);
for(int j=0;j<n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
折半插入排序
#include<stdio.h>
#include<iostream>
using namespace std;
void InsertSort(int A[],int n){
int i,j,temp;
int low,high,mid;
for(i=1;i<n;i++){
temp=A[i];
low=0;
high=i-1;
while(low<=high){
mid=(low+high)/2;
if(A[mid]>temp){
high=mid-1;
}
else{
low=mid+1;
}
}
for(j=i-1;j>=0&&j>=high+1;j--){
A[j+1]=A[j];
}
A[high+1]=temp;
}
}
int main(){
int A[10]={47,78,50,12,99,3,6,44,32,27};
int n=sizeof(A)/sizeof(A[0]);
for(int i=0;i<n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
InsertSort(A,n);
for(int j=0;j<n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
希尔排序
#include<stdio.h>
#include<iostream>
using namespace std;
void ShellSort(int A[],int n){
int d,i,j;
for(d=n/2;d>=1;d=d/2){
for(i=1+d;i<=n;++i){
if(A[i]<A[i-d]){
A[0]=A[i];
for(j=i-d;j>0&&A[0]<A[j];j=j-d){
A[j+d]=A[j];
}
A[j+d]=A[0];
}
}
}
}
int main(){
int A[15]={0,44,25,61,47,17,78,50,12,99,3,6,44,32,27};
int n=14;
for(int i=1;i<=n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
ShellSort(A,n);
cout<<"希尔排序"<<endl;
for(int j=1;j<=n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
冒泡排序
#include<stdio.h>
#include<iostream>
using namespace std;
void swap(int &a,int &b){
int temp;
temp=a;
a=b;
b=temp;
}
void BubbleSort(int A[],int n){
bool flag=false;
for(int i=n-2;i>=0;i--){
flag=false;
for(int j=0;j<=i;j++){
if(A[j]>A[j+1]){
swap(A[j],A[j+1]);
flag=true;
}
}
if(flag==false){
return;
}
}
}
int main(){
int A[10]={45,25,86,78,91,3,65,14,36,55};
int n=sizeof(A)/sizeof(A[0]);
for(int i=0;i<n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
BubbleSort(A,n);
cout<<"冒泡排序"<<endl;
for(int j=0;j<n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
快速排序
#include<stdio.h>
#include<iostream>
using namespace std;
int Partition(int A[],int low,int high){
int pivot=A[low];
while(low<high){
while(low<high&&A[high]>pivot){
high--;
}
A[low]=A[high];
while(low<high&&A[low]<pivot){
low++;
}
A[high]=A[low];
}
A[low]=pivot;
return low;
}
void QuickSort(int A[],int low,int high){
if(low<high){
int pivotposition=Partition(A,low,high);
QuickSort(A,low,pivotposition-1);
QuickSort(A,pivotposition+1,high);
}
}
int main(){
int A[12]={15,27,86,71,91,3,65,14,49,98,36,55};
int n=sizeof(A)/sizeof(A[0]);
for(int i=0;i<n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
QuickSort(A,0,n-1);
cout<<"快速排序"<<endl;
for(int j=0;j<n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
简单选择排序
#include<stdio.h>
#include<iostream>
using namespace std;
void swap(int &a,int &b){
int temp;
temp=a;
a=b;
b=temp;
}
void SelectionSort(int A[],int n){
for(int i=0;i<n-1;i++){
int min=i;
for(int j=i+1;j<n;j++){
if(A[j]<A[min]){
min=j;
}
}
if(min!=i){
swap(A[min],A[i]);
}
}
}
int main(){
int A[12]={14,24,86,71,88,5,65,14,49,97,36,55};
int n=sizeof(A)/sizeof(A[0]);
for(int i=0;i<n;i++){
cout<<"\t"<<A[i];
}
cout<<endl;
SelectionSort(A,n);
cout<<"简单选择排序"<<endl;
for(int j=0;j<n;j++){
cout<<"\t"<<A[j];
}
cout<<endl;
return 0;
}
|