Skip to content

Commit 57b2ec5

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#110 from TharinduRewatha/patch-2
Selection_Sort.java
2 parents bc87e72 + 3ee6004 commit 57b2ec5

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Selection_Sort.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//https://www.facebook.com/permalink.php?story_fbid=2750473708542571&id=100007399066161
2+
//Subscribed by tharindu Rewatha
3+
4+
class JavaExample
5+
{
6+
void selectionSort(int arr[])
7+
{
8+
int len = arr.length;
9+
10+
for (int i = 0; i < len-1; i++)
11+
{
12+
// Finding the minimum element in the unsorted part of array
13+
int min = i;
14+
for (int j = i+1; j < len; j++)
15+
if (arr[j] < arr[min])
16+
min = j;
17+
18+
/* Swapping the found minimum element with the first
19+
* element of the sorted subarray using temp variable
20+
*/
21+
int temp = arr[min];
22+
arr[min] = arr[i];
23+
arr[i] = temp;
24+
}
25+
}
26+
27+
// Displays the array elements
28+
void printArr(int arr[])
29+
{
30+
for (int i=0; i<arr.length; i++)
31+
System.out.print(arr[i]+" ");
32+
System.out.println();
33+
}
34+
35+
public static void main(String args[])
36+
{
37+
JavaExample obj = new JavaExample();
38+
int numarr[] = {101,5,18,11,80, 67};
39+
40+
System.out.print("Original array: ");
41+
obj.printArr(numarr);
42+
43+
//calling method for selection sorting
44+
obj.selectionSort(numarr);
45+
46+
System.out.print("Sorted array: ");
47+
obj.printArr(numarr);
48+
}
49+
}

0 commit comments

Comments
 (0)