Selection sort program using lowest element in java

 // Selection sort by using lowest element of the given array.

import java.util.Scanner;

import java.util.Arrays;

class SelectionSort2

{

public static void main(String args[])

{

Scanner s=new Scanner(System.in);

SelectionSort2 obj=new SelectionSort2();

System.out.println("Enter a Integer number :");

int num=s.nextInt();

int[] arr=new int[num];

for(int i=0; i<arr.length; i++)

{

arr[i]=s.nextInt();

}

obj.sort(arr);

System.out.println("Sorted data :"+Arrays.toString(arr));

}


private void sort(int arr[])

{

int sortedIndex=-1;

for(int i=(arr.length-1); i>0; i--)

{

int min_num=arr[arr.length-1];

int min_num_index=arr.length-1;

for(int j=(arr.length-1); j>sortedIndex; j--)

{

if(min_num>arr[j])

{

min_num=arr[j];

min_num_index=j;

}

//System.out.println(+min_num);

//System.out.println(+min_num_index);

}

++sortedIndex;

int temp=arr[sortedIndex];

arr[sortedIndex]=min_num;

arr[min_num_index]=temp;

}

}

}

Expected Output :




Post a Comment

0 Comments