Advertisement
hrustic_nihad

SORTING_BY_CHOICE_1D_ARRAY

Jan 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. // SORTING_ARRAY_by_choice_.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<iomanip>
  6. using namespace std;
  7. void unos(int[], int);
  8. void sortiranje(int[], int,int);
  9. void ispis(int[], int);
  10. int main()
  11. {
  12.     int niz[10], izbor;
  13.     unos(niz, 10);
  14.     do {
  15.         cout << "U kom poretku zelite sortirati dati niz ? Za opadajuci (1), za rastuci (2)" << endl;
  16.         cin >> izbor;
  17.     } while (izbor != 1 && izbor != 2);
  18.     sortiranje(niz, 10, izbor);
  19.     ispis(niz, 10);
  20.  
  21.     system("pause>0");
  22.     return 0;
  23. }
  24.  
  25. void unos(int niz[], int vel)
  26. {
  27.     for (int i = 0; i < vel; i++)
  28.     {
  29.         cin >> niz[i];
  30.     }
  31. }
  32.  
  33. void sortiranje(int niz[], int vel, int izbor)
  34. {
  35.     bool promena = true;
  36.     int temp;
  37.     while (promena)
  38.     {
  39.         promena = false;
  40.         for (int i = 0; i < vel - 1; i++)
  41.         {
  42.             if (niz[i]<niz[i + 1] && izbor == 1 || niz[i]>niz[i + 1] && izbor == 2)
  43.             {
  44.                 temp = niz[i];
  45.                 niz[i] = niz[i + 1];
  46.                 niz[i + 1] = temp;
  47.                 promena = true;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. void ispis(int niz[], int vel)
  54. {
  55.     for (int i = 0; i < vel; i++)
  56.     {
  57.         cout << niz[i] << "  ";
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement