Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday 17 October 2017

Selection Sort Program in Java

Selection Sort Algorithm Java

selection sort java

In the last article, We saw the example of bubble sort algorithm in java but here we are going to learn selection sort program in java with example.

Let's start selection sort program in java with output.

First questions is arise in mind what is selection sort and how it works?


What is selection sort?

Selection sort is an sorting algorithm by using section sort algorithm we can sort an array element in ascending and descending order.

In selections sort, We search for the smallest or highest elements in an array and place it to the proper or right location. We swap the current element with the next smallest or highest element.

Selection sort algorithm is better for small size of an array. It is not recommended for large size of array like other sorting algorithm in java.


Time complexity of selection sort algorithm

  • Worst case:  O(n^2)
  • Average case:  O(n^2)
  • Best case:  O(n^2)

Java Program for selection sort in ascending order

This is the simple java selections sort program in ascending order. Here we will take an unsorted array elements and then implement selection sort algorithm and arrange all the elements in ascending order.

class SelectionSortExample
{
public static void selectionSort(int array[])
{
for(int i = 0; i < array.length - 1; i++)
{
int index = i;
for(int j = i+1; j < array.length; j++)
{
if(array[j] < array[index])
{
index = j;
}
}

int smallestNumber = array[index];
array[index] = array[i];
array[i] = smallestNumber;
}
}

public static void main(String args[])
{
int array1[] = {5,4,10,2,24,8,50};
System.out.println("Before using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
System.out.println();

//using selectionSort() method for sorting
selectionSort(array1);

System.out.println("After using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
}
}

Output: Before using selection sort
             5 4 10 2 24 8 50
             After using selection sort
             2 4 5 8 10 24 50

This is the first selection sort program in java with output. Now moving on to java program for selection sort in descending order.


Java Program for selection sort in descending order

This is simple selection sort java descending order program. This example is same as above example but with little change. We will change " if array[j] < array[index] to array[j] > array[index]".


class SelectionSortExample
{
public static void selectionSort(int array[])
{
for(int i = 0; i < array.length - 1; i++)
{
int index = i;
for(int j = i+1; j < array.length; j++)
{
if(array[j] > array[index])
{
index = j;
}
}

int smallestNumber = array[index];
array[index] = array[i];
array[i] = smallestNumber;
}
}

public static void main(String args[])
{
int array1[] = {5,4,10,2,24,8,50};
System.out.println("Before using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
System.out.println();

//using selectionSort() method for sorting
selectionSort(array1);

System.out.println("After using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
}

}

Output: Before using selection sort
             5 4 10 2 24 8 50
             After using selection sort 
             50 24 10 8 5 4 2

This is the simple article of selection sort algorithm with ascending and descending order in java with example.

Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate