Skip to content

Commit bd42abc

Browse files
authored
Updated SelectionSort.java
Updated SelectionSort.java to its generic version
1 parent affcbac commit bd42abc

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

Sorts/SelectionSort.java

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,73 @@
1-
import java.util.Scanner;
2-
31
/**
4-
* This class implements Selection Sort
5-
*
6-
* @author Unknown
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
74
*
85
*/
96

10-
class SelectionSort
11-
{
12-
/**
13-
* Main method
14-
*
15-
* @param args Command line arguments
16-
*/
17-
public static void main(String[] args)
18-
{
19-
int array[]=new int[6];
20-
Scanner input=new Scanner(System.in);
21-
22-
//Input
23-
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
24-
for(int i=0; i<6; i++)
25-
{
26-
array[i]=input.nextInt();
27-
}
28-
29-
//Sorting
30-
for(int i=0; i<6; i++)
31-
{
32-
int min=i;
33-
for(int j=i+1; j<6; j++)
34-
{
35-
if(array[j]<array[min])
36-
{
37-
min=j;
38-
}
39-
}
40-
int temp=array[i];
41-
array[i]=array[min];
42-
array[min]=temp;
43-
}
44-
45-
//Output
46-
for(int i=0; i<6; i++)
47-
{
48-
System.out.print(array[i]+"\t");
49-
}
50-
51-
input.close();
52-
}
7+
public class SelectionSort {
8+
9+
/**
10+
* This method implements the Generic Selection Sort
11+
*
12+
* @param arr The array to be sorted
13+
* @param n The count of total number of elements in array
14+
* Sorts the array in increasing order
15+
**/
16+
17+
public static <T extends Comparable<T>> void SS(T[] arr, int n) {
18+
19+
for (int i=0;i<n-1;i++) {
20+
21+
// Initial index of min
22+
int min = i;
23+
24+
for (int j=i+1;j<n;j++) {
25+
if (arr[j].compareTo(arr[min]) < 0) {
26+
min = j;
27+
}
28+
}
29+
30+
// Swapping if index of min is changed
31+
if (min != i) {
32+
T temp = arr[i];
33+
arr[i] = arr[min];
34+
arr[min] = temp;
35+
}
36+
}
37+
}
38+
39+
// Driver Program
40+
public static void main(String[] args) {
41+
42+
// Integer Input
43+
int[] arr1 = {4,23,6,78,1,54,231,9,12};
44+
int n = arr1.length;
45+
46+
Integer[] array = new Integer[n];
47+
for (int i=0;i<n;i++) {
48+
array[i] = arr1[i];
49+
}
50+
51+
SS(array, n);
52+
53+
// Output => 1 4 6 9 12 23 54 78 231
54+
for(int i=0; i<n; i++)
55+
{
56+
System.out.print(array[i]+"\t");
57+
}
58+
59+
System.out.println();
60+
61+
// String Input
62+
String[] array1 = {"c", "a", "e", "b","d"};
63+
n = array1.length;
64+
65+
SS(array1, n);
66+
67+
//Output => a b c d e
68+
for(int i=0; i<n; i++)
69+
{
70+
System.out.print(array1[i]+"\t");
71+
}
72+
}
5373
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy