Advertisement
VictoriaLodochkina

SAKOD lab1

Sep 7th, 2020 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. sort.cpp
  2. #include "Sort.h"
  3. #include "swap.h"
  4. void vstavka(int* mas, int n)
  5. {
  6.     for (int i = 1; i < n; i++)
  7.     {
  8.         for (int j = i; (j>0)&&(mas[j-1]>mas[j]); j--)
  9.         {
  10.             swap(&mas[j-1], &mas[j]);
  11.         }
  12.     }
  13. }
  14. void shaker(int *mas, int start, int N)
  15. {
  16.     int l, r, i;
  17.     l=start;
  18.     r=N-1;
  19.     while (l<=r)
  20.     {
  21.         for (i=r; i>=l; i--)
  22.         {
  23.             if (mas[i-1]>mas[i])
  24.             {
  25.                 swap(mas[i], mas[i-1]);
  26.             }
  27.         }
  28.         l++;
  29.         for (i=l; i<=r; i++)
  30.         {
  31.             if (mas[i-1]>mas[i])
  32.             {
  33.                 swap(mas[i], mas[i-1]);
  34.             }
  35.         }
  36.         r--;
  37.     }
  38. }
  39. sort.h
  40. #ifndef SORT_H_
  41. #define SORT_H_
  42. void vstavka(int* mas, int n);
  43. void shaker(int *mas, int start, int N)
  44. #endif
  45.  
  46. swap.cpp
  47. #include "swap.h"
  48. void swap(int* a, int* b)
  49. {
  50.     int temp = *a;
  51.     *a = *b;
  52.     *b = temp;
  53. }
  54. swap.h
  55. #ifndef SWAP_H_
  56. #define SWAP_H_
  57. void swap(int* a, int* b);
  58. #endif
  59. main
  60. #include <iostream>
  61. #include "Sort.h"
  62.  
  63. int main()
  64. {
  65.     int num;
  66.     std::cout << "Enter the size of array: ";
  67.     std::cin >> num;
  68.     int* arr = new int[num];
  69.     int* arr1 = new int[num];
  70.     int* arr2 = new int[num];
  71.     std::cout << "Enter array: " << std::endl;
  72.     for (int i = 0; i < num; i++)
  73.     {
  74.         std::cin >> arr[i];
  75.         arr1[i] = arr[i];
  76.         arr2[i] = arr[i];
  77.     }
  78.     std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
  79.     vstavka(arr, num);
  80.     for (int i = 0; i < num; i++)
  81.     {
  82.         std::cout << arr[i] << " ";
  83.     }
  84.     std::cout << std::endl;
  85.     std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
  86.     shaker(arr1, 1, num);
  87.     for (int i = 0; i < num; i++)
  88.     {
  89.         std::cout << arr1[i] << " ";
  90.     }
  91.     std::cout << std::endl;
  92.     std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
  93.     vstavka(arr2, num);
  94.     for (int i = 0; i < num; i++)
  95.     {
  96.         std::cout << arr2[i] << " ";
  97.     }
  98.     std::cout << std::endl;
  99.     return 0;
  100. }
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement