Selection Sort program in java

 //Here is the selection sort program in java programming

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

import java.util.Scanner;

import java.util.Arrays;

class SelectionSort

{

public static void main(String args[])

{

Scanner s=new Scanner(System.in);

SelectionSort obj=new SelectionSort();

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=(arr.length);

System.out.println(+arr.length);

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

{

int max_num=0;

//int min_num=0;

int max_num_index=arr[0];

for(int j=0; j<sortedIndex; j++)

{

if(max_num<arr[j])

{

max_num=arr[j];

max_num_index=j;

}

}

--sortedIndex;

int temp=arr[sortedIndex];

arr[sortedIndex]=max_num;

arr[max_num_index]=temp;

}

}

}

Post a Comment

0 Comments