Advertisement
Centipede18

sortArraySelection

Apr 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void sortArraySelection(float a[], int n, int m)
  5. {
  6.     int i, j, minIndex, tmp;
  7.     for(i=0; i < n-1; i++)
  8.     {
  9.         minIndex = i;
  10.         for(int j = i+1; j < n; j++)
  11.         {
  12.             if(m == 1)
  13.             {
  14.                 if(a[j] < a[minIndex])
  15.                 {
  16.                     minIndex = j;
  17.                 }
  18.             }
  19.             else if(m == 0)
  20.             {
  21.                 if(a[j] > a[minIndex])
  22.                 {
  23.                     minIndex = j;
  24.                 }
  25.             }
  26.         }
  27.         if(minIndex != i)
  28.         {
  29.             tmp = a[i];
  30.             a[i] = a[minIndex];
  31.             a[minIndex] = tmp;
  32.         }
  33.     }
  34. }
  35.  
  36. main()
  37. {
  38.     float a[100005];
  39.     int n, m;
  40.     cin>>n>>m;
  41.     for(int i=0; i<n; i++) cin>>a[i];
  42.     sortArraySelection(a, n, m);
  43.     for(int i=0; i<n; i++)
  44.     {
  45.         cout<<a[i]<<' ';
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement