forked from soumikbur/Hackoctoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_S.java
More file actions
38 lines (35 loc) · 1.21 KB
/
Copy pathSelection_S.java
File metadata and controls
38 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.ObjectInputFilter.Status;
import java.util.Scanner;
public class Selection_S {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of the array:");
int l = sc.nextInt();// Length of array
int i, j, min = 0, t = 0;// loop controller
int arr[] = new int[l];// new array
for (i = 0; i < l; i++)// For user defined elements
{
System.out.print("Enter array element:");
arr[i] = sc.nextInt();// Length of array
}
// selection sorting the array
for (i = 0; i < l - 1; i++) {
// Find the min
min = i;// for comparing
for (j = i + 1; j < l; j++) {
if (arr[j] < arr[min])// ascending order
min = j;
}
// swapping done
t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
System.out.println("The resultant sorted array: ");
for (i = 0; i < l; i++) // Displaying
{
System.out.print(arr[i] + " ");
}
sc.close();// scanner closes
}
}