Advertisement
PreveNtion

Sort

May 16th, 2018
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. void main ()
  4. {
  5.  
  6.     const int LENGTH = 5;
  7.  
  8.     int num[LENGTH] = {1000,100,300,200,600};
  9.     int temp;
  10.  
  11.     std::string str{LENGTH} = {"国語","英語","現社","おばお","あほ"}
  12.     std::string stTemp;
  13.  
  14.     std::cout << "ソート前 - > ";
  15.     for(int i = 0; i < LENGTH;i++)
  16.     {
  17.         std::cout<< num[i];
  18.         if(i < LENGTH - 1) std::cout <<":";
  19.     }
  20.     std::cout << std::endl;
  21.  
  22.     //ソート
  23.     for(int i = 0; i < LENGTH;i++)
  24.     {
  25.         for(int j = LENGTH - 1; j >= i; j--)
  26.         {
  27.             if(num[j - 1] > num[j])
  28.             {
  29.                 //数字のソート
  30.                 temp = num[j - 1];
  31.                 num[j - 1] = num[j];
  32.                 num[j] = temp;
  33.                
  34.                 //文字のソート
  35.                 stTemp = str[j - 1];
  36.                 str[j-1] = str[j];
  37.                 str[j] = stTemp;
  38.             }
  39.         }
  40.     }
  41.  
  42.     std::cout << "ソート後 - > ";
  43.     for(int i = 0; i < LENGTH;i++)
  44.     {
  45.         std::cout<< num[i];
  46.         if(i < LENGTH - 1) std::cout <<":";
  47.     }
  48.     std::cout << std::endl;
  49.  
  50.     return;
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement