sorting question.

 Given an array with N distinct elements all are positive , convert the

given array to a form where all elements are in the range

from 0 to N-1. The order of elements is the same, i.e., 0 is

placed in the place of the smallest element, 1 is placed for

the second smallest element, ... N-1 is placed for the largest

element.

public class code {
    public static void main(String[] args) {
        int[] arr={54,11,28,47,91,63};
        int n=arr.length;
        int x=0;
        for(int i=0; i<n; i++){
            int mn=Integer.MAX_VALUE;
            int ix=-1;
            for(int j=0; j<n; j++){
                if(arr[j]<mn && arr[j]>0){
                    mn=arr[j];
                    ix=j;
                }
            }
            arr[ix]=x;
            x--;
        }
        for(int ele : arr)
            System.out.print(-1*ele+" ");
        System.out.println();
    }
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.