排序练习写了冒泡排序,插入排序,希尔排序,堆排序,归并排序(递归和非递归)
错误:
#include
#include
#define MaxSize 100005
void insert_sort(int a[],int N);
void swap(int *a,int *b);
void bubble_sort(int a[],int N);
void shell_sort(int a[],int N);
void HeapAdjust(int H[],int f,int l);
void heap_sort(int a[],int N);
void Merge(int A[],int tempA[],int L,int R,int Rend);
// void Msort(int A[],int tempA[],int L,int R);
void Merge_sort(int A[],int N);
void Merge_pass(int A[],int tempA[],int len,int N);
int main(){
int N;
scanf("%d",&N);
int i;
int a[100005] = {0};
int temp;
for(i=0;i<N;++i){
scanf("%d",&temp);
a[i] = temp;
}
// bubble_sort(a,N);
// insert_sort(a,N);
// shell_sort(a,N);
// heap_sort(a,N);
Merge_sort(a,N);
for(i=0;i<N;++i){
if(i){
printf(" ");
}
printf("%d",a[i]);
}
}
void swap(int *a,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort(int a[],int N){
int i,n,flag;
for(n=N-1;n>0;n--){
flag = 0;
for(i=0;i<n;i++){
if(a[i]>a[i+1]){
swap(&a[i],&a[i+1]);
flag = 1;
}
}
if(flag==0) break;
}
}
void insert_sort(int a[],int N){
int i,j,temp;
for(i=1;i<N;++i){
temp = a[i];
for(j=i-1;j>=0&&temp<a[j];--j){//forget j>0
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
void shell_sort(int a[],int N){
int i,j,temp,D;
for(D=N/2;D>=1;D/=2){
for(i=D;i<N;i+=D){
temp = a[i];
for(j=i-D;j>=0&&temp<a[j];j-=D){//forget j>0
a[j+D] = a[j];
}
a[j+D] = temp;
}
}
}
void HeapAdjust(int H[],int f,int e){
int temp = H[f];
int parent,child;
for(parent=f;parent*2+1<=e;parent=child){//在这里定义的i在循环外不能使用
child = 2*parent+1;
if(child+1<e&&H[child+1]>H[child]){
child++;
}
if(H[child]<temp) break;
else{
H[parent] = H[child];
}
}
H[parent]=temp;
}
void heap_sort(int a[],int N){
int i=N/2-1;
for(;i>=0;--i){
HeapAdjust(a,i,N);
}
for(int j=N-1;j>=0;--j){
swap(&a[0],&a[j]);
HeapAdjust(a,0,j);
}
}
void Merge(int A[],int tempA[],int L,int R,int Rend){
if (L>=Rend) return;
int count = Rend-L+1;
int temp = L;
int Lend = R-1;
while(L<=Lend&&R<=Rend){
if(A[L]>=A[R]) tempA[temp++] = A[R++];
else tempA[temp++] = A[L++];
}
while(R<=Rend){
tempA[temp++] = A[R++];
}
while(L<=Lend){
tempA[temp++] = A[L++];
}
}
void Merge_pass(int A[],int tempA[],int len,int N){
int i;
for(i=0;i+2*len<N;i+=2*len){
Merge(A,tempA,i,i+len,i+2*len-1);
}
if(i+len<N){
Merge(A,tempA,i,i+len,N-1);
}
else{
for(;i<N;++i){
tempA[i] = A[i];
}
}
}
void Merge_sort(int A[],int N){
int *tempA = (int*)malloc(N*sizeof(int));
int len = 1;
if(tempA){
while(1){
Merge_pass(A,tempA,len,N);
len = len*2;
Merge_pass(tempA,A,len,N);
len = len*2;
if(len>N) break;
}
free(tempA);//dont forget
}
}
递归方式归并排序如下:
void Merge(int A[],int tempA[],int L,int R,int Rend){
if (L>=Rend) return;
int count = Rend-L+1;
int temp = L;
int Lend = R-1;
while(L<=Lend&&R<=Rend){
if(A[L]>=A[R]) tempA[temp++] = A[R++];
else tempA[temp++] = A[L++];
}
while(R<=Rend){
tempA[temp++] = A[R++];
}
while(L<=Lend){
tempA[temp++] = A[L++];
}
for(int i=0;i<count;i++){
A[Rend] = tempA[Rend];// wrong used A[Rend--] = tempA[Rend];
Rend--;
}
}
void Msort(int A[],int tempA[],int L,int R){
int cen;
if(L<R){
cen = (L+R)/2;
Msort(A,tempA,L,cen);//wrong used Msort(A,tempA,0,cen);
Msort(A,tempA,cen+1,R);
Merge(A,tempA,L,cen+1,R);
}
}
void Merge_sort(int A[],int N){
int *tempA = (int*)malloc(N*sizeof(int));
if(tempA){
Msort(A,tempA,0,N-1);
free(tempA);//dont forget
}
else{
printf("空间不足");//wrong used else()
}
}
写的比较乱其实,这篇文章很清晰排序算法-09-排序1 排序 (25分)-第一部分