{"id":25835,"date":"2017-05-30T20:41:14","date_gmt":"2017-05-30T15:11:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25835"},"modified":"2018-10-29T11:40:10","modified_gmt":"2018-10-29T06:10:10","slug":"java-programming-count-inversions-in-an-array","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-count-inversions-in-an-array\/","title":{"rendered":"Count Inversions in an array"},"content":{"rendered":"<p><strong>Count Inversions<\/strong> in an <a href=\"https:\/\/www.wikitechy.com\/technology\/algorithm-in-c-median-of-two-sorted-arrays\/\">array <\/a>indicates \u2013 how far (or close) the array is from being sorted. If array is already <a href=\"https:\/\/www.wikitechy.com\/technology\/sorted-insert-circular-linked-list-2\/\" target=\"_blank\" rel=\"noopener\">sorted<\/a> 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<h4 id=\"example\"><span style=\"color: #993300;\"><strong>Example:<\/strong><\/span><\/h4>\n<p>The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)<\/p>\n<h3 id=\"method-1-simple\"><span style=\"color: #003366;\"><strong>METHOD 1 (Simple)<\/strong><\/span><\/h3>\n<p>For each element, count number of elements which are on right side of it and are smaller than it.<\/p>\n[ad type=\u201dbanner\u201d]\n<h3 id=\"implementation\"><span style=\"color: #000080;\"><strong>Implementation:<\/strong><\/span><\/h3>\n<h3 id=\"java-programming-for-count-inversions-in-an-array\"><span style=\"color: #339966;\"><b>Java Programming for Count Inversions in an array:\u00a0<\/b><\/span><\/h3>\n[pastacode lang=\u201djava\u201d manual=\u201dclass%20Test%0A%7B%0A%20%20%20%20static%20int%20arr%5B%5D%20%3D%20new%20int%5B%5D%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20%20%20%20%0A%20%20%20%20static%20int%20getInvCount(int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20int%20inv_count%20%3D%200%3B%0A%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%20-%201%3B%20i%2B%2B)%0A%20%20%20%20%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%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20arr%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20inv_count%2B%2B%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20return%20inv_count%3B%0A%20%20%20%20%7D%0A%20%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(%22Number%20of%20inversions%20are%20%22%20%2B%20getInvCount(arr.length))%3B%0A%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<div class=\"responsive-tabs-wrapper\">\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre>Number of inversions are 5<\/pre>\n<p><span style=\"color: #993366;\"><strong>Time Complexity:<\/strong><\/span> <strong>O(n^2)<\/strong><\/p>\n<h3 id=\"method-2enhance-merge-sort\"><span style=\"color: #000080;\"><strong>METHOD 2(Enhance Merge Sort)<\/strong><\/span><\/h3>\n<p>Suppose we know the number of inversions in the left half and right half of the array (let be<em><strong> inv1 and inv2<\/strong><\/em>), 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 <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/merge-sort-algorithm-in-java\" target=\"_blank\" rel=\"noopener\">merge( )<\/a>.<\/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<h3 id=\"how-to-get-number-of-inversions-in-merge\"><span style=\"color: #ff6600;\"><strong>How to get number of inversions in merge()?<\/strong><\/span><\/h3>\n[ad type=\u201dbanner\u201d]\n<p>In <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-merge-sort-doubly-linked-list\/\" target=\"_blank\" rel=\"noopener\">merge<\/a> process, let i is used for indexing left <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-length-largest-subarray-contiguous-elements-2\/\" target=\"_blank\" rel=\"noopener\">sub-array<\/a> 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<h3 id=\"the-complete-picture\"><span style=\"color: #ff9900;\"><strong>The complete picture:<\/strong><\/span><\/h3>\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<h3 id=\"implementation-2\"><span style=\"color: #000080;\"><strong>Implementation:<\/strong><\/span><\/h3>\n<h3 id=\"java-programming-for-count-inversions-in-an-array-2\"><span style=\"color: #339966;\"><b>Java Programming for Count Inversions in an array:\u00a0<\/b><\/span><\/h3>\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201d%20%2F%2F%20Java%20implementation%20of%20counting%20the%20%0A%2F%2F%20inversion%20using%20merge%20sort%0A%20%0Aclass%20Test%0A%7B%0A%20%20%20%20%20%0A%20%20%0A%20%20%20%20static%20int%20mergeSort(int%20arr%5B%5D%2C%20int%20array_size)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20temp%5B%5D%20%3D%20new%20int%5Barray_size%5D%3B%0A%20%20%20%20%20%20%20%20return%20_mergeSort(arr%2C%20temp%2C%200%2C%20array_size%20-%201)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%0A%20%20%20%20static%20int%20_mergeSort(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20right)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20int%20mid%2C%20inv_count%20%3D%200%3B%0A%20%20%20%20%20%20if%20(right%20%3E%20left)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20mid%20%3D%20(right%20%2B%20left)%2F2%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20inv_count%20%20%3D%20_mergeSort(arr%2C%20temp%2C%20left%2C%20mid)%3B%0A%20%20%20%20%20%20%20%20inv_count%20%2B%3D%20_mergeSort(arr%2C%20temp%2C%20mid%2B1%2C%20right)%3B%0A%20%20%20%20%20%20%0A%0A%20%20%20%20%20%20%20%20inv_count%20%2B%3D%20merge(arr%2C%20temp%2C%20left%2C%20mid%2B1%2C%20right)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20return%20inv_count%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20static%20int%20merge(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20mid%2C%20int%20right)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20int%20i%2C%20j%2C%20k%3B%0A%20%20%20%20%20%20int%20inv_count%20%3D%200%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20i%20%3D%20left%3B%20%0A%20%20%20%20%20%20j%20%3D%20mid%3B%20%20%0A%20%20%20%20%20%20k%20%3D%20left%3B%20%0A%20%20%20%20%20%20while%20((i%20%3C%3D%20mid%20-%201)%20%26%26%20(j%20%3C%3D%20right))%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3C%3D%20arr%5Bj%5D)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20inv_count%20%3D%20inv_count%20%2B%20(mid%20-%20i)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20while%20(i%20%3C%3D%20mid%20-%201)%0A%20%20%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20while%20(j%20%3C%3D%20right)%0A%20%20%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20for%20(i%3Dleft%3B%20i%20%3C%3D%20right%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20temp%5Bi%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20return%20inv_count%3B%0A%20%20%20%20%7D%0A%20%20%0A%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20new%20int%5B%5D%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Number%20of%20inversions%20are%20%22%20%2B%20mergeSort(arr%2C%205))%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-2\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre>Number of inversions are 5<\/pre>\n<p>If we want to count only inversions then we need to create a copy of original array and call <strong>mergeSort()<\/strong> on copy.<br \/>\n<strong><br \/>\n<span style=\"color: #993366;\">Time Complexity:<\/span> O(nlogn)<\/strong><\/p>\n<p><span style=\"color: #993366;\"><strong>Algorithmic Paradigm:\u00a0<\/strong><\/span><span style=\"color: #3366ff;\"><a style=\"color: #3366ff;\" href=\"https:\/\/www.wikitechy.com\/technology\/algorithm-in-c-median-of-two-sorted-arrays\/\" target=\"_blank\" rel=\"noopener\"><strong>Divide and Conquer<\/strong><\/a><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java Programming-Count Inversions in an array-Divide and Conquer-Inversion Count for an array indicates how far (or close) the array is from being sorted.<\/p>\n","protected":false},"author":1,"featured_media":26089,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73719],"tags":[75860,75867,75866,75865,75859,75864,75876,75861,75863,75862,75877],"class_list":["post-25835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-divide-and-conquer","tag-count-inversions-in-an-array-java","tag-count-inversions-in-matrix","tag-count-inversions-python","tag-counting-inversions-c","tag-counting-inversions-divide-and-conquer","tag-counting-inversions-divide-and-conquer-algorithm","tag-counting-inversions-python","tag-inversion-count-using-bit","tag-maximum-number-of-inversions-in-an-array","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\/25835","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25835"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26089"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}