Created
January 7, 2019 17:10
-
-
Save landjd19/57e0cbbf307a9d6aecfcf60bd88e1cc5 to your computer and use it in GitHub Desktop.
Selection Sort on 10 Primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class intSort | |
{ | |
public intSort(){ | |
int[] primes = new int[10]; | |
for(int i=0;i<=9;i++){ | |
int newNum = (int)((Math.random() * 1000) + 1); | |
if(isAPrime(newNum)){ | |
primes[i] = newNum; | |
}else{ | |
i--; | |
} | |
} | |
for(int i = 0; i < primes.length; i++){ | |
System.out.print(primes[i] + ", "); | |
} | |
SelectionSort(primes); | |
} | |
public boolean isAPrime(int y) | |
{ | |
int counter = 2; | |
while(counter<=y){ | |
if(y==2 || y==1){ | |
//System.out.println("This is a prime number!"); | |
return true; | |
}else if(y%counter == 0 && y != 2){ | |
//counter = 2; | |
//System.out.println("This is not a prime number"); | |
return false; | |
} else { | |
counter++; | |
if(counter == y){ | |
//System.out.println("This is a prime number!"); | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public void SelectionSort(int[] input){ | |
for(int i = 0; i < input.length; i++){ | |
int placeholder = input[i]; | |
int swapPlace = i; | |
for(int n = i + 1; n < input.length; n++){ | |
if(input[n] < placeholder){ | |
swapPlace = n; | |
placeholder = input[swapPlace]; | |
} | |
} | |
input[swapPlace] = input[i]; | |
input[i] = placeholder; | |
} | |
for(int i = 0; i < input.length; i++){ | |
System.out.println(input[i] + ", "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment