This repository was archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathSelectionSort.java
More file actions
45 lines (35 loc) · 1.54 KB
/
Copy pathSelectionSort.java
File metadata and controls
45 lines (35 loc) · 1.54 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
39
40
41
42
43
44
45
package Sorting;
import java.util.*;
public class SelectionSort {
public static void swap(int[] arr,int a,int b) // Swapping Method
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void main(String[] args)
{
// Input an array first
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int n = sc.nextInt(); // To input the size of the array i.e. n
int[] arr = new int[n]; // Created an array of size n
System.out.print("Enter "+n+" elements : ");
for(int i=0;i<n;i++)
{ arr[i]=sc.nextInt(); } // Input the elements of the array
// Sorting Part
// In Selection Sort also, we keep the sorted part at the right size
for(int i = arr.length-1 ; i > 0 ; i--) // i denotes the last sorted index of the array
{
int l_Index = 0; // Index of the largest element in the array (Assumed to be at 0th Position).
for(int j = 0; j <= i; j++)
{
if( arr[j] > arr[l_Index]) l_Index = j; // We find the largest element in the array
} // And assign l_index to it's index position
swap(arr, i, l_Index); // Later we swap the position of ith element with l_index
}
// Printing the array
System.out.print("Sorted Array is : ");
for(int i: arr) System.out.print(i+" ");
}
}