Circular Sorting Algorithm For Array Elements
Circular sorting algorithm rearranges elements to their natural indices. It's useful for identifying duplicates and finding missing elements in an array. Example: [5,4,2,1,3] becomes [1,2,3,4,5].
Circular sorting
Pre-requisite : elements of the array should be between 1 to length of the array
In Circular Sorting elements are placed at their natural indexs
Example:if the array is [5,4,2,1,3] => [1,2,3,4,5]
import java.util.*;
public class Main {
public static void main(String[] args) {
int arr[] = {5,4,2,1,3};
findDuplicates(arr);
for(int i =0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
public static void findDuplicates(int[] nums){
int i =0,n = nums.length;
while(i<n){
int j = nums[i]-1;
if(nums[i...