ronansaini
BAN USER- 0of 0 votes
AnswersI have an array containing duplicates in the following format:
arr[]={ 2,9,1,5,1,4,9,7,2,1,4 }
I want to sort the array in place such that all the duplicate elements are moved towards the end and sorted in different sub arrays like following:
arr[]={ 1,2,4,5,7,9, 1,2,4,9, 1 }
There is no range for integers for the array specified. Following is the code which i tried. This code recursively sorts the subarrays and then move duplicates towards the end. But Complexity wise this is not optimal solution.
Please suggest if it can be solve in O(n) or O(nlogn). Entire code is as follows:
- ronansaini in Indiapublic static int sortDuplicates(int a[],int start,int end){ int i, k,temp; if(start==end) return 1; Arrays.sort(a, start, end); k = start; for (i = start+1; i < end; i++) { if (a[k] != a[i] && a[k]<a[i]) { temp=a[k+1]; a[k+1] = a[i]; a[i]=temp; k++; } } return sortDuplicates(a,k+1,a.length); }
| Report Duplicate | Flag | PURGE
United HealthGroup Java Developer Algorithm Arrays