Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
64 lines (51 loc) · 1.66 KB

File metadata and controls

64 lines (51 loc) · 1.66 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
public class SelectionSort {
/**
* This method performs the selection sort algorithm on the input array.
*
* @param arr The array to be sorted.
*/
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
// Assume the current index has the minimum value
int minIndex = i;
for (int j = i + 1; j < n; j++) {
// If a smaller element is found, update minIndex
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the element at index i
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input prompt for array size
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Input prompt for array elements
System.out.println("Enter the elements of the array:");
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
// Perform Selection Sort
selectionSort(arr);
// Output the sorted array
System.out.println("Sorted array:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
/*
Enter the size of the array: 6
Enter the elements of the array:
12 5 7 3 9 2
Sorted array:
2 3 5 7 9 12
*/
Morty Proxy This is a proxified and sanitized view of the page, visit original site.