Hello Guys ,
In my previous post , I discussed about bubble sort program implementation in java using array , Today we will learn the selection sort program implementation in java using array again
So lets get start
Program Implementation of Selection Sort in java
Output
Time Complexity : The Time Complexity of the Selection Sort Algorithm is O(n^2) ,Because we are using two for loop in the algo.
Hope you guys understand the program well and if you have any problem do comment below.
In next Post I will share the Program of Insertion Sort Using Array.
Thanks for Reading
-Noeik
In my previous post , I discussed about bubble sort program implementation in java using array , Today we will learn the selection sort program implementation in java using array again
So lets get start
Program Implementation of Selection Sort in java
/*
@author noeik
*/
public class SelectionSort {
static int[] data = {10,23,45,5,3,2};// global data integer array
public static void main(String[] args) {
sort(data);
display();
}
// selection sorting method
public static void sort(int[] data)
{
for(int i=0;i<data.length-1;i++)
{
int minIndex =i;
for(int j=i+1;j<data.length;j++)
{
System.out.println("before sorting element");
display();
if(data[j]<data[minIndex])
{
int temp =data[j];
data[j]=data[minIndex];
data[minIndex]=temp;
System.out.println("After sorting element");
display();
}
}
}
}
// displaying the array method
public static void display()
{
String result = "[";
for(int d :data)
{
result = result +d+",";
}
result =result+"]";
System.out.println(result);
}
}
Output
before sorting element
[10,23,45,5,3,2,]
before sorting element
[10,23,45,5,3,2,]
before sorting element
[10,23,45,5,3,2,]
After sorting element
[5,23,45,10,3,2,]
before sorting element
[5,23,45,10,3,2,]
After sorting element
[3,23,45,10,5,2,]
before sorting element
[3,23,45,10,5,2,]
After sorting element
[2,23,45,10,5,3,]
before sorting element
[2,23,45,10,5,3,]
before sorting element
[2,23,45,10,5,3,]
After sorting element
[2,10,45,23,5,3,]
before sorting element
[2,10,45,23,5,3,]
After sorting element
[2,5,45,23,10,3,]
before sorting element
[2,5,45,23,10,3,]
After sorting element
[2,3,45,23,10,5,]
before sorting element
[2,3,45,23,10,5,]
After sorting element
[2,3,23,45,10,5,]
before sorting element
[2,3,23,45,10,5,]
After sorting element
[2,3,10,45,23,5,]
before sorting element
[2,3,10,45,23,5,]
After sorting element
[2,3,5,45,23,10,]
before sorting element
[2,3,5,45,23,10,]
After sorting element
[2,3,5,23,45,10,]
before sorting element
[2,3,5,23,45,10,]
After sorting element
[2,3,5,10,45,23,]
before sorting element
[2,3,5,10,45,23,]
After sorting element
[2,3,5,10,23,45,]
[2,3,5,10,23,45,]
Time Complexity : The Time Complexity of the Selection Sort Algorithm is O(n^2) ,Because we are using two for loop in the algo.
Hope you guys understand the program well and if you have any problem do comment below.
In next Post I will share the Program of Insertion Sort Using Array.
Thanks for Reading
-Noeik
0 comments:
Post a Comment