CODE NỔI BỌT CHUẨN
#include <stdio.h>
02 #include <stdlib.h>
03
04 void swap(int *x,int *y)
05 {
06 int temp;
07 temp = *x;
08 *x = *y;
09 *y = temp;
10 }
11 void bublesort(int list[], int n)
12 {
13 int i,j;
14 for(i=0;i<(n-1);i++)
15 for(j=0;j<(n-(i+1));j++)
16 if(list[j] > list[j+1])
17 swap(&list[j],&list[j+1]);
18 }
19
20 void printlist(int list[],int n)
21 {
22 int i;
23 for(i=0;i<n;i++)
24 printf("%d\t",list[i]);
25 }
26
27
28 void main()
29 {
30 const int MAX_ELEMENTS = 10;
31 int list[MAX_ELEMENTS];
32
33 int i = 0;
34
35 // generate random numbers and fill them to the list
36 for(i = 0; i < MAX_ELEMENTS; i++ ){
37 list[i] = rand();
38 }
39 printf("The list before sorting is:\n");
40 printlist(list,MAX_ELEMENTS);
41
42 // sort the list
43 bublesort(list,MAX_ELEMENTS);
44
45 // print the result
46 printf("The list after sorting using bubble sorting algorithm:\n");
47 printlist(list,MAX_ELEMENTS);
48 }
#include <stdio.h>
02 #include <stdlib.h>
03
04 void swap(int *x,int *y)
05 {
06 int temp;
07 temp = *x;
08 *x = *y;
09 *y = temp;
10 }
11 void bublesort(int list[], int n)
12 {
13 int i,j;
14 for(i=0;i<(n-1);i++)
15 for(j=0;j<(n-(i+1));j++)
16 if(list[j] > list[j+1])
17 swap(&list[j],&list[j+1]);
18 }
19
20 void printlist(int list[],int n)
21 {
22 int i;
23 for(i=0;i<n;i++)
24 printf("%d\t",list[i]);
25 }
26
27
28 void main()
29 {
30 const int MAX_ELEMENTS = 10;
31 int list[MAX_ELEMENTS];
32
33 int i = 0;
34
35 // generate random numbers and fill them to the list
36 for(i = 0; i < MAX_ELEMENTS; i++ ){
37 list[i] = rand();
38 }
39 printf("The list before sorting is:\n");
40 printlist(list,MAX_ELEMENTS);
41
42 // sort the list
43 bublesort(list,MAX_ELEMENTS);
44
45 // print the result
46 printf("The list after sorting using bubble sorting algorithm:\n");
47 printlist(list,MAX_ELEMENTS);
48 }