# C learning practice --- use Bubble sorting to return the descending order of a random number column.

Source: Internet
Author: User

C learning practice --- use Bubble sorting to return the descending order of a random number column.

`1/* use Bubble sorting to return the descending order of a column of random numbers, and record the time 2 */3 # include <stdio. h> 4 # include <time. h> 5 # include <stdlib. h> 6 # include <windows. h> 7 8 # define LEN 10000 // length of a column 9 # define RANGE 1000 // random number RANGE 0-100010 # define SLEEP_TIME 5000 // waiting time before sorting 11 12 int * get_rand_nums (); 13 int * get_decrease_nums (); 14 15 int main () {16 17 int * p, I, start_time, end_time; 18 19 start_time = time (NULL ); 20 21 p = get_decrease_nums (); 22 printf ("\ nSorted nums: \ n"); 23 for (I = 0; I <LEN; I ++) {24 printf ("% d,", * (p + I); 25} 26 27 end_time = time (NULL); 28 29 printf ("\ nStarting time: % d \ n ", start_time); 30 printf (" Ending time: % d \ n ", end_time); 31 printf (" Total time used: % d S ", end_time-start_time-SLEEP_TIME/1000); 32 printf ("\ a"); 33 return 0; 34} 35 36/* generate LEN random numbers, returns x/37 int * get_rand_nums () {38 39 static int nums [LEN]; 40 41 srand (unsigned) time (NULL); 42 int I; 43 printf ("Generate these nums: \ n"); 44 for (I = 0; I <LEN; I ++) {45 nums [I] = rand () % RANGE; 46 printf ("% d,", nums [I]); 47} 48 49 return nums; 50} 51 52/* use bubble sort to return a descending array */53 int * get_decrease_nums () {54 55 int * arr, I, j, temp; 56 57 arr = get_rand_nums (); 58 printf ("\ nSorting in 5 Senconds ..... "); 59 Sleep (SLEEP_TIME); 60 61 for (I = 0; I <LEN-1; I ++) {62 for (j = 0; j <(LEN-I-1); j ++) {63 if (* (arr + j + 1)> * (arr + j )) {64 temp = * (arr + j); 65 * (arr + j) = * (arr + j + 1); 66 * (arr + j + 1) = temp; 67} 68} 69} 70 71 return arr; 72}`

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## 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.