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 lab11aiz;
- import java.util.Random;
- /**
- *
- * @author Hubert
- */
- public class Lab11aiz extends Sort2{
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Lab11aiz sort=new Lab11aiz();
- Lab11aiz Qsort =new Lab11aiz();
- sort.wypelnij(10, 10);
- System.out.println("===Tablica wylosowanych liczb===");
- sort.wypisz();
- System.out.println("===Forma kopca===");
- sort.construct();
- sort.wypiszDrzewo();
- System.out.println("===heapSort===");
- sort.heapSort();
- //====================================
- Qsort.wypelnij(10, 10);
- System.out.println("===Tablica wylosowanych liczb===");
- Qsort.wypisz();
- System.out.println("===QUICKSORT===");
- Qsort.quicksort();
- Qsort.wypisz();
- }
- @Override
- protected int losuj(int w_max) {
- Random random=new Random();
- return random.nextInt(w_max)+1;
- }
- @Override
- public void wypelnij(int n, int wartosc_maksymalna) {
- tab = new int [n];
- ileT=n;
- for( int i=0; i<ileT; i++) {
- tab[i] = losuj(wartosc_maksymalna);
- }
- }
- @Override
- public void wypisz() {
- for(int i=0;i<ileT;i++)
- System.out.println("Tablica["+i+"]= "+tab[i]);
- }
- @Override
- public void wypiszDrzewo() {
- int p = 0;
- while (Math.pow(2, p) <= ileT) {
- p++;
- }
- p--;
- for (int i = 0; i < p + 1; i++) {
- int k = p - i;
- if (k != 0) {
- for (int b = 0; b <= Math.pow(2, k) - 2; b++) {
- System.out.print(" ");
- }
- }
- for (int j = (int) Math.pow(2, i) - 1; j < (int) Math.pow(2, i + 1) - 1; j++) {
- if (j < ileT) {
- System.out.print(tab[j] + " ");
- for (int m = 0; m < Math.pow(2, p - i + 1); m++) {
- System.out.print(" ");
- }
- for (int b = 0; b <= Math.pow(2, k) - 2; b++) {
- System.out.print(" ");
- }
- }
- }
- System.out.println("");
- }
- }
- @Override
- protected void downHeap(int i) {
- int v, j;
- v= tab[i];
- while(i <= ((ileT-1)/2)) {
- j = 2*i;
- if(j<ileT) {
- if(tab[j]<tab[j+1]) {
- j = j+1;
- }
- }
- if(v >= tab[j]) break;
- tab[i] = tab[j];
- i=j;
- }
- tab[i]=v;
- }
- @Override
- protected void construct() {
- for (int i = ((ileT - 1) / 2); i >= 0; i--) {
- downHeap(i);
- }
- }
- @Override
- protected int deleteMax() {
- int tmp = tab[0];
- tab[0] = tab[ileT - 1];
- ileT--;
- downHeap(0);
- return tmp;
- }
- @Override
- protected void heapSort() {
- int m = ileT;
- construct();
- for(int i = m-1; i>=1; i--) {
- tab[i] = deleteMax();
- wypiszDrzewo();
- System.out.println("===========nastepne przejscie===");
- }
- ileT = m;
- }
- @Override
- protected void quicksort() {
- quicksort(0, ileT - 1);
- }
- public void quicksort(int x,int y){
- int i = x, j = y, v, pomocnicza;
- v = tab[j];
- do{
- while (tab[i] < v) {
- i++;
- }
- while (v < tab[j]) {
- j--;
- }
- if(i<=j)
- {
- pomocnicza=tab[i];
- tab[i]=tab[j];
- tab[j]=pomocnicza;
- i++;j--;
- }
- }while(i <= j);
- if (x < j) {
- quicksort(x, j);
- }
- if (i < y) {
- quicksort(i, y);
- }
- }
- @Override
- protected void countsort() {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement