हिंदी

Define a class pin code and store the given pin codes in a single-dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.

Advertisements

प्रश्न

Define a class pin code and store the given pin codes in a single-dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.

110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

संक्षेप में उत्तर
Advertisements

उत्तर

class SelectionSort
{
   public static void main(String args[]) 
   {
     int arr[]={110061,110001,110029,110023,110055,110006,110019, 110033}; 
     int len=arr.length
     //performing selection sort in ascending order
     for(int i=0;i<len-1;i++)
     {
        int pos=i;
        for(int j=i+1;j<len;j++)
        {
        if(arr[pos]>arr[j]) //finding the smallest element
     w.r.t. index pos. i
         pos=j; 
        }
        int temp=arr[i]; //swapping using 3rd variable
        arr[i]=arr[pos];
        arr[pos]=temp;
     }
     System.out.println("Array elements after the 
     sorting"); 
     for(int i=0;i<len;i++)
     {
        System.out.print(arr[i]+" ");
     }
   }
}

Output:

Array elements after the sorting

110001 110006 110019 110023 110029 110033 110055 110061

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2023-2024 (March) Official
Advertisements
Share
Notifications



      Forgot password?
Use app×