Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define DEBUG if(0)
  5.  
  6. void insertionSort(int v[], int size) {
  7. int i, currentValue, prevIndex;
  8. for(i = 1; i < size; i++) {
  9. currentValue = v[i];
  10. prevIndex = i - 1;
  11. while(prevIndex >= 0 && currentValue < v[prevIndex]) {
  12. v[prevIndex + 1] = v[prevIndex];
  13. prevIndex--;
  14. }
  15. v[prevIndex + 1] = currentValue;
  16. }
  17. }
  18.  
  19. void printIntersection(int a[], int b[],int size) {
  20. int i = 0, j = 0;
  21. while(i < size && j < size) {
  22. if(a[i] < b[j])
  23. i++;
  24. else if(b[j] < a[i])
  25. j++;
  26. else {
  27. printf("%d\n", b[j++]);
  28. i++;
  29. }
  30. }
  31. }
  32.  
  33. int main() {
  34. int size, i;
  35. scanf("%d", &size);
  36. DEBUG printf("**** SIZE%d\n", size);
  37. int* a = (int*) malloc(sizeof(int) * size);
  38. int* b = (int*) malloc(sizeof(int) * size);
  39. for(i = 0; i < size; i++)
  40. scanf("%d", &a[i]);
  41. for(i = 0; i < size; i++)
  42. scanf("%d", &b[i]);
  43. DEBUG {
  44. printf("*** arrays:\n");
  45. for(i = 0; i < size; i++)
  46. printf("%d ", a[i]);
  47. printf("\n");
  48. for(i = 0; i < size; i++)
  49. printf("%d ", b[i]);
  50. printf("\n");
  51. }
  52. insertionSort(a, size);
  53. insertionSort(b, size);
  54. printIntersection(a, b, size);
  55. free(a);
  56. free(b);
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement