c語言實(shí)現(xiàn)冒泡排序

c語言冒泡排序的方法:

站在用戶的角度思考問題,與客戶深入溝通,找到岳西網(wǎng)站設(shè)計(jì)與岳西網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋岳西地區(qū)。

先選定第一個(gè)數(shù)字為最大再對數(shù)字兩兩進(jìn)行比較,得到兩者之間的最大值,依次比較。具體代碼實(shí)現(xiàn)如下:

#include <iostream>
#include <time.h>
using namespace std;
void srandData(int *, int );//產(chǎn)生隨機(jī)數(shù)的函數(shù)
void bubbleSort(int *, int );//冒泡排序具體實(shí)現(xiàn)函數(shù)
void swap(int *, int *);//兩個(gè)數(shù)字實(shí)現(xiàn)交換的函數(shù)
void display(int *, int );//在屏幕輸出結(jié)果函數(shù)
int main()
{
const int N = 10;//定義常數(shù)
int arr[N];//定義數(shù)組
srandData(arr, N);
bubbleSort(arr, N);
display(arr, N);
return 0;
}
void srandData(int *a, int n)
{
srand(time(NULL));
for(int i = 0; i < n; i++)
{
a[i] = rand() % 50;//取50以下的數(shù)字
cout << a[i] << " ";
}
cout << endl;
}
void swap(int *b, int *c)
{
int temp = *c;
*c = *b;
*b = temp;
}
void bubbleSort(int *a, int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n - i - 1; j++)
{
if(a[j] < a[j + 1])
{
swap(&a[j], &a[j + 1]);//兩者交換
}
}
}
}
void display(int *d, int n)
{
for(int i = 0; i < n; i++)
{
cout << d[i] << " ";
}
cout << endl;
}

以上就是c語言冒泡排序怎樣實(shí)現(xiàn)從大到小的詳細(xì)內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!

當(dāng)前標(biāo)題:c語言實(shí)現(xiàn)冒泡排序
網(wǎng)站鏈接:http://bm7419.com/article36/phodpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、用戶體驗(yàn)、定制開發(fā)、Google商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)