Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main
- package Package;
- public class Main {
- public static void main(String[] args)
- {
- StrategyForSorting ObjStategy = new StrategyForSorting();
- ObjStategy.Menu();
- }
- }
- //StrategyForSorting
- package Package;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.List;
- import java.util.Scanner;
- public class StrategyForSorting extends BaseSort
- {
- void Menu() {
- List<Integer> ListToSort = new ArrayList<Integer>();
- InputNumbers(ListToSort);
- int[] ArrayToSort = ListToSort.stream().mapToInt(i->i).toArray();
- int ChoosenOption;
- do {
- ChoosenOption = MenuChoice();
- switch (ChoosenOption) {
- case 1:
- new BubbleSort().Sort(ArrayToSort);
- Display(ArrayToSort);
- break;
- case 2:
- new InsertionSort().Sort(ArrayToSort);
- Display(ArrayToSort);
- break;
- case 3:
- new MergeSort().Sort(ArrayToSort);
- Display(ArrayToSort);
- break;
- case 4:
- new ShuffleUnsort().Sort(ArrayToSort);
- Display(ArrayToSort);
- break;
- case 5:
- break;
- }
- }while(ChoosenOption != 5);
- }
- void Display(int ArrayToSort[])
- {
- for(int Number : ArrayToSort)
- {
- System.out.print(Number + ", ");
- }
- System.out.println("");
- }
- void InputNumbers(List<Integer> ListToSort)
- {
- boolean InputOK;
- Scanner UserInput = new Scanner(System.in);
- do {
- InputOK = true;
- ListToSort.clear();
- System.out.println("Podaj kolejno liczby które chcesz po sortowac. Nalezy je podzielic za pomoca \' , \'");
- String StrUserInput = UserInput.nextLine();
- String[] GivenNumbers = StrUserInput.split(" , ");
- try {
- for (String GivenNumber : GivenNumbers) {
- ListToSort.add(Integer.parseInt(GivenNumber));
- }
- } catch (Exception e) {
- InputOK = false;
- System.out.println("Podano nieprawidlowe dane.");
- }
- }while (!InputOK);
- }
- int MenuChoice() {
- boolean InputOK;
- int ChoosenOption = 0;
- do {
- InputOK = true;
- System.out.println("1.Sortowanie Bombelkowe");
- System.out.println("2.Sortowanie Przez Wstawianie");
- System.out.println("3.Sortowanie Przez Scalanie");
- System.out.println("4.Rozsortowanie");
- System.out.println("5.Wyjscie");
- System.out.println("Wprowadz numer opcji menu: ");
- try {
- Scanner UserInput = new Scanner(System.in);
- ChoosenOption = Integer.parseInt(UserInput.nextLine()); //Zamiana String do Int
- } catch (InputMismatchException | NumberFormatException ex) // Sprawdzenie
- {
- System.out.println("To nie jest liczba.");
- InputOK = false;
- }
- }while (!InputOK && ChoosenOption > 5 || ChoosenOption < 0 );
- return ChoosenOption;
- }
- }
- //BaseSort
- package Package;
- import java.util.List;
- public abstract class BaseSort {
- ISort ISortObj;
- public void Sort(int ListToSort[])
- {
- ISortObj.Sort(ListToSort);
- }
- }
- //Isort
- package Package;
- import java.util.List;
- public interface ISort {
- void Sort(int ListToSort[]);
- }
- //BubbleSort
- package Package;
- import java.util.List;
- public class BubbleSort implements ISort{
- @Override
- public void Sort(int ListToSort[])
- {
- int Lenght = ListToSort.length;
- int i, j, t;
- for (i = 0; i < Lenght; i++)
- {
- for (j = i + 1; j < Lenght; j++)
- {
- if (ListToSort[j] < ListToSort[i])
- {
- t = ListToSort[i];
- ListToSort[i] = ListToSort[j];
- ListToSort[j] = t;
- }
- }
- }
- }
- }
- //InsertionSort
- package Package;
- import java.util.List;
- public class InsertionSort implements ISort{
- @Override
- public void Sort(int ListToSort[])
- {
- int Lenght = ListToSort.length;
- for (int j = 1; j < Lenght; j++) {
- int Key = ListToSort[j];
- int i = j-1;
- while ( (i > -1) && ( ListToSort [i] > Key ) ) {
- ListToSort [i+1] = ListToSort [i];
- i--;
- }
- ListToSort[i+1] = Key;
- }
- }
- }
- //MergeSort
- package Package;
- import java.util.List;
- public class MergeSort implements ISort{
- @Override
- public void Sort(int ListToSort[])
- {
- int n = ListToSort.length;
- mergeSort(ListToSort,0,n-1);
- }
- void merge(int a[], int beg, int mid, int end)
- {
- int i, j, k;
- int n1 = mid - beg + 1;
- int n2 = end - mid;
- /* temporary Arrays */
- int LeftArray[] = new int[n1];
- int RightArray[] = new int[n2];
- /* copy data to temp arrays */
- for (i = 0; i < n1; i++)
- LeftArray[i] = a[beg + i];
- for (j = 0; j < n2; j++)
- RightArray[j] = a[mid + 1 + j];
- i = 0; /* initial index of first sub-array */
- j = 0; /* initial index of second sub-array */
- k = beg; /* initial index of merged sub-array */
- while (i < n1 && j < n2)
- {
- if(LeftArray[i] <= RightArray[j])
- {
- a[k] = LeftArray[i];
- i++;
- }
- else
- {
- a[k] = RightArray[j];
- j++;
- }
- k++;
- }
- while (i<n1)
- {
- a[k] = LeftArray[i];
- i++;
- k++;
- }
- while (j<n2)
- {
- a[k] = RightArray[j];
- j++;
- k++;
- }
- }
- void mergeSort(int a[], int beg, int end)
- {
- if (beg < end)
- {
- int mid = (beg + end) / 2;
- mergeSort(a, beg, mid);
- mergeSort(a, mid + 1, end);
- merge(a, beg, mid, end);
- }
- }
- }
- //ShuffleUnsort
- package Package;
- import java.util.List;
- import java.util.Random;
- public class ShuffleUnsort implements ISort{
- @Override
- public void Sort(int ListToSort[])
- {
- // Creating object for Random class
- Random RandomObj = new Random();
- int Lenght = ListToSort.length;
- // Starting from the last element and swapping one by one.
- for (int FirstC = Lenght-1; FirstC > 0; FirstC--) {
- // Pick a random index from 0 to i
- int SecondC = RandomObj.nextInt(FirstC+1);
- // Swap array[i] with the element at random index
- int Temp = ListToSort[FirstC];
- ListToSort[FirstC] = ListToSort[SecondC];
- ListToSort[SecondC] = Temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement