forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort.java
More file actions
49 lines (41 loc) · 1.3 KB
/
Selection_Sort.java
File metadata and controls
49 lines (41 loc) · 1.3 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
//https://www.facebook.com/permalink.php?story_fbid=2750473708542571&id=100007399066161
//Subscribed by tharindu Rewatha
class JavaExample
{
void selectionSort(int arr[])
{
int len = arr.length;
for (int i = 0; i < len-1; i++)
{
// Finding the minimum element in the unsorted part of array
int min = i;
for (int j = i+1; j < len; j++)
if (arr[j] < arr[min])
min = j;
/* Swapping the found minimum element with the first
* element of the sorted subarray using temp variable
*/
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
// Displays the array elements
void printArr(int arr[])
{
for (int i=0; i<arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
JavaExample obj = new JavaExample();
int numarr[] = {101,5,18,11,80, 67};
System.out.print("Original array: ");
obj.printArr(numarr);
//calling method for selection sorting
obj.selectionSort(numarr);
System.out.print("Sorted array: ");
obj.printArr(numarr);
}
}