單連結串列的排序(插入,選擇,冒泡)

Mr.路痴發表於2020-12-20

1.插入排序

連結串列的建立等系列操作程式碼詳見單連結串列實現

void Insert_Sort(LinkList &L) {  // 插入排序,變更結點連線

    Node *p = L->next, *head;  // p指標指向每次要插入的結點,head指標用於找到插入的位置
    Node *temp = p->next;      // temp指標 指向 p指標後繼結點,避免斷鏈
    p->next = NULL;            // 構造一個結點的有序表
    p = temp;                  

    while( p!= NULL ) {

        temp = p->next;        // 儲存 p指標後繼結點
        head = L;              
        while ( head->next != NULL && p->data > head->next->data)
            head = head->next;         // 有序表中查詢插入的位置
        p->next = head->next;          
        head->next = p;
        p = temp;
    }
}

2.選擇排序

void Select_Sort1(LinkList &L){  // 選擇排序,變更結點連線

    Node *head = L, *p = head->next;  
    head->next = NULL;   

    for ( ; p!=NULL; ) {

        Node *minn = p, *min_pre;   // *minn指向最小值結點, *min_pre指向*minn前一個,便於刪除操作

        for ( Node *q = p; q->next!=NULL; q = q->next) {

            if ( minn->data > q->next->data ) {
                minn = q->next;
                min_pre = q;
            }
        }
        if( minn!=p) {  

            Node *r = minn;
            min_pre->next = minn->next;  // 在當前連結串列中刪除minn結點
            r->next = NULL;
            head->next = r;              // 將minn結點插入到新有序表最後一位
            head = head->next;
        }
        else {
            p = p->next;       //只有當minn==p,將p插入有序表時才需將p指向後繼結點
            minn->next = NULL;
            head->next = minn;
            head = head->next;
        }
    }
}
void Select_Sort2(LinkList &L){     // 選擇排序,交換結點資料

    for ( Node *p = L->next; p->next!=NULL; p = p->next) {

        Node *minn = p;
        for ( Node *q = p->next; q!=NULL; q = q->next) {

            if( minn->data > q->data ) {
                minn = q;
            }
        }
        if( minn != p ) {
            ElemType t = minn->data;
            minn->data = p->data;
            p->data = t;
        }
    }
}

3.氣泡排序

void Bubble_Sort(Node *head){   // 氣泡排序,交換結點資料

	for ( Node *temp = head->next; temp->next != NULL; temp = temp->next ){

		for ( Node *p = head->next; p->next != NULL; p = p->next){

			if ( p->data > p->next->data ) {

				ElemType t = p->data;
				p->data = p->next->data;
				p->next->data = t;
			}
		}
	}
}

實現

#include<stdio.h>
#include<malloc.h>
#include<time.h>
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct node{
    ElemType data;
    node *next;
}Node,*LinkList;

Status CreatList(LinkList &L,int n){//建立連結串列
    LinkList p,q;
    int i;
    L = (LinkList)malloc(sizeof(Node));
    q =L;
    for(int i=0; i<n; i++){
        p = (LinkList)malloc(sizeof(Node));
        scanf("%d",&p->data);
        q->next = p;
        q = p;
    }
    q->next = NULL;
    return OK;
}

void ListTraverse(LinkList L){//遍歷連結串列
    if(L->next == NULL)
        printf("LinkList is empty!!!");
    else{
        LinkList p;
        p = L->next;
        while(p!= NULL){
            printf("%d ",p->data);
            p = p->next;
        }
        printf("\n");
    }
}

void Bubble_Sort(Node *head) {  //氣泡排序,交換結點資料

	for ( Node *temp = head->next; temp->next != NULL; temp = temp->next ){

		for ( Node *p = head->next; p->next != NULL; p = p->next){

			if ( p->data > p->next->data ) {

				ElemType t = p->data;
				p->data = p->next->data;
				p->next->data = t;
			}
		}
	}
}

void Select_Sort2(LinkList &L){  // 選擇排序,交換結點資料

    for ( Node *p = L->next; p->next!=NULL; p = p->next) {

        Node *minn = p;
        for ( Node *q = p->next; q!=NULL; q = q->next) {

            if( minn->data > q->data ) {
                minn = q;
            }
        }
        if( minn != p ) {
            ElemType t = minn->data;
            minn->data = p->data;
            p->data = t;
        }
    }
}

void Select_Sort1(LinkList &L){  // 選擇排序,變更結點連線

    Node *head = L, *p = head->next;

    for ( ; p!=NULL;) {

        Node *minn = p, *min_pre;

        for ( Node *q = p; q->next!=NULL; q = q->next) {

            if( minn->data > q->next->data ) {
                minn = q->next;
                min_pre = q;
            }
        }
        if( minn!=p) {

            Node *r = minn;
            min_pre->next = minn->next;
            r->next = NULL;
            head->next = r;
            head = head->next;
        }
        else {
            p = p->next;
            minn->next = NULL;
            head->next = minn;
            head = head->next;
        }
    }
}

void Insert_Sort(LinkList &L) {  // 插入排序,變更結點連線
    Node *p = L->next, *head;
    Node *temp = p->next;
    p->next = NULL;
    p = temp;

    while( p!= NULL ) {

        temp = p->next;
        head = L;
        while ( head->next != NULL && p->data > head->next->data)
            head = head->next;
        p->next = head->next;
        head->next = p;
        p = temp;
    }
}

int main(){
    LinkList L;
    printf("輸入10個數:\n");
    CreatList(L,10);
    printf("初始化10個元素,分別是:\n");
    ListTraverse(L);


    Select_Sort1(L);
    printf("升序排序輸出連結串列:\n");
    ListTraverse(L);

    return OK;
}
 // 23 12 13 44 55 21 89 76 67 9

相關文章