Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class combinationSorter {
- public static int arraySize = 10;
- public static int first;
- public static int second;
- public static int small = 0;
- public static int selection1[] = new int[arraySize];
- public static int selection2[] = new int[arraySize];
- public static int bubble[] = new int[arraySize];
- public static int insertion[] = new int[arraySize];
- public static Random rand = new Random();
- public static int bubbleComparison = 0;
- public static int selectionComparison = 0;
- public static int insertionComparison = 0;
- public static int timesChecked = 0;
- public static int size = 0;
- public static void pause(int time)
- {
- try
- {
- Thread.sleep(time);
- }
- catch(InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }
- }
- public static void selectionSort(){
- small = selection1[0];
- int place = 0;
- for(int outerLoop=0; outerLoop<arraySize;outerLoop++){
- for(int innerLoop=0; innerLoop<arraySize; innerLoop++)
- {
- if((selection1[innerLoop]<=small) && (selection1[innerLoop]>0))
- {
- small = selection1[innerLoop];
- place=innerLoop;
- }
- selectionComparison++;
- }
- selection2[outerLoop]=small;
- selection1[place]=0;
- small=Integer.MAX_VALUE;
- }
- }
- public static void bubbleSort(){
- while(true)
- {
- timesChecked = 0;
- for(int bubbleLoop=0; bubbleLoop<arraySize-1;bubbleLoop++){
- if(bubble[bubbleLoop]>bubble[bubbleLoop+1])
- {
- first = bubble[bubbleLoop];
- second = bubble[(bubbleLoop)+1];
- bubble[bubbleLoop]=second;
- bubble[(bubbleLoop)+1] = first;
- timesChecked++;
- }
- bubbleComparison++;
- }
- if(timesChecked==0)
- {
- break;
- }
- }
- }
- public static void insertionSort(){
- while(size!=arraySize){
- for(int i=size;i>0;i--){
- if(insertion[i]>insertion[i-1]){
- break;
- }
- else {
- int temp=insertion[i];
- insertion[i]=insertion[i-1];
- insertion[i-1]=temp;
- }
- insertionComparison++;
- }
- size++;
- }
- }
- public static void main(String args[]) {
- for (int randomNumbers = 0; randomNumbers<arraySize; randomNumbers++){
- selection1[randomNumbers] = rand.nextInt(100)+1;
- bubble[randomNumbers] = selection1[randomNumbers];
- insertion[randomNumbers] = selection1[randomNumbers];
- }
- /*
- for (int displayRandomNumbers = 0; displayRandomNumbers<arraySize; displayRandomNumbers++){
- System.out.println(bubble[displayRandomNumbers]);
- }
- */
- selectionSort();
- bubbleSort();
- insertionSort();
- System.out.println("---------------");
- System.out.println("Selection Comparisons: "+selectionComparison);
- System.out.println("Bubble Comparisons: "+bubbleComparison);
- System.out.println("Insertion Comparisons: "+insertionComparison);
- System.out.println("---------------");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement