-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
53 lines (52 loc) · 1.47 KB
/
SelectionSort.java
File metadata and controls
53 lines (52 loc) · 1.47 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
46
47
48
49
50
51
52
53
// Program: Selection Sort Algorithm
// Topic: Sorting Algorithms (Arrays)
// Description: Implements the Selection Sort technique to arrange an array of integers in ascending order.
// Iteratively finds the minimum element from the unsorted part of the array and swaps it with the first unsorted element.
// Displays the sorted array at the end.
package Sorting;
import java.util.*;
/**
*
* @author Samim
*/
public class SelectionSort //Ascending Sort
{
public void sort(int ar[],int n)
{
int min;
for(int i=0;i<n;i++)
{
min = i;
for(int j=i+1;j<n;j++)
{
if(ar[j]<ar[min])
{
min=j;
}
}
int temp=ar[min];
ar[min]=ar[i];
ar[i]=temp;
}
System.out.println("Sorted Array :");
for(int i=0;i<n;i++)
{
System.out.println(ar[i]);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size;
System.out.println("Enter Number oF Elements :");
size=sc.nextInt();
int ar[]=new int [size];
System.out.println("Enter The Elements Of the Array :");
for(int i=0;i<size;i++)
{
ar[i]=sc.nextInt();
}
SelectionSort obj=new SelectionSort();
obj.sort(ar, size);
}
}