-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
74 lines (63 loc) · 1.55 KB
/
Copy pathSelectionSort.java
File metadata and controls
74 lines (63 loc) · 1.55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package MyPractice;
public class SelectionSort {
static int count=0;
public static void main(String [] args)
{
//int [] a = {10,8,3,7,6,4,2,5,1,9};
int [] a = {1,2,3,4,5,6,7,8,9,10};
// int [] sortedArray = selectionSort(a,0);
int [] sortedArray = bubble(a);
print(sortedArray);
System.out.println("Passes taken : " + count);
}
public static void print(int [] ar)
{
for(int i=0;i<ar.length;i++)
{
System.out.println(ar[i]);
}
}
public static int [] selectionSort(int [] ar,int left)
{
int right=ar.length-1;
if(left==right)
return ar;
int minPos = findMin(left,right,ar);
swap(ar,left,minPos);
return selectionSort(ar,left+1);
}
public static int findMin(int left,int right,int [] ar)
{
int min=left;
left++;
while(left<=right)
{
if(ar[left]<ar[min])
min = left;
left++;
// count++;
}
return min;
}
public static void swap(int [] ar, int index1, int index2)
{
int temp = ar[index1];
ar[index1] = ar[index2];
ar[index2]=temp;
count++;
}
public static int [] bubble(int [] ar)
{
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar.length-i-1;j++)
{
if(ar[j]>ar[j+1])
{
swap(ar,j,j+1);
}
}
}
return ar;
}
}