fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void selectionSort(char x[10][20], int n) {
  5. for (int i = 0; i < n - 1; i++) {
  6. int m = i; // Giả sử vị trí nhỏ nhất là i
  7. for (int j = i + 1; j < n; j++) {
  8. if (strcmp(x[j], x[m]) < 0) {
  9. m = j; // Cập nhật vị trí nhỏ nhất
  10. }
  11. }
  12. // Đổi chỗ x[i] và x[m] nếu tìm thấy phần tử nhỏ hơn
  13. if (m != i) {
  14. char tmp[20];
  15. strcpy(tmp, x[m]);
  16. strcpy(x[m], x[i]);
  17. strcpy(x[i], tmp);
  18. }
  19. }
  20. }
  21.  
  22. void show(char x[10][20], int n) {
  23. for (int i = 0; i < n; i++) {
  24. cout << x[i] << "\t";
  25. }
  26. cout << endl;
  27. }
  28.  
  29. int main() {
  30. char x[10][20] = {"John", "Wen", "Ozil", "Thor", "Merci", "Adam", "Dany", "Terry", "Henry", "Ronal"};
  31.  
  32. cout << "Danh sach ban dau: " << endl;
  33. int n = 10;
  34. show(x, n);
  35.  
  36. selectionSort(x, n);
  37.  
  38. cout << "Danh sach sau khi sap xep: " << endl;
  39. show(x, n);
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Danh sach ban dau: 
John	Wen	Ozil	Thor	Merci	Adam	Dany	Terry	Henry	Ronal	
Danh sach sau khi sap xep: 
Adam	Dany	Henry	John	Merci	Ozil	Ronal	Terry	Thor	Wen