Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * and open the template in the editor.
- */
- package lab12;
- /**
- *
- * @author Seko
- */
- public class Lab12
- {
- /**
- * @param args the command line arguments
- */
- public static void main ( String[] args )
- {
- Zadanie1 zad1 = new Zadanie1 ( "bbabbababaabaababa", "aba" );
- zad1.naiwny (true);
- Zadanie2 zad2 = new Zadanie2 ( 10 );
- zad2.MergeSort ();
- }
- }
- package lab12;
- /**
- *
- * @author Seko
- */
- public class Zadanie1
- {
- public String tekst;
- public String wzorzec;
- public Zadanie1 ( String tekst, String wzorzec )
- {
- this.tekst = tekst;
- this.wzorzec = wzorzec;
- }
- public void naiwny ( boolean podkreslenie )
- {
- System.out.println ( "Tekst: \t\t" + this.tekst );
- System.out.println ( "Wzorzec: \t" + this.wzorzec );
- System.out.println ( podkreslenie ? this.tekst : "Wzorzec zaczyna siÄ™ na indeksach numer:" );
- int ilosc = 0;
- for ( int i = 0; i <= this.tekst.length () - this.wzorzec.length (); ++ i )
- {
- int j = 0;
- while ( j < this.wzorzec.length () && this.tekst.charAt ( i + j ) == this.wzorzec.charAt ( j ) )
- {
- ++ j;
- }
- if ( j == this.wzorzec.length () )
- {
- System.out.print ( podkreslenie ? "^" : i + ", " );
- ++ ilosc;
- }
- else if ( podkreslenie )
- {
- System.out.print ( " " );
- }
- }
- System.out.println ( "\nIlość: " + ilosc + "\n" );
- }
- }
- package lab12;
- import java.util.Arrays;
- import java.util.Random;
- /**
- *
- * @author Seko
- */
- public class Zadanie2
- {
- public int[] tab;
- public int[] tmp;
- public Zadanie2 ( int n )
- {
- this.tmp = new int[ n ];
- Random rand = new Random ();
- this.tab = new int[ n ];
- for ( int i = 0; i < n; ++ i )
- {
- this.tab[ i ] = rand.nextInt ( 100 );
- }
- }
- public void MergeSort ()
- {
- System.out.println ( "Przed sortowaniem:\n" + Arrays.toString ( this.tab ) );
- MergeSort ( 0, this.tab.length - 1 );
- System.out.println ( "Po sortowaniu:\n" + Arrays.toString ( this.tab ) );
- }
- private void MergeSort ( int indexPoczatkowy, int indexKoncowy )
- {
- int indexSrodek = ( indexPoczatkowy + indexKoncowy + 1 ) / 2;
- if ( indexSrodek - indexPoczatkowy > 1 )
- {
- MergeSort ( indexPoczatkowy, indexSrodek - 1 );
- }
- if ( indexKoncowy - indexSrodek > 0 )
- {
- MergeSort ( indexSrodek, indexKoncowy );
- }
- int indexLewego = indexPoczatkowy;
- int indexPrawego = indexSrodek;
- for ( int i = indexPoczatkowy; i <= indexKoncowy; ++ i )
- {
- this.tmp[ i ] = ( ( indexLewego == indexSrodek ) || ( indexPrawego <= indexKoncowy && this.tab[ indexLewego ] > this.tab[ indexPrawego ] ) ) ? this.tab[ indexPrawego ++ ] : this.tab[ indexLewego ++ ];
- }
- for ( int i = indexPoczatkowy; i <= indexKoncowy; ++ i )
- {
- this.tab[ i ] = this.tmp[ i ];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement