-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
51 lines (40 loc) · 1.04 KB
/
SelectionSort.java
File metadata and controls
51 lines (40 loc) · 1.04 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
package javaapplication5;
import java.util.*;
/**
* Selection sort
*
* @author Brandon Salines
* @version 1
*/
public class SelectionSort
{
public static void main(String[] args){
int size = 10000000;
int [] array = new int [size];
Random rand = new Random();
//Add items to array
for (int i = 0; i < array.length; i ++){
array[i] = rand.nextInt(100) + 1;
}
bubbleSort(array);
}
public static void bubbleSort (int a []) {
int numPair = a.length;
boolean swapped = true;
int toSwap = 0;
int iterations = 1;
while (swapped){
numPair = numPair -1;
swapped = false;
for (int i = 0; i < numPair; i++){
if (a[i] > a [i+1]){
swapped = true;
toSwap = a[i];
a[i] = a[i+1];
a[i+1] = toSwap;
}
}
iterations = iterations +1;
}
}
}