8000 Added Selection Sort · AllAlgorithms/java@dc30e97 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc30e97

Browse files
DESKTOP-RHFEE2P\Yashabranhe
authored andcommitted
Added Selection Sort
1 parent 924e6d9 commit dc30e97

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

sorting/SelectionSort.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package demos;
2+
3+
import java.util.Scanner;
4+
5+
public class SelectionSort {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
Scanner sc=new Scanner(System.in);//user input using scanner
10+
System.out.println("Enter the size of the array");//taking size of the array from user
11+
int size=sc.nextInt();
12+
13+
int array[]=new int[size];//declaring array of n elements to be sorted
14+
System.out.println("Enter"+ size + "elements to sort");
15+
for(int i=0;i<size;i++)//taking array elements from user and storing them in array
16+
{
17+
array[i]=sc.nextInt();
18+
}
19+
20+
System.out.println("Array before sort");
21+
for(int i=0;i<size;i++)//taking array elements from user and storing them in array
22+
{
23+
System.out.print(array[i]+ " ");//Printing elements of array before sorting in a single line
24+
}
25+
26+
SelectionSorting(array);
27+
}
28+
29+
public static void SelectionSorting(int array[]){
30+
31+
for(int i=0;i<array.length;i++)
32+
{
33+
for(int j=i+1;j<array.length;j++)
34+
{
35+
if(array[i]>array[j])//if i'th element of array is greater than j'th element swap the elements
36+
{
37+
int temp=array[i];
38+
array[i]=array[j];
39+
array[j]=temp;
40+
}
41+
}
42+
}
43+
44+
System.out.println("Array after sorting");
45+
for(int i=0;i<array.length;i++)
46+
{
47+
System.out.print(array[i]+ " ");//Printing elements of array after sorting in a single line
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)
0