Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7.  
  8. void bubbleSort(vector<int>& a)
  9. {
  10. bool swapp = true;
  11. while(swapp)
  12. {
  13. swapp = false;
  14. for (int i = 0; i < a.size()-1; i++)
  15. {
  16. if (a[i]>a[i+1] )
  17. {
  18. a[i] += a[i+1];
  19. a[i+1] = a[i] - a[i+1];
  20. a[i] -=a[i+1];
  21. swapp = true;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. void printArray(vector<int> a){
  28. cout << "\n Sortirano polje: \n";
  29. for (size_t i=0; i <a.size(); i++) {
  30. cout<<a[i]<<" ";
  31.  
  32. }
  33. cout<<endl;
  34. }
  35.  
  36. int main()
  37. {
  38. vector<int> polje;
  39.  
  40. ifstream inputFile("niz.txt");
  41.  
  42. if (inputFile.good()) {
  43.  
  44. int current_number = 0;
  45. while (inputFile >> current_number){
  46. polje.push_back(current_number);
  47. }
  48. inputFile.close();
  49.  
  50. }else {
  51. cout << "Error!";
  52. _exit(0);
  53. }
  54.  
  55. cout << "Ucitano polje: \n";
  56. printArray(polje);
  57. bubbleSort(polje);
  58. printArray(polje);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement