fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // int priority(int x){
  5. // if ( x > 0)
  6. // return 1 ;
  7. // else
  8. // return 0;
  9. // }
  10.  
  11. // bool compare(int x , int y ) {
  12. // if ( priority(x) == priority(y)){
  13. // if ( priority(x) == 1)
  14. // return x < y ;
  15. // else
  16. // return x > y ;
  17. // }
  18. // else
  19. // return ( priority(x) == priority(y));
  20. // }
  21.  
  22. // các số chia hết cho 3 đứng đầu,
  23. // rồi đến các số chia cho 3 dư 2 và cuối cùng là các số chia 3 dư 1
  24. int priority(int x){
  25. if ( x % 3 == 0)
  26. return 2 ;
  27. else if ( x % 3 == 2)
  28. return 1;
  29. else
  30. return 0;
  31. }
  32.  
  33. bool compare(int x , int y ) {
  34. return ( priority(x) > priority(y));
  35. }
  36.  
  37. void selectionSort( int a[] , int n ) {
  38. for ( int i = 0;i < n - 1;i++){
  39. int minIndex = i ;
  40. for ( int j = i + 1 ; j < n ; j++)
  41. if (compare(a[j] , a[minIndex])) {
  42. minIndex = j ;
  43. }
  44. swap(a[i] , a[minIndex]);
  45. }
  46. }
  47.  
  48.  
  49. int main() {
  50. // your code goes here
  51. int arr[] = { 5 , 4, 3 , 1 ,2 , 7,8,9,12,14,15 } ;
  52. selectionSort(arr,11);
  53. for ( int i = 0 ; i < 11 ; i++){
  54. cout << arr[i]<<" ";
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
3 9 12 15 2 8 5 14 7 4 1