Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Scanner;
  3. public class MySorts {
  4.  
  5.  
  6.  
  7.     public static void main (String[] args){
  8.  
  9.         int[] intArray = new int[5];
  10.         //int unsortedData;
  11.         int option;
  12.         Scanner in = new Scanner(System.in);
  13.        
  14.        
  15.            
  16.        
  17.         while (true){
  18.     System.out.println("Choose which Sort type to use\n");
  19.     System.out.println("(1): Option 1 - Insertions Sort\n(2): Option 2 - Bubble Sort\n(3): Option 3 - Selection Sort\n(4): Exit");
  20.    
  21.         //System.out.print("[1]"+ "Insertion Sort");
  22. //  System.out.println("[2] BubbleSort");
  23.     //System.out.println("[3] Selection Sort");
  24.      option = in.nextInt();
  25.         for (int i = 0; i < 5; i++){
  26.             intArray[i] = Math.round( (float)Math.random()* 1000);
  27.             //unsortedData = intArray[i];
  28.             System.out.println("The unsorted data is: "+intArray[i]);//prints an array of random numbers
  29.         }
  30.        
  31.        
  32.         switch(option){
  33.         case 1:System.out.println("Running option 1");
  34.         insertionSort(intArray);break;
  35.         case 2:System.out.println("Running option 2");
  36.         bubbleSort(intArray);break;
  37.         case 3:System.out.println("Running option 3");
  38.         selectionSort(intArray);break;
  39.         case 4:System.out.println("Ending Program");
  40.        
  41.        
  42.         System.exit(0);break;
  43.         default:
  44.             System.out.print("Please enter a valid option 1, 2 or 3");break;
  45.         }
  46.         }}
  47.    
  48.        
  49. //      }while(option != 4); //continue until user's requests to exit
  50. //      if (option == 4)
  51. //          System.exit(0);
  52.    
  53.        
  54.        
  55.    
  56.    
  57.     public static void insertionSort(int[] numbers) {
  58.         int position, scan;
  59.         int temp;
  60.  
  61.         for (int index = 1; index < numbers.length; index++)
  62.         {
  63.             temp = numbers[index];
  64.             position = index;
  65.             //move item up
  66.             while(position > 0 && numbers[position - 1]>temp)
  67.             {
  68.                 numbers[position]=numbers[position-1];
  69.                 position--;
  70.             }
  71.             numbers[position] = temp;
  72.        
  73.         }
  74.        
  75.         for (int i = 0;i<numbers.length;i++){
  76.         System.out.println("The Insertionsorted data is: "+numbers[i]);
  77.         }
  78.     }
  79.    
  80.    
  81.     public static void bubbleSort(int[] a) {
  82.         int i, j;
  83.         int lastExchangeIndex;
  84.         int tmp;
  85.         i = a.length - 1;
  86.         // continue the process until no exchanges are made
  87.         while (i > 0) {
  88.             lastExchangeIndex = 0;
  89.             for (j = 0; j < i; j++)
  90.             {          
  91.                 if (a[j + 1] < a[j])
  92.                 {
  93.                     // swap elements
  94.                     tmp = a[j + 1];
  95.                     a[j + 1] = a[j];
  96.                     a[j] = tmp;
  97.                     lastExchangeIndex = j;
  98.                 }
  99.             }
  100.             i = lastExchangeIndex;
  101.         }
  102.         for (int x = 0;x<a.length;x++){
  103.             System.out.println("The Bubblesorted data is: "+a[x]);
  104.         }
  105.     }
  106.  
  107.    
  108.    
  109.    
  110.     public static void selectionSort(int[] numbers)
  111.     {
  112.         int pos, temp;  
  113.        
  114.         // work on list from 0 to n-1 items
  115.         for (int index = 0; index< numbers.length-1; index++)
  116.             {
  117.                     pos= index;
  118.                     for (int scan = index+1; scan < numbers.length; scan++)
  119.                     {
  120.             if (numbers[scan]<numbers[pos])
  121.             {
  122.                 pos = scan;
  123.             }
  124.       } // inner for
  125.                 // Swap the elements
  126.                 temp = numbers[pos];
  127.                 numbers[pos] = numbers[index];
  128.                 numbers[index] = temp;            
  129.    
  130.             } // outer for
  131.    
  132.         for(int x = 0;x<numbers.length;x++){
  133.             System.out.println("The SelectionSorted data is: "+numbers[x]);
  134.         } // selectionSort
  135. }      
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement