-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectSortApp.java
More file actions
77 lines (59 loc) · 982 Bytes
/
SelectSortApp.java
File metadata and controls
77 lines (59 loc) · 982 Bytes
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
75
76
77
class SelectSort
{
public long[] mArray;
public int nElems;
public SelectSort(int max)
{
mArray = new long[max];
nElems = 0;
}
public void insert(long value)
{
mArray[nElems] = value;
nElems++;
}
public void display()
{
for(int i=0;i<nElems;i++)
{
System.out.print(mArray[i]+" ");
}
System.out.println(" ");
}
//O(n^2)
public void sort()
{
int i,j;
int min;
for(i=0;i<nElems-1;i++)
{
min = i;
for(j=i+1;j<nElems;j++)
{
if(mArray[min]>mArray[j])
min = j;
}
swap(min,i);
}
}
private void swap(int i,int j)
{
long temp = mArray[i];
mArray[i] = mArray[j];
mArray[j] = temp;
}
}
class SelectSortApp
{
public static void main(String args[])
{
SelectSort arr = new SelectSort(10);
for(int j=0;j<10;j++)
{
arr.insert((long)(Math.random()*1000.0));
}
arr.display();
arr.sort();
arr.display();
}
}