Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mysort;
- import java.util.Random;
- import static mysort.Sort.SortMethod.BUBBLE;
- import static mysort.Sort.SortMethod.SELECTION;
- /**
- *
- * @author Hubert
- */
- public class Mysort extends Sort{
- int ilosc = 0;
- public static void main(String[] args) {
- Mysort m=new Mysort();
- System.out.println("===Wylosowane liczby===");
- m.wypelnij(5, 10);
- m.wypisz();
- m.sortuj(true, SELECTION);
- m.wypisz();
- m.sortuj(true, BUBBLE);
- m.wypisz();
- }
- //=====================================================1 zadanie
- @Override
- protected int losuj(int w_max) {
- Random random = new Random();
- return random.nextInt(w_max + 1);
- }
- @Override
- public void wypisz() {
- for (int i = 0; i < super.ile; i++) {
- System.out.print(super.tab[i] + " ");
- }
- System.out.println();
- System.out.println("ilosc porownan: " + ilosc);
- }
- @Override
- public void wypelnij(int n, int wartosc_maksymalna) {
- tab = new int[n];
- for (int i = 0; i < n; i++) {
- super.tab[i] = losuj(wartosc_maksymalna);
- super.ile++;
- }
- }
- //=====================================================2 zadanie
- private boolean sprawdzaj(int a, int b, boolean czy_rosnie) {
- ilosc++;
- if (czy_rosnie) {
- if (a > b) {
- return true;
- } else {
- return false;
- }
- } else {
- if (a < b) {
- return true;
- } else {
- return false;
- }
- }
- }
- @Override
- protected void selectionsort(boolean rosnaco) {
- System.out.println("===Selection sort===");
- for (int i = 0; i < tab.length - 1; i++) {
- int minimalna = i;
- for (int j = i + 1; j < tab.length; j++) {
- if (sprawdzaj(tab[j], tab[minimalna], rosnaco)) {
- minimalna= j;
- }
- int pomocnicza = tab[minimalna];
- tab[minimalna] = tab[i];
- tab[i] = pomocnicza;
- }
- }
- }
- //=====================================================3 zadanie
- @Override
- protected void insertsort(boolean rosnaco) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- //=====================================================4 zadanie
- @Override
- protected void bubblesort(boolean rosnaco) {
- System.out.println("===Bubble sort===");
- for (int i = 0; i < tab.length - 1; i++) {
- for (int j = 0; j < tab.length - 1; j++) {
- if (sprawdzaj(tab[j], tab[j + 1], rosnaco)) {
- int pomocnicza = tab[j];
- tab[j] = tab[j + 1];
- tab[j + 1] = pomocnicza;
- }//if
- }//for2
- }//for1
- }
- //=====================================================5 zadanie
- @Override
- public void sortuj(boolean rosnaco, SortMethod metoda) {
- switch (metoda) {
- case INSERTION:
- this.insertsort(rosnaco);
- break;
- case SELECTION:
- this.selectionsort(rosnaco);
- break;
- case BUBBLE:
- this.bubblesort(rosnaco);
- break;
- default:
- throw new RuntimeException();
- }
- }
- private void selectionsort(boolean b, SortMethod sortMethod) {
- System.out.println("===Selection sort===");
- for (int i = 0; i < tab.length - 1; i++) {
- int minimalna = i;
- for (int j = i + 1; j < tab.length; j++) {
- if (sprawdzaj(tab[j], tab[minimalna], b)) {
- minimalna= j;
- }
- int pomocnicza = tab[minimalna];
- tab[minimalna] = tab[i];
- tab[i] = pomocnicza;
- }
- } }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement