Insertion

/** * Something went wrong trying to go from lowest to highest instead of highest to lowest idrk * @author Charlie Barton * @version 1 */ import java.util.*; public class PrimeInsertion { public static void main(String[] args){ int count = 0; int[] nums = new int[10]; while(count < 10){ int random = (int)(Math.random() * 200 +1); if(isAPrime(random)){ nums[count] = random; count++; } } System.out.println(Arrays.toString(nums)); count = 0; insertionSort(nums); System.out.println(Arrays.toString(nums)); for(int i = 0; i<nums.length; i++){ System.out.print(" " + nums[i]); } } public static void insertionSort(int [] a){ for( int i=1; i<a.length; i++){ int key = a[i]; int j = i-1; while(j > 0&& a[j] < key) { a[j-1] = a[j]; j--; } a[j+1] =key; } } public static boolean isAPrime(int random) { boolean isAPrime=true; int num=2; while(num<random){//can do y/2 if(random%num==0) return false; num++; } return isAPrime; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.