Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. // Функция сортировки прямым обменом (метод "пузырька")
  5. void bubbleSort(int *num, int size)
  6. {
  7. // Для всех элементов
  8. for (int i = 0; i < size - 1; i++)
  9. {
  10. for (int j = (size - 1); j > i; j--) // для всех элементов после i-ого
  11. {
  12. if (num[j - 1] < num[j]) // если текущий элемент меньше предыдущего
  13. {
  14. int temp = num[j - 1]; // меняем их местами
  15. num[j - 1] = num[j];
  16. num[j] = temp;
  17. }
  18. }
  19. }
  20. }
  21. int main()
  22. {
  23. int *a;
  24. int n;
  25. cout << "N=";
  26. cin >> n;
  27. a = new int[n];
  28. srand(time(NULL));
  29. for (int i = 0; i < n; i++)
  30. {
  31. a[i] = rand() % 201 - 100;
  32. cout << a[i] << " ";
  33. }
  34. time_t t1 = clock();
  35. bubbleSort(a, n); // вызываем функцию сортировки
  36. time_t t2 = clock();
  37. cout << endl;
  38. for (int i = 0; i < n; i++)
  39. cout << a[i] << " ";
  40. cout << endl << t2 - t1;
  41. cin.get(); cin.get();
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement