溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C 語(yǔ)言 排序算法,冒泡排序,選擇排序,插入排序,二分查找

發(fā)布時(shí)間:2020-07-10 07:09:58 來(lái)源:網(wǎng)絡(luò) 閱讀:274 作者:990487026 欄目:開(kāi)發(fā)技術(shù)




排序算法,冒泡排序,選擇排序,插入排序


chunli@CentOS/tmp/work$ cat sort.c 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int bubble_sort(int array[], int size)
{
    if(NULL == array || size <2)
    {
        return -1;
    }
    int i = 0;
    int j = 0;
    for(i = 0; i < size; i++)
    {
        for(j = 0; j < size; j++)
        {
            if(array[i] < array[j])
            {
                int t = array[i];
                array[i] = array[j];
                array[j] = t;
            }
        }
    }
    return 0;
}

int select_sort(int array[], int size)
{
    if(NULL == array || size <2)
    {
        return -1;
    }
    int i = 0;
    int j = 0;
    int index = 0;
    for(i=0; i<size-1; i++)
    {
        index = i;
        for(j=i+1;j<size;j++)
        {
            if(array[j] < array[index])
            {
                index = j;
            }
        }
        int tmp = array[i];
        array[i] = array[index];
        array[index] = tmp;
    }
    return 0;
}

int insert_sort(int array[], int size)
{
    if(NULL == array || size <2)
    {
        return -1;
    }
    int i = 0;
    int j = 0;
    int t = 0;
    for(i=1; i<size; i++)
    {
        if(array[i-1] > array[i])
        {
            t = array[i];
            j = i;
            while(j>0 && array[j-1]>t)
            {
                array[j] = array[j-1];
                j--;
            } 
            array[j] = t;  
        }
    }
    return 0;
}

int main()
{
    srand((int)time(NULL));
    int array[30];
    for(int i=0;i<sizeof(array)/sizeof(int);i++) array[i] = rand()%100;
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");
    bubble_sort(array,sizeof(array)/sizeof(int));
    //select_sort(array,sizeof(array)/sizeof(int));
    //insert_sort(array,sizeof(array)/sizeof(int));
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");
    return 0;
}
chunli@CentOS/tmp/work$ gcc sort.c -Wall -std=c99 && ./a.out 
35 05 33 14 81 51 22 18 09 17 57 83 10 03 04 82 92 84 72 28 48 01 74 06 81 94 44 62 56 08 
01 03 04 05 06 08 09 10 14 17 18 22 28 33 35 44 48 51 56 57 62 72 74 81 81 82 83 84 92 94 
chunli@CentOS/tmp/work$





二分查找:

chunli@CentOS/tmp/work$ cat sort.c 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int bubble_sort(int array[], int size)
{
    if(NULL == array || size <2)
    {
        return -1;
    }
    int i = 0;
    int j = 0;
    for(i = 0; i < size; i++)
    {
        for(j = 0; j < size; j++)
        {
            if(array[i] < array[j])
            {
                int t = array[i];
                array[i] = array[j];
                array[j] = t;
            }
        }
    }
    return 0;
}

int binaray(int array[], int size, int key)
{
    int low = 0;
    int high = size;
    int mid = 0;
    while(low <= high)
    {
        mid = (high+low)/2;
        if(key == array[mid])
        {
            return mid;
        }
        else if(key > array[mid])
        {
            low = mid+1; 
        }
        else
        {
            high = mid-1;
        }
    }
    return -1;
}
 

int main()
{
    srand((int)time(NULL));
    int array[30];
    int ret = 0;
    for(int i=0;i<sizeof(array)/sizeof(int);i++) array[i] = rand()%100;
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");
    bubble_sort(array,sizeof(array)/sizeof(int));
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");

    ret = binaray(array, sizeof(array)/sizeof(int), 16);
    printf("查找值%d\n",ret);
    
    return 0;
}
chunli@CentOS/tmp/work$ gcc sort.c -Wall -std=c99 && ./a.out 
39 98 53 33 01 15 53 66 69 83 32 37 05 40 34 65 67 64 77 62 30 38 70 11 54 73 58 85 67 79 
01 05 11 15 30 32 33 34 37 38 39 40 53 53 54 58 62 64 65 66 67 67 69 70 73 77 79 83 85 98 
查找值-1
chunli@CentOS/tmp/work$




二分查找,遞歸實(shí)現(xiàn)


chunli@CentOS/tmp/work$ cat sort.c 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int bubble_sort(int array[], int size)
{
    if(NULL == array || size <2)
    {
        return -1;
    }
    int i = 0;
    int j = 0;
    for(i = 0; i < size; i++)
    {
        for(j = 0; j < size; j++)
        {
            if(array[i] < array[j])
            {
                int t = array[i];
                array[i] = array[j];
                array[j] = t;
            }
        }
    }
    return 0;
}

int binaray(int array[], int IndexLow, int IndexHigh, int key)
{
    if(NULL == array)
    {
        return -1;
    }

    if(IndexLow <= IndexHigh)
    {
        int IndexMid = (IndexLow + IndexHigh)/2;
        if(array[IndexMid] == key)
        {
            return IndexMid;
        }
        else if(key > array[IndexMid])
        {
            return binaray(array, IndexMid+1, IndexHigh, key);
        }
        else 
        {
            return binaray(array, IndexLow, IndexMid-1, key);
        }
    }
    return -1;
}


int main()
{
    srand((int)time(NULL));
    int array[31];
    int ret = 0;
    for(int i=0;i<sizeof(array)/sizeof(int);i++) array[i] = rand()%100;
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");
    bubble_sort(array,sizeof(array)/sizeof(int));
    for(int i=0;i<sizeof(array)/sizeof(int);i++) printf("%02d ", array[i]);    printf("\n");

    printf("-------\n");
    ret = binaray(array, 0, sizeof(array)/sizeof(int)-1, 6);    printf("index=%d\n",ret);
    ret = binaray(array, 0, sizeof(array)/sizeof(int)-1, 99);   printf("index=%d\n",ret);
    return 0;
}
chunli@CentOS/tmp/work$ gcc -g sort.c -Wall -std=c99 && ./a.out 
90 09 13 02 15 37 67 45 18 28 99 68 61 53 70 93 94 41 21 14 64 67 31 25 78 12 33 03 52 84 64 
02 03 09 12 13 14 15 18 21 25 28 31 33 37 41 45 52 53 61 64 64 67 67 68 70 78 84 90 93 94 99 
-------
index=-1
index=30
chunli@CentOS/tmp/work$


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI