Take the bubble sort method as an example:
1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 intCreatdata (intArr[],intNintMinintmax)5 {6 intI,j,flag;7Srand (Time (NULL));//creat Data8 if(max-min+1<n)return 0;9 for(i=0; i<n;i++)Ten { One Do A { -Arr[i]= (max-min+1) *rand ()/(rand_max+1)+min; -flag=0; the for(j=0; j<i;j++) - { - if(arr[i]==Arr[j]) - { +flag=1; - } + } A } at while(flag); - } - return 1; - } - voidBubblesort (intA[],intN) - { in inti,j,t; - for(i=0; i<n;i++) to { + for(j=n-1; j>i;j--) - { the if(a[j-1]>A[j]) * { $t=a[j-1];Panax Notoginsenga[j-1]=A[j]; -a[j]=T; the } + } A /*printf ("%2d times:", i+1); the For (j=0;j<n;j++) + printf ("%d", a[j]); - printf ("\ n");*/ $ } $ } - intMain () - { the inti,a[6]; - for(i=0;i<6; i++)Wuyi { thea[i]=0; - Wu } - if(!creatdata (A,6,1, -)) About { $printf"generate random number not successful \ n"); - return 1; - } -printf"Original data:"); A for(i=0;i<6; i++) +printf"%d", A[i]); theprintf"\ n"); -Bubblesort (A,6); $printf"after sorting:"); the for(i=0;i<6; i++) theprintf"%d", A[i]); theprintf"\ n"); the return 0; -}
Generate non-repeating random number (C + +)