{"id":26087,"date":"2017-05-30T20:32:12","date_gmt":"2017-05-30T15:02:12","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26087"},"modified":"2017-05-30T20:32:12","modified_gmt":"2017-05-30T15:02:12","slug":"c-programming-count-inversions-in-an-array","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-count-inversions-in-an-array\/","title":{"rendered":"C Programming &#8211; Count Inversions in an array"},"content":{"rendered":"<p>Count Inversions in an <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-count-inversions-in-an-array\/\">array <\/a>indicates \u2013 how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.<\/p>\n<p><span id=\"more-3968\"><\/span>Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j<\/p>\n<p><strong>Example:<\/strong><br \/>\nThe sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>METHOD 1 (Simple)<\/strong><br \/>\nFor each element, count number of elements which are on right side of it and are smaller than it.<\/p>\n<h3 id=\"c-programming\"><b>C Programming<\/b><\/h3>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Aint%20getInvCount(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20int%20inv_count%20%3D%200%3B%0A%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%20-%201%3B%20i%2B%2B)%0A%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20arr%5Bj%5D)%0A%20%20%20%20%20%20%20%20inv_count%2B%2B%3B%0A%20%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20Driver%20progra%20to%20test%20above%20functions%20*%2F%0Aint%20main(int%20argv%2C%20char**%20args)%0A%7B%0A%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20printf(%22%20Number%20of%20inversions%20are%20%25d%20%5Cn%22%2C%20getInvCount(arr%2C%20n))%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<div class=\"responsive-tabs-wrapper\">\n<p><strong>Output:<\/strong><\/p>\n<pre>Number of inversions are 5<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n^2)<\/p>\n<p><strong>METHOD 2(Enhance Merge Sort)<\/strong><\/p>\n<p>Suppose we know the number of inversions in the left half and right half of the array (let be inv1 and inv2), what kinds of inversions are not accounted for in Inv1 + Inv2? The answer is \u2013 the inversions we have to count during the merge step. Therefore, to get number of inversions, we need to add number of inversions in left subarray, right subarray and merge().<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25855\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array.png\" alt=\"Count Inversions in an array\" width=\"576\" height=\"289\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array.png 576w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-300x151.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/p>\n<p><strong>How to get number of inversions in merge()?<\/strong><\/p>\n[ad type=\u201dbanner\u201d]\nIn merge process, let i is used for indexing left sub-array and j for right sub-array. At any step in merge(), if a[i] is greater than a[j], then there are (mid \u2013 i) inversions. because left and right subarrays are sorted, so all the remaining elements in left-subarray (a[i+1], a[i+2] \u2026 a[mid]) will be greater than a[j]\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26089\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-1.png\" alt=\"Count Inversions in an array\" width=\"562\" height=\"275\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-1.png 562w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-1-300x147.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-1-559x275.png 559w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/p>\n<p><strong>The complete picture:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26088\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-java.png\" alt=\"Count Inversions in an array java\" width=\"631\" height=\"502\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-java.png 631w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Count-Inversions-in-an-array-java-300x239.png 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/p>\n<p><strong>Implementation:<\/strong><\/p>\n<h3 id=\"c-programming-2\"><b>C Programming<\/b><\/h3>\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0A%20%0Aint%20%20_mergeSort(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20right)%3B%0Aint%20merge(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20mid%2C%20int%20right)%3B%0A%20%0A%2F*%20This%20function%20sorts%20the%20input%20array%20and%20returns%20the%0A%20%20%20number%20of%20inversions%20in%20the%20array%20*%2F%0Aint%20mergeSort(int%20arr%5B%5D%2C%20int%20array_size)%0A%7B%0A%20%20%20%20int%20*temp%20%3D%20(int%20*)malloc(sizeof(int)*array_size)%3B%0A%20%20%20%20return%20_mergeSort(arr%2C%20temp%2C%200%2C%20array_size%20-%201)%3B%0A%7D%0A%20%0A%2F*%20An%20auxiliary%20recursive%20function%20that%20sorts%20the%20input%20array%20and%0A%20%20returns%20the%20number%20of%20inversions%20in%20the%20array.%20*%2F%0Aint%20_mergeSort(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20right)%0A%7B%0A%20%20int%20mid%2C%20inv_count%20%3D%200%3B%0A%20%20if%20(right%20%3E%20left)%0A%20%20%7B%0A%20%20%20%20%2F*%20Divide%20the%20array%20into%20two%20parts%20and%20call%20_mergeSortAndCountInv()%0A%20%20%20%20%20%20%20for%20each%20of%20the%20parts%20*%2F%0A%20%20%20%20mid%20%3D%20(right%20%2B%20left)%2F2%3B%0A%20%0A%20%20%20%20%2F*%20Inversion%20count%20will%20be%20sum%20of%20inversions%20in%20left-part%2C%20right-part%0A%20%20%20%20%20%20and%20number%20of%20inversions%20in%20merging%20*%2F%0A%20%20%20%20inv_count%20%20%3D%20_mergeSort(arr%2C%20temp%2C%20left%2C%20mid)%3B%0A%20%20%20%20inv_count%20%2B%3D%20_mergeSort(arr%2C%20temp%2C%20mid%2B1%2C%20right)%3B%0A%20%0A%20%20%20%20%2F*Merge%20the%20two%20parts*%2F%0A%20%20%20%20inv_count%20%2B%3D%20merge(arr%2C%20temp%2C%20left%2C%20mid%2B1%2C%20right)%3B%0A%20%20%7D%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20This%20funt%20merges%20two%20sorted%20arrays%20and%20returns%20inversion%20count%20in%0A%20%20%20the%20arrays.*%2F%0Aint%20merge(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20mid%2C%20int%20right)%0A%7B%0A%20%20int%20i%2C%20j%2C%20k%3B%0A%20%20int%20inv_count%20%3D%200%3B%0A%20%0A%20%20i%20%3D%20left%3B%20%2F*%20i%20is%20index%20for%20left%20subarray*%2F%0A%20%20j%20%3D%20mid%3B%20%20%2F*%20j%20is%20index%20for%20right%20subarray*%2F%0A%20%20k%20%3D%20left%3B%20%2F*%20k%20is%20index%20for%20resultant%20merged%20subarray*%2F%0A%20%20while%20((i%20%3C%3D%20mid%20-%201)%20%26%26%20(j%20%3C%3D%20right))%0A%20%20%7B%0A%20%20%20%20if%20(arr%5Bi%5D%20%3C%3D%20arr%5Bj%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%0A%20%20%20%20%20%2F*this%20is%20tricky%20\u2013%20see%20above%20explanation%2Fdiagram%20for%20merge()*%2F%0A%20%20%20%20%20%20inv_count%20%3D%20inv_count%20%2B%20(mid%20-%20i)%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%0A%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20left%20subarray%0A%20%20%20(if%20there%20are%20any)%20to%20temp*%2F%0A%20%20while%20(i%20%3C%3D%20mid%20-%201)%0A%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%0A%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20right%20subarray%0A%20%20%20(if%20there%20are%20any)%20to%20temp*%2F%0A%20%20while%20(j%20%3C%3D%20right)%0A%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%0A%20%20%2F*Copy%20back%20the%20merged%20elements%20to%20original%20array*%2F%0A%20%20for%20(i%3Dleft%3B%20i%20%3C%3D%20right%3B%20i%2B%2B)%0A%20%20%20%20arr%5Bi%5D%20%3D%20temp%5Bi%5D%3B%0A%20%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main(int%20argv%2C%20char**%20args)%0A%7B%0A%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20printf(%22%20Number%20of%20inversions%20are%20%25d%20%5Cn%22%2C%20mergeSort(arr%2C%205))%3B%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Number of inversions are 5<\/pre>\n<p>Note that above code modifies (or sorts) the input array. If we want to count only inversions then we need to create a copy of original array and call mergeSort() on copy.<br \/>\n<strong><br \/>\nTime Complexity: <\/strong>O(nlogn)<\/p>\n<p><strong>Algorithmic Paradigm:<\/strong> Divide and Conquer<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Count Inversions in an array-Divide and Conquer-Inversion Count for an array indicates &#8211; how far (or close) the array is from being sorted.<\/p>\n","protected":false},"author":2,"featured_media":26090,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73719],"tags":[76813,76811,76814,76802,76803,73086,72824,73083,73085,76804,76808,76801,75860,75867,75866,75864,75861,76812,76805,75863,76806,73088,76809,76807,76810,75862,75877],"class_list":["post-26087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-divide-and-conquer","tag-array-count-java","tag-array-definition-computer-science","tag-array-definition-in-computer-science","tag-array-in-python","tag-array-inversion","tag-array-sort-c","tag-c-array-size","tag-c-sort-array","tag-c-array-sort","tag-c-int-array","tag-c-join-arrays","tag-count-inversions-in-an-array","tag-count-inversions-in-an-array-java","tag-count-inversions-in-matrix","tag-count-inversions-python","tag-counting-inversions-divide-and-conquer-algorithm","tag-inversion-count-using-bit","tag-java-count-array","tag-length-of-array-in-c","tag-maximum-number-of-inversions-in-an-array","tag-python-array-count","tag-sort-array-c","tag-sorting-an-array-in-c","tag-sorting-array-in-c","tag-sorting-of-array-in-c","tag-write-a-c-program-to-find-the-inversion-count-of-an-array","tag-write-a-program-to-find-the-inversion-count-of-an-array-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26087"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26087\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26090"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}