408天勤链表插入排序与双气泡排序课后习题代码

双气泡排序

这个重难点是双指针,left与right,left++right–,什么时候left++什么时候right–。

void Bsort(int a[],int n){
    
    
    int right = n-1;
    int left = 0;
    int i,j;
    bool flag = true;
    while(flag ){
    
    
        flag = false;
        for(i=left;i<right;i++){
    
    
            if(a[i]>a[i+1]){
    
    
                int tmp = a[i];
                a[i] = a[i+1];
                a[i+1] = tmp;
                flag = true;
            }
        }
        --right;
        for(j = right;j>left;j--){
    
    
            if(a[j]<a[j-1]){
    
    
                int tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
                flag = true;
            }
        }
        ++left;
    }
}

调用方式;

int a[5] = {
    
    6,9,1,4,3};
Bsort(a,5);

链表直接插入排序

void CreateLink(LNode*& h,int a[],int n){
    
    
    int i;
    LNode* s,*t;
    h = (LNode*)malloc(sizeof(LNode));
    h->next = NULL;
    t = h;
    for(i=0;i<n;i++){
    
    
        s = (LNode*) malloc(sizeof(LNode));
        s->data = a[i];
        t->next = s;
        t= s;
    }
    t->next = NULL;
}

void Sort(LNode*&h){
    
    
    LNode* pre,*p,*q;
    q = h->next;
    h->next = NULL;
    while(q != NULL){
    
    
        pre = h;
        p = h->next;
        while(p!=NULL && p->data <= q->data){
    
    
            pre = p;
            p = p->next;
        }
        pre->next = q;
        q = q->next;
        pre->next->next = p;
    }

}

下面完整测试里,复制粘贴即可是测试样例。

完整测试

#include<iostream>
using namespace std;
void Print(int a[],int n){
    
    
    for(int i =0;i<n;i++)
        cout << a[i] << " ";
    cout << endl;
}

typedef struct LNode{
    
    
    int data;
    struct LNode* next;
}LNode;
void CreateLink(LNode*& h,int a[],int n){
    
    
    int i;
    LNode* s,*t;
    h = (LNode*)malloc(sizeof(LNode));
    h->next = NULL;
    t = h;
    for(i=0;i<n;i++){
    
    
        s = (LNode*) malloc(sizeof(LNode));
        s->data = a[i];
        t->next = s;
        t= s;
    }
    t->next = NULL;
}

void Sort(LNode*&h){
    
    
    LNode* pre,*p,*q;
    q = h->next;
    h->next = NULL;
    while(q != NULL){
    
    
        pre = h;
        p = h->next;
        while(p!=NULL && p->data <= q->data){
    
    
            pre = p;
            p = p->next;
        }
        pre->next = q;
        q = q->next;
        pre->next->next = p;
    }

}
void PrintL(LNode *p){
    
    
    LNode *q = p->next;
    while(q!=NULL){
    
    
        cout << q->data << " ";
        q = q->next;
    }
    cout << endl;
}
void Bsort(int a[],int n){
    
    
    int right = n-1;
    int left = 0;
    int i,j;
    bool flag = true;
    while(flag ){
    
    
        flag = false;
        for(i=left;i<right;i++){
    
    
            if(a[i]>a[i+1]){
    
    
                int tmp = a[i];
                a[i] = a[i+1];
                a[i+1] = tmp;
                flag = true;
            }
        }
        --right;
        for(j = right;j>left;j--){
    
    
            if(a[j]<a[j-1]){
    
    
                int tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
                flag = true;
            }
        }
        ++left;
    }
}
int main(){
    
    
	int a[5] = {
    
    6,9,1,4,3};
    Print(a,5);
    int b[5];
    LNode *h = (LNode *)malloc(sizeof(LNode));
    CreateLink(h,a,5);
    PrintL(h);
    Sort(h);
    PrintL(h);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37149062/article/details/125806349