SELECTION- SẮP XẾP CHỌN - CODE C CHUẨN
http://cprogramminglanguage.net/selection-sort-algorithm-c-souce-code.aspx
Selection sort is a simplicity sorting algorithm. It works as its name as it is. Here are basic steps of selection sort algorithm:
1. Find the minimum element in the list
2. Swap it with the element in the first position of the list
3. Repeat the steps above for all remainder elements of the list starting at the second position.
Here is the selection sort algorithm implemented in C programming language:
view source
print?
01 #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
12 void selection_sort(int list[], int n)
13 {
14 int i, j, min;
15
16 for (i = 0; i < n - 1; i++)
17 {
18 min = i;
19 for (j = i+1; j < n; j++)
20 {
21 if (list[j] < list[min])
22 {
23 min = j;
24 }
25 }
26 swap(&list[i], &list[min]);
27 }
28 }
29
30 void printlist(int list[],int n)
31 {
32 int i;
33 for(i=0;i<n;i++)
34 printf("%d\t",list[i]);
35 }
36
37 void main()
38 {
39 const int MAX_ELEMENTS = 10;
40 int list[MAX_ELEMENTS];
41
42 int i = 0;
43
44 // generate random numbers and fill them to the list
45 for(i = 0; i < MAX_ELEMENTS; i++ ){
46 list[i] = rand();
47 }
48 printf("The list before sorting is:\n");
49 printlist(list,MAX_ELEMENTS);
50
51 // sort the list
52 selection_sort(list,MAX_ELEMENTS);
53
54 // print the result
55 printf("The list after sorting:\n");
56 printlist(list,MAX_ELEMENTS);
57 }
THAM KHẢO CODE QUICK SORT TẠI ĐÂY
http://www.comp.dit.ie/rlawlor/Alg_DS/sorting/quickSort.c
NOIM.INFO
http://cprogramminglanguage.net/selection-sort-algorithm-c-souce-code.aspx
» C Algorithms » C Selection Sort Algorithm
Selection Sort Algorithm
Sponsored Links
Selection Sort Algorithm
Sponsored Links
Selection sort is a simplicity sorting algorithm. It works as its name as it is. Here are basic steps of selection sort algorithm:
1. Find the minimum element in the list
2. Swap it with the element in the first position of the list
3. Repeat the steps above for all remainder elements of the list starting at the second position.
Here is the selection sort algorithm implemented in C programming language:
view source
print?
01 #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
12 void selection_sort(int list[], int n)
13 {
14 int i, j, min;
15
16 for (i = 0; i < n - 1; i++)
17 {
18 min = i;
19 for (j = i+1; j < n; j++)
20 {
21 if (list[j] < list[min])
22 {
23 min = j;
24 }
25 }
26 swap(&list[i], &list[min]);
27 }
28 }
29
30 void printlist(int list[],int n)
31 {
32 int i;
33 for(i=0;i<n;i++)
34 printf("%d\t",list[i]);
35 }
36
37 void main()
38 {
39 const int MAX_ELEMENTS = 10;
40 int list[MAX_ELEMENTS];
41
42 int i = 0;
43
44 // generate random numbers and fill them to the list
45 for(i = 0; i < MAX_ELEMENTS; i++ ){
46 list[i] = rand();
47 }
48 printf("The list before sorting is:\n");
49 printlist(list,MAX_ELEMENTS);
50
51 // sort the list
52 selection_sort(list,MAX_ELEMENTS);
53
54 // print the result
55 printf("The list after sorting:\n");
56 printlist(list,MAX_ELEMENTS);
57 }
THAM KHẢO CODE QUICK SORT TẠI ĐÂY
http://www.comp.dit.ie/rlawlor/Alg_DS/sorting/quickSort.c
NOIM.INFO