Advertisement
Guest User

Directi Q2

a guest
Mar 28th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time.
  2.  
  3. Given below is pseudocode of insertion sort algorithm applied on array A (zero-based indexing).
  4. def compare(a, b) :
  5. if a > b return 1
  6. else return -1
  7.  
  8. for i : 1 to length(A)
  9. j = i - 1
  10. while j > 0
  11. if compare(A[j-1], A[j]) > 0
  12. swap(A[j], A[j-1])
  13. j = j - 1
  14. else break
  15.  
  16. Given an array A of distinct integers , find difference between number of compare function calls and number of swap function calls by above algorithm when applied on A.
  17.  
  18.  
  19. Let us take an example with A = {1, 2, 4, 3}. If we apply insertion sort as above on A we call sequeunce of compare and swap functions in following order
  20.  
  21. compare (A[0], A[1])
  22. compare (A[1], A[2])
  23. compare (A[2], A[3])
  24. swap (A[2], A[3])
  25. compare (A[1], A[2])
  26.  
  27.  
  28. Here compare function is called 4 times, swap function is called 1 time. The answer is 4-1 = 3.
  29. Input
  30.  
  31. The first line of the input contains an integer T denoting the number of test cases.T test cases follow
  32. The first line of each test case contains a single integer N denoting length of array A.
  33. The second line of each test case contains N space-separated distinct integers A0, A1, ..., AN-1denoting the elements of A.
  34. Output
  35.  
  36. For each test case, output a single line containing difference between number of compare function calls and number of swap function calls.
  37. Constraints
  38.  
  39. 1 ≤ T ≤ 100
  40. 1 ≤ N ≤ 10000
  41. 1 ≤ A[i] ≤ N
  42. Example
  43.  
  44. Input:
  45. 6
  46. 2
  47. 1 2
  48. 2
  49. 2 1
  50. 4
  51. 1 2 4 3
  52. 4
  53. 2 3 1 4
  54. 4
  55. 4 3 2 1
  56. 10
  57. 5 3 2 4 1 6 7 9 8 10
  58.  
  59. Output:
  60. 1
  61. 0
  62. 3
  63. 2
  64. 0
  65. 6
  66. Explanation
  67.  
  68. Example test case 1.
  69. For A = {1, 2} following is the sequence of compare and swap functions called.
  70.  
  71. compare (A[0], A[1])
  72.  
  73. Hence answer is 1 for first test case.
  74.  
  75.  
  76. Example test case 2.
  77. For A = {2, 1} following is the sequence of compare and swap functions called.
  78.  
  79. compare (A[0], A[1])
  80. swap (A[0], A[1])
  81.  
  82.  
  83. Hence answer is 0 for second test case.
  84.  
  85. Example test case 4.
  86. For A = {2, 3, 1, 4} following is the sequence of compare and swap functions called.
  87.  
  88. compare (A[0], A[1])
  89. compare (A[1], A[2])
  90. swap (A[1], A[2])
  91. compare (A[0], A[1])
  92. swap (A[0], A[1])
  93. compare (A[2], A[3])
  94.  
  95.  
  96. Hence answer is 2 for fourth test case.
  97. note--O(n^2) fails..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement