Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- void init_array(int* A, int n);
- void print_array(int* A, int n);
- void swap(int* a, int* b, int temp);
- void shaker_sort(int* A, int n);
- int main()
- {
- srand(123456);
- int const n = 10;
- int A[n];
- init_array(A, n);
- print_array(A, n);
- shaker_sort(A, n);
- }
- void init_array(int* A, int n)
- {
- for (int i = 0; i < n; i++)
- {
- A[i] = rand() % 40;
- }
- return;
- }
- void print_array(int* A, int n)
- {
- for (int i = 0; i < n; i++)
- {
- printf("%-4d", A[i]);
- }
- printf("\n");
- return;
- }
- void swap(int* a, int* b)
- {
- int temp = *a;
- *a = *b;
- *b = temp;
- return ;
- }
- void shaker_sort (int* A, int n)
- {
- for (int i = 0; i < n-1; i++)
- {
- for (int j = 0; j < n - (i + 1); j++)
- {
- if (A[j] > A[j + 1])
- {
- swap(A+j, A+j + 1);
- print_array(A, n);
- }
- /*for (int z = n - (i+j+ 1); z > 0; i--)
- {
- if (A[z + 1] < A[z])
- { swap(A+z, A+z+1);
- print_array(A, n);
- }
- }*/
- }
- for (int z = n - (i + 1); z > 0; z--)
- {
- if (A[z + 1] < A[z])
- {
- swap(A + z, A + z + 1);
- print_array(A, n);
- }
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement