BC 322 BC 322 Computer Programming 11 Sorting
BC 322 ครงท การเรยงลำดบขอมล BC 322 : Computer Programming 11 (Sorting) 1
การเรยงลำดบ (Sorting) • Bubble sort • Selection sort BC 322 : Computer Programming 2
������ i=0 [0] 7 BC 322 : Computer Programming Bubble Sort [1] 4 [2] [3] [4] 1 9 2 5
������ i=1 [0] BC 322 : Computer Programming Bubble Sort [1] [2] [3] [4] 6
������ i=2 [0] BC 322 : Computer Programming Bubble Sort [1] [2] [3] [4] 7
������ i=3 [0] Bubble Sort [1] [2] [3] [4] ผลลพธ BC 322 : Computer Programming 8
EX 1. การเรยงจากนอยไปมาก #include <stdio. h> void main() { int i, temp, round; int x[5]={9, 7, 5, 3, 1}; printf("The initialized arrayn"); for(i=0; i<5; i++) printf("%d", x[i]); printf("n"); for(round=0; round<5 -1; round++) for(i=0; i<5 -1 -round; i++) { if(x[i]>x[i+1]) { temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; } } printf("The ascending-sorted arrayn"); for(i=0; i<5; i++) printf("%d", x[i]); printf("n"); BC 322 : Computer Programming } (Bubble) ผลร น 9
EX 2. การเรยงจากมากไปนอย #include <stdio. h> void main() { int i, temp, round; int x[5]={9, 7, 5, 3, 1}; printf("The initialized arrayn"); for(i=0; i<5; i++). . . . printf("n"); for(. . . . . ) for(i. . . . ) { if(. . . ) {. . . . . . . . } } printf("The descending-sorted arrayn"); for(i=0; i<5; i++) printf("%d", x[i]); printf("n"); BC 322 : Computer Programming } (Bubble) ผลร น 10
Solution EX 3 BC 322 : Computer Programming 12
Solution EX 3(ตอ ) BC 322 : Computer Programming 13
รอบท Selection Sort i=0 [0] [1] [2] [3] [4] 7 4 1 9 2 BC 322 : Computer Programming 16
รอบท Selection Sort i=1 [0] [1] [2] [3] [4] * BC 322 : Computer Programming 17
รอบท BC 322 : Computer Programming Selection Sort i=2 [0] [1] * * [2] [3] [4] 18
รอบท ผลลพธ Selection Sort i=3 [0] [1] [2] * * * BC 322 : Computer Programming [3] [4] 19
EX 4. การเรยงจากนอยไปมาก #include <stdio. h> void main() { int i, j, min, temp, x[5]; for(i=0; i<5; i++) { printf("Enter num: "); scanf("%d", &x[i]); } printf("The initialized arraynn"); for(i=0; i<5; i++) printf("%d ", x[i]); printf("nnn"); for (round= 0; round < 5 -1; i++) { min = round; for (i = round+1; i < 5; ij++) { if (x[i] < x[min]) min = i; } temp = x[i]; x[i] = x[min]; x[min] = temp; } printf("The ascending-sorted arraynn"); for(i=0; i<5; i++) printf("%d ", x[i]); printf("nnn"); BC 322 } : Computer Programming (Selection ผลร น 20
��������� ��� 5 ��������� ตวอยางการรบคาและแสดงผล Please enter charater 1: F � Please enter charater 2: A Please enter charater 3: X Please enter charater 4: M Please enter charater 5: S ============ Sort by descending … XSMFA ������ BC 322 : Computer Programming 21
Ex 5 จงเขยนโปรแกรมเพอรบอาย และเกรดเฉลยของนกศกษา 5 คน แลวเรยงขอมลทงหมดตาม อายจากนอยไปหามาก ตวอยางผลรน Summary of student data… Student 1 Please enter age : 20 Please enter weight : 49. 8 ========== Student 2 Please enter age : 18 Please enter weight : 50. 5 ========== Student 3 Please enter age : 22 Please enter weight : 60. 9 ========== Student 4 Please enter age : 17 Please enter weight : 58. 4 ========== Student 5 Please enter age : 21 Please enter weight : 56. 0 BC 322========== : Computer Programming ======== Age Weight ======== 17 58. 4 18 50. 5 20 49. 8 21 56. 0 22 60. 9 ======== 22
- Slides: 23