Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Scanner;
- public class MySorts {
- public static void main (String[] args){
- int[] intArray = new int[5];
- //int unsortedData;
- int option;
- Scanner in = new Scanner(System.in);
- while (true){
- System.out.println("Choose which Sort type to use\n");
- System.out.println("(1): Option 1 - Insertions Sort\n(2): Option 2 - Bubble Sort\n(3): Option 3 - Selection Sort\n(4): Exit");
- //System.out.print("[1]"+ "Insertion Sort");
- // System.out.println("[2] BubbleSort");
- //System.out.println("[3] Selection Sort");
- option = in.nextInt();
- for (int i = 0; i < 5; i++){
- intArray[i] = Math.round( (float)Math.random()* 1000);
- //unsortedData = intArray[i];
- System.out.println("The unsorted data is: "+intArray[i]);//prints an array of random numbers
- }
- switch(option){
- case 1:System.out.println("Running option 1");
- insertionSort(intArray);break;
- case 2:System.out.println("Running option 2");
- bubbleSort(intArray);break;
- case 3:System.out.println("Running option 3");
- selectionSort(intArray);break;
- case 4:System.out.println("Ending Program");
- System.exit(0);break;
- default:
- System.out.print("Please enter a valid option 1, 2 or 3");break;
- }
- }}
- // }while(option != 4); //continue until user's requests to exit
- // if (option == 4)
- // System.exit(0);
- public static void insertionSort(int[] numbers) {
- int position, scan;
- int temp;
- for (int index = 1; index < numbers.length; index++)
- {
- temp = numbers[index];
- position = index;
- //move item up
- while(position > 0 && numbers[position - 1]>temp)
- {
- numbers[position]=numbers[position-1];
- position--;
- }
- numbers[position] = temp;
- }
- for (int i = 0;i<numbers.length;i++){
- System.out.println("The Insertionsorted data is: "+numbers[i]);
- }
- }
- public static void bubbleSort(int[] a) {
- int i, j;
- int lastExchangeIndex;
- int tmp;
- i = a.length - 1;
- // continue the process until no exchanges are made
- while (i > 0) {
- lastExchangeIndex = 0;
- for (j = 0; j < i; j++)
- {
- if (a[j + 1] < a[j])
- {
- // swap elements
- tmp = a[j + 1];
- a[j + 1] = a[j];
- a[j] = tmp;
- lastExchangeIndex = j;
- }
- }
- i = lastExchangeIndex;
- }
- for (int x = 0;x<a.length;x++){
- System.out.println("The Bubblesorted data is: "+a[x]);
- }
- }
- public static void selectionSort(int[] numbers)
- {
- int pos, temp;
- // work on list from 0 to n-1 items
- for (int index = 0; index< numbers.length-1; index++)
- {
- pos= index;
- for (int scan = index+1; scan < numbers.length; scan++)
- {
- if (numbers[scan]<numbers[pos])
- {
- pos = scan;
- }
- } // inner for
- // Swap the elements
- temp = numbers[pos];
- numbers[pos] = numbers[index];
- numbers[index] = temp;
- } // outer for
- for(int x = 0;x<numbers.length;x++){
- System.out.println("The SelectionSorted data is: "+numbers[x]);
- } // selectionSort
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement