日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

數(shù)據(jù)結(jié)構(gòu)之冒泡排序

作者:愛吃土豆的馬鈴薯21 更新時間: 2022-07-13 編程語言

冒泡排序算法的原理如下: 相鄰比較的元素。n個元素比較n-1次。

可以分為兩種比較方式。

第一種:重者沉,從第一個元素開始比較 ,如果第一個比第二個大,就交換他們兩個,否則,就不交換位置,然后再第二個比第三個比較大小···,依次向后比較,直到?jīng)]有任何一對數(shù)字需要比較。

第二種:輕者浮,從最后一個元素開始比較 ,如果第n個比第n-1個小,就交換他們兩個,否則,就不交換位置,然后再第n-1個比第n-2個比較大小···,依次向后比較,直到?jīng)]有任何一對數(shù)字需要比較。

具體代碼實現(xiàn)的方式有很多種

博主這里以順序表冒泡排序為例

//基于順序表的冒泡排序
#include <stdio.h>
#define LISTSIZE 100    // LISTSIZE 表示順序表可能的最大數(shù)據(jù)元素數(shù)目

/****** 順序表存儲結(jié)構(gòu) ******/
typedef struct
{
    int list[LISTSIZE];
    int length;
}SqList;

//初始化順序表
int InitList(SqList* L)   
{
    L->length = 0;
    return 1;
}

//求順序表表長
int ListLenth(SqList L)                
{
    return L.length;
}

//判斷順序表是否為空
int ListEmpty(SqList L)   
{
    if (L.length <= 0)
    {
        return 1;
    }
     return 0;
}

//向順序表插入元素 
int ListInsert(SqList* L, int pos, int item)
{         
    int i;

    if (L->length >= LISTSIZE) 
    {
        printf("順序表已滿,無法插入\n");
        return 0;
    }

    if (pos <= 0 || pos > L->length + 1)
    {                                // 檢查元素插入位置是否在順序表里
        printf("插入位置不合法\n");
        return 0;
    }

    for (i = L->length - 1; i >= pos - 1; i--)
    {                                // 移動數(shù)據(jù)元素
        L->list[i + 1] = L->list[i];
    }

    L->list[pos - 1] = item;        // 插入元素
    L->length++;                    // 表長加一

    return 1;
}

// 遍歷順序表
int TraverList(SqList L)
{
    int i;
    for (i = 0; i < L.length; i++)
    {
        printf("%d ", L.list[i]);
    }
    printf("\n");
    return 1;
}
// 交換位置函數(shù) ,可以不單獨寫出來,在用的時候再寫
void swap(SqList* L, int i, int j)
{
    int temp = L->list[i];
    L->list[i] = L->list[j];
    L->list[j] = temp;
}
// 冒泡排序之重者沉
//void BubbleSort(SqList* L)
//{
//    int i, j, over;
//    for (i = 0; i < L->length - 1; i++)
//    {
//        over = 1;
//        for (j = 0; j < L->length - i - 1; j++)
//        {
//            if (L->list[j] > L->list[j + 1])
//            {
//                swap(L, j, j + 1);
//                over = 0;
//            }
//        }
//        if (over)
//        {
//            break;
//        }
//        TraverList(*L);
//    }
//}
//冒泡排序之輕者浮
void BubbleSort(SqList* L)
{
    int i, j, over;
    for (i = 0; i < L->length - 1; i++)
    {
        over = 1;
        for (j = L->length - 1; j > 0; j--) // 這里需要注意j
        {
            if (L->list[j - 1] > L->list[j])
            {
                swap(L, j - 1, j);
                over = 0;
            }
        }
        if (over)
        {
            break;
        }
        TraverList(*L);
    }
}
int main()
{
    SqList L;
    int x;
    char ch;
    int pos = 1;
    InitList(&L);
    do
    {
        scanf_s("%d", &x);
        ListInsert(&L, pos++, x);
    } while ((ch = getchar()) != '\n');
    BubbleSort(&L);
    printf("The sorted List is\n");
    TraverList(L);
    return 0;
}

執(zhí)行代碼如圖

重者沉

輕者浮

?

?

原文鏈接:https://blog.csdn.net/smallcabbage12/article/details/125304250

欄目分類
最近更新