Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctime>
- #include <iostream>
- void PrintA(int A[], int N);
- void Swap(int *a, int *b);
- void InsertionSort(int A[], int N);
- int main(void)
- {
- const int N = 10;
- int A[N];
- for (int i = 0; i < N; i++)
- {
- A[i] = 1 + rand() % 9;
- }
- printf("Array under insertion sort: n");
- PrintA(A, N);
- InsertionSort(A, N);
- /*printf("Array after insertion sort: n");
- PrintA(A, N);*/
- system("pause");
- }
- void PrintA(int A[], int N)
- {
- for (int i = 0; i < N; i++)
- {
- printf("%d ", A[i]);
- }
- printf("n");
- }
- void Swap(int *a, int *b)
- {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- void InsertionSort(int A[], int N)
- {
- int j;
- for (int i = 1; i < N; i++)
- {
- j = i;
- while (j > 0 && A[j - 1] > A[j])
- {
- Swap(&A[j - 1], &A[j]);
- j--;
- }
- }
- PrintA(A, N);
- }
- void InsertionSort(int A[], int N)
- {
- // Вот здесь:
- int template[A.Length];
- for(int i = 0;i < A.Length;i++)
- {
- template[i] = A[i];
- }
- int j;
- for (int i = 1; i < N; i++)
- {
- j = i;
- while (j > 0 && A[j - 1] > A[j])
- {
- Swap(&A[j - 1], &A[j]);
- j--;
- }
- }
- //Передаем наш массив
- PrintA(template, N);
- }
- int main(void)
- {
- const int N = 10;
- int A[N], B[N];
- ....код наполняющий массив A
- memcpy((void*)B, (void*)A, (sizeof(int) * N));
- PrintA(B, N);
- typedef struct Massiv_ {
- int A[100];
- } Massiv;
- void Sort(Massiv M, int N) {
- // Сортируем M.A - тот массив, что передан, тронут не будет
- }
Add Comment
Please, Sign In to add comment