公司員工的年齡排序

來源:互聯網
上載者:User

取值範圍一定(0-100),且遠小於記錄數的排序(可能上萬)。

這是如果可以使用O(n)的輔助空間,可以使時間複雜度降低到O(n)。

用age[10]類比年齡1-10。25個資料類比員工數。age數組記錄下標表示的年齡人數。統計完age相當於排好了序,然後寫入data數組就可以。

#include <stdio.h>#include <stdlib.h>void sort(int data[],int length){    int age[10],i,j,c = 0;        if(data == NULL || length <= 0)        return;    for(i = 0;i < 10;i++){        age[i] = 0;    }    for(i = 0;i < length;i++){        if(data[i] < 0 || data[i] > 10)            return;        age[data[i]] ++;    }    for(i = 0;i < 10;i++)        for(j = 0;j < age[i];j++)            data[c++] = i;}void main(){    int d[25] = {1,2,3,4,5,4,6,5,4,8,4,7,2,5,5,5,3,9,7,7,4,6,7,5,8},i;    sort(d,25);    for(i = 0;i < 25;i++)        printf("%d  ",d[i]);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.