rengetsu

3ND_Algoritmai_SelectionSort

May 15th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. //ISF_17/1. Robert Kropa, Raimond Silobrit, Pavel Trostianko
  2. //Selection Sort
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. int amount, ats, tmp,n[1000];
  12. string failas="test.txt", rez="rez.txt";
  13.  
  14. void swap(int *xp, int *yp)
  15. {
  16.     int temp = *xp;
  17.     *xp = *yp;
  18.     *yp = temp;
  19. }
  20.  
  21. void Didejimo()
  22. {
  23.     int i,j, min_idx;
  24.  
  25.     for (i = 0; i < amount-1; i++)
  26.     {
  27.         min_idx = i;
  28.         for (j = i+1; j < amount; j++)
  29.           if (n[j] < n[min_idx])
  30.             min_idx = j;
  31.  
  32.         swap(&n[min_idx], &n[i]);
  33.     }
  34.     ofstream fen(rez.c_str());
  35.     for(int ii=0;ii<amount;ii++)
  36.     {
  37.         fen << n[ii] << endl;
  38.     }
  39. }
  40.  
  41. void Mazejimo()
  42. {
  43.  
  44.     int i,j, min_idx;
  45.  
  46.     for (i = 0; i < amount-1; i++)
  47.     {
  48.         min_idx = i;
  49.         for (j = i+1; j < amount; j++)
  50.           if (n[j] > n[min_idx])
  51.             min_idx = j;
  52.  
  53.         swap(&n[min_idx], &n[i]);
  54.     }
  55.     ofstream fen(rez.c_str());
  56.     for(int ii=0;ii<amount;ii++)
  57.     {
  58.         fen << n[ii] << endl;
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     cout << "ISf-17/1 Selection Sort. Robert Kropa, Raimond Silobrit, Pavel Trostianko." << endl;
  65.     ifstream fin(failas.c_str());
  66.     fin >> amount;
  67.     for(int i=0;i<amount;i++)
  68.     {
  69.         fin >> n[i];
  70.     }
  71.     cout << "Iveskite 0 - jeigu norite isvesti elementus didejimo tvarka, 1 - jeigu mazejimo." << endl;
  72.     cin >> ats;
  73.     if(ats==0){Didejimo();}
  74.     else if(ats==1){Mazejimo();}
  75.     else{cout<<"Error!"<<endl;}
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment