Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sort.cpp
- #include "Sort.h"
- #include "swap.h"
- void vstavka(int* mas, int n)
- {
- for (int i = 1; i < n; i++)
- {
- for (int j = i; (j>0)&&(mas[j-1]>mas[j]); j--)
- {
- swap(&mas[j-1], &mas[j]);
- }
- }
- }
- void shaker(int *mas, int start, int N)
- {
- int l, r, i;
- l=start;
- r=N-1;
- while (l<=r)
- {
- for (i=r; i>=l; i--)
- {
- if (mas[i-1]>mas[i])
- {
- swap(mas[i], mas[i-1]);
- }
- }
- l++;
- for (i=l; i<=r; i++)
- {
- if (mas[i-1]>mas[i])
- {
- swap(mas[i], mas[i-1]);
- }
- }
- r--;
- }
- }
- sort.h
- #ifndef SORT_H_
- #define SORT_H_
- void vstavka(int* mas, int n);
- void shaker(int *mas, int start, int N)
- #endif
- swap.cpp
- #include "swap.h"
- void swap(int* a, int* b)
- {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- swap.h
- #ifndef SWAP_H_
- #define SWAP_H_
- void swap(int* a, int* b);
- #endif
- main
- #include <iostream>
- #include "Sort.h"
- int main()
- {
- int num;
- std::cout << "Enter the size of array: ";
- std::cin >> num;
- int* arr = new int[num];
- int* arr1 = new int[num];
- int* arr2 = new int[num];
- std::cout << "Enter array: " << std::endl;
- for (int i = 0; i < num; i++)
- {
- std::cin >> arr[i];
- arr1[i] = arr[i];
- arr2[i] = arr[i];
- }
- std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
- vstavka(arr, num);
- for (int i = 0; i < num; i++)
- {
- std::cout << arr[i] << " ";
- }
- std::cout << std::endl;
- std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
- shaker(arr1, 1, num);
- for (int i = 0; i < num; i++)
- {
- std::cout << arr1[i] << " ";
- }
- std::cout << std::endl;
- std::cout << "THIS IS SORT BY INSERTS:" << std::endl;
- vstavka(arr2, num);
- for (int i = 0; i < num; i++)
- {
- std::cout << arr2[i] << " ";
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement