java - Data Structures -
i coding program going compute number of inversions in array. requirement use divide-and-conquer algorithm. have used merge sort afterwards stuck. need create method called count in order count inversions using recursion. here need help... thank in advance
import java.util.*; public class inversions { public static void main(string[] args) { integer[] = {2, 6, 3, 5, 1}; mergesort(a); system.out.println(arrays.tostring(a)); } public static void mergesort(comparable [ ] a) { comparable[] tmp = new comparable[a.length]; mergesort(a, tmp, 0, a.length - 1); } private static void mergesort(comparable [ ] a, comparable [ ] tmp, int left, int right) { if( left < right ) { int center = (left + right) / 2; mergesort(a, tmp, left, center); mergesort(a, tmp, center + 1, right); merge(a, tmp, left, center + 1, right); } } private static void merge(comparable[ ] a, comparable[ ] tmp, int left, int right, int rightend ) { int leftend = right - 1; int k = left; int num = rightend - left + 1; while(left <= leftend && right <= rightend) if(a[left].compareto(a[right]) <= 0) tmp[k++] = a[left++]; else tmp[k++] = a[right++]; while(left <= leftend) // copy rest of first half tmp[k++] = a[left++]; while(right <= rightend) // copy rest of right half tmp[k++] = a[right++]; // copy tmp for(int = 0; < num; i++, rightend--) a[rightend] = tmp[rightend]; } public int count(int[] a, int n){} }
added changes in code.
import java.util.*; public class inversions { public static int countinversions = 0; public static void main(string[] args) { integer[] = {2, 6, 3, 5, 1}; mergesort(a); system.out.println(arrays.tostring(a)); system.out.println(countinversions); } public static void mergesort(comparable [ ] a) { comparable[] tmp = new comparable[a.length]; mergesort(a, tmp, 0, a.length - 1); } private static void mergesort(comparable [ ] a, comparable [ ] tmp, int left, int right) { if( left < right ) { int center = (left + right) / 2; mergesort(a, tmp, left, center); mergesort(a, tmp, center + 1, right); merge(a, tmp, left, center + 1, right); } } private static void merge(comparable[ ] a, comparable[ ] tmp, int left, int right, int rightend ) { int leftend = right - 1; int k = left; int num = rightend - left + 1; while(left <= leftend && right <= rightend) if(a[left].compareto(a[right]) <= 0) tmp[k++] = a[left++]; else { countinversions+=((leftend-left)+1); // counts inversions tmp[k++] = a[right++]; } while(left <= leftend) // copy rest of first half tmp[k++] = a[left++]; while(right <= rightend) // copy rest of right half tmp[k++] = a[right++]; // copy tmp for(int = 0; < num; i++, rightend--) a[rightend] = tmp[rightend]; } }
in above code, counting inversions when 2 arrays merging. example 1st sub-array contains [1, 6] , 2nd sub-array has [2, 3]. when these merged inversion count 2 i.e. [6>2] & [6>3]. when condition comes a[left] > a[right] count number of elements remaining on right sub-array because less a[left] , add them countinversions.
Comments
Post a Comment