fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int b_comparisons = 0;
  6. int i_comparisons = 0;
  7. int i_swaps=0;
  8. int b_swaps=0;
  9.  
  10.  
  11. void bubleSort(int arr[],int n){
  12. for (int i = 0;i<n-1;i++){
  13. for (int j=0;j<n-1-i;j++){
  14. b_comparisons++;
  15. if (arr[j]>arr[j+1]){
  16. b_swaps++;
  17. int temp=arr[j];
  18. arr[j]=arr[j+1];
  19. arr[j+1]=temp;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. void instSort(int arr[], int n) {
  26. for (int i = 1; i < n; i++) {
  27. int key = arr[i];
  28. int j = i - 1;
  29.  
  30. while (j >= 0 && arr[j] > key) {
  31. i_comparisons++;
  32. arr[j + 1] = arr[j];
  33. j = j - 1;
  34. i_swaps++;
  35. }
  36.  
  37. arr[j + 1] = key;
  38. i_swaps++;
  39. }
  40. }
  41. void printArr(int arr[],int n){
  42. for (int i=0;i<n;i++){
  43. cout<<arr[i]<<" ";
  44. }
  45. }
  46.  
  47.  
  48.  
  49. int main() {
  50. int n=4;
  51. int arr[n]={3,1,5,0};
  52. int arr2[n]={3,4,7,1};
  53.  
  54. cout<<"Бульбашкове"<<endl;
  55. bubleSort(arr,n);
  56. printArr(arr,n);
  57.  
  58. cout<<endl<< "Кількість перестановок " << b_swaps<<endl;
  59.  
  60. cout<< "Вставками "<<endl;
  61. instSort(arr2,n);
  62. printArr(arr2,n);
  63. cout << endl<<"Кількість перестановок " << i_swaps<<endl;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Бульбашкове
0 1 3 5 
Кількість перестановок 4
Вставками 
1 3 4 7 
Кількість перестановок 6