Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define DEBUG if(0)
- void insertionSort(int v[], int size) {
- int i, currentValue, prevIndex;
- for(i = 1; i < size; i++) {
- currentValue = v[i];
- prevIndex = i - 1;
- while(prevIndex >= 0 && currentValue < v[prevIndex]) {
- v[prevIndex + 1] = v[prevIndex];
- prevIndex--;
- }
- v[prevIndex + 1] = currentValue;
- }
- }
- void printIntersection(int a[], int b[],int size) {
- int i = 0, j = 0;
- while(i < size && j < size) {
- if(a[i] < b[j])
- i++;
- else if(b[j] < a[i])
- j++;
- else {
- printf("%d\n", b[j++]);
- i++;
- }
- }
- }
- int main() {
- int size, i;
- scanf("%d", &size);
- DEBUG printf("**** SIZE%d\n", size);
- int* a = (int*) malloc(sizeof(int) * size);
- int* b = (int*) malloc(sizeof(int) * size);
- for(i = 0; i < size; i++)
- scanf("%d", &a[i]);
- for(i = 0; i < size; i++)
- scanf("%d", &b[i]);
- DEBUG {
- printf("*** arrays:\n");
- for(i = 0; i < size; i++)
- printf("%d ", a[i]);
- printf("\n");
- for(i = 0; i < size; i++)
- printf("%d ", b[i]);
- printf("\n");
- }
- insertionSort(a, size);
- insertionSort(b, size);
- printIntersection(a, b, size);
- free(a);
- free(b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement