Skip to content

Commit 29c17bf

Browse files
authored
Merge pull request #928 from MathurUtkarsh/patch-1
Create selection_sort.java
2 parents c3b61c4 + 783b225 commit 29c17bf

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* In the following Java program, we ask user to enter the array elements or number,
2+
then we compare the array's element and start swapping with the variable temp.
3+
Put the first element in the temp and the second element in the first,
4+
and then temp in the second number and continue for the next match to sort the whole
5+
array in ascending order. */
6+
7+
import java.util.Scanner;
8+
9+
public class SelectionSortExample2
10+
{
11+
public static void main(String args[])
12+
{
13+
int size, i, j, temp;
14+
int arr[] = new int[50];
15+
Scanner scan = new Scanner(System.in);
16+
17+
System.out.print("Enter Array Size : "); // size of array taken from user.
18+
size = scan.nextInt();
19+
20+
System.out.print("Enter Array Elements : ");
21+
for(i=0; i<size; i++)
22+
{
23+
arr[i] = scan.nextInt(); // enter elements in array.
24+
}
25+
26+
System.out.print("Sorting Array using Selection Sort Technique..\n");
27+
for(i=0; i<size; i++)
28+
{
29+
for(j=i+1; j<size; j++)
30+
{
31+
if(arr[i] > arr[j])
32+
{
33+
temp = arr[i];
34+
arr[i] = arr[j];
35+
arr[j] = temp;
36+
}
37+
}
38+
}
39+
40+
System.out.print("Now the Array after Sorting is :\n");
41+
for(i=0; i<size; i++)
42+
{
43+
System.out.print(arr[i]+ " "); // printing the sorted array.
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)