Pages

Monday, 23 December 2019

SELECTION SORT

import java.util.Scanner;

class selectionsort
 {
  public static void main(String[] args)
  {
   int i,j,n,min,temp=0;
   Scanner ss = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=ss.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=ss.nextInt();
    }
   System.out.print("\n Unsorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
 
   for(i=0;i<n;i++)
    {
     min=i;
     for(j=i+1;j<n;j++)
      {
        if(a[j]<a[min])
        {
      min=j;
        }
      }
      temp=a[i];
      a[i]=a[min];
      a[min]=temp;
     System.out.printf( "\n Pass-%d  value that is to swap =%d with minimum value =%d",i,temp, a[i]);
    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
  }
}

output:

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...