1. 程式人生 > >隨機數生成與排序

隨機數生成與排序

程式碼描述:生成隨機數後排序,並對比排序前後數的位置
編譯環境:keil uVision5
開發板:PC2348


#include <stdlib.h>
#include <stdio.h>
#include "LPC23xx.H"
#define NUM 100

unsigned int data[NUM];
unsigned int position[NUM];

void sort()
{
    unsigned int i,j,temp;
    for(i=0;i<NUM-1;i++)
        for(j=i+1;j<NUM;j++)
    {
        if
(data[i]>data[j]) { temp=data[i]; data[i]=data[j]; data[j]=temp; temp=position[i]; position[i]=position[j]; position[j]=temp; } } } int main (void) { unsigned int i; for(i=0;i<NUM;i++) { data[i]=rand()%(NUM+1
); } for(i=0;i<NUM;i++) { position[i]=i; } sort(); while (1); }