Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitd208ced

Browse files
authored
Merge pull request#152 from baijayanti1234/patch-2
Create Selection_Sort.java
2 parents1a78a65 +3d89f87 commitd208ced

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+
classSelectionSort
3+
{
4+
voidsort(intarr[])
5+
{
6+
intn =arr.length;
7+
8+
// One by one move boundary of unsorted subarray
9+
for (inti =0;i <n-1;i++)
10+
{
11+
// Find the minimum element in unsorted array
12+
intmin_idx =i;
13+
for (intj =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+
inttemp =arr[min_idx];
20+
arr[min_idx] =arr[i];
21+
arr[i] =temp;
22+
}
23+
}
24+
25+
// Prints the array
26+
voidprintArray(intarr[])
27+
{
28+
intn =arr.length;
29+
for (inti=0;i<n; ++i)
30+
System.out.print(arr[i]+" ");
31+
System.out.println();
32+
}
33+
34+
// Driver code to test above
35+
publicstaticvoidmain(Stringargs[])
36+
{
37+
SelectionSortob =newSelectionSort();
38+
intarr[] = {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)

[8]ページ先頭

©2009-2025 Movatter.jp