Advertisement
gha890826

Selection sort

Apr 14th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void sort(char* sort_a,int num)
  5. {
  6.     for(int i=0;i<num;i++)
  7.     {
  8.         for(int j=i+1;j<num;j++)
  9.         {
  10.             if(sort_a[i]>sort_a[j])
  11.             {
  12.                 char temp;
  13.                 temp=sort_a[i];
  14.                 sort_a[i]=sort_a[j];
  15.                 sort_a[j]=temp;
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     char n[5]={'s','o','n','c','i'};
  24.     cout<<"n[] before sort: ";
  25.     for(int i=0;i<5;i++)
  26.     {
  27.         cout<<n[i];
  28.     }
  29.     cout<<endl;
  30.     sort(n,5);
  31.     cout<<"after sorted:";
  32.     for(int i=0;i<5;i++)
  33.     {
  34.         cout<<n[i];
  35.     }
  36.     cout<<endl;
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement