Skip to content

Commit d208ced

Browse files
authored
Merge pull request #152 from baijayanti1234/patch-2
Create Selection_Sort.java
2 parents 1a78a65 + 3d89f87 commit d208ced

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

java program/Selection_Sort.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Java program for implementation of Selection Sort
2+
class SelectionSort
3+
{
4+
void sort(int arr[])
5+
{
6+
int n = arr.length;
7+
8+
// One by one move boundary of unsorted subarray
9+
for (int i = 0; i < n-1; i++)
10+
{
11+
// Find the minimum element in unsorted array
12+
int min_idx = i;
13+
for (int j = i+1; j < n; j++)
14+
if (arr[j] < arr[min_idx])
15+
min_idx = j;
16+
17+
// Swap the found minimum element with the first
18+
// element
19+
int temp = arr[min_idx];
20+
arr[min_idx] = arr[i];
21+
arr[i] = temp;
22+
}
23+
}
24+
25+
// Prints the array
26+
void printArray(int arr[])
27+
{
28+
int n = arr.length;
29+
for (int i=0; i<n; ++i)
30+
System.out.print(arr[i]+" ");
31+
System.out.println();
32+
}
33+
34+
// Driver code to test above
35+
public static void main(String args[])
36+
{
37+
SelectionSort ob = new SelectionSort();
38+
int arr[] = {64,25,12,22,11};
39+
ob.sort(arr);
40+
System.out.println("Sorted array");
41+
ob.printArray(arr);
42+
}
43+
}

0 commit comments

Comments
 (0)