Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. * and open the template in the editor.
  2. */
  3. package lab12;
  4.  
  5. /**
  6. *
  7. * @author Seko
  8. */
  9. public class Lab12
  10. {
  11.  
  12. /**
  13. * @param args the command line arguments
  14. */
  15. public static void main ( String[] args )
  16. {
  17. Zadanie1 zad1 = new Zadanie1 ( "bbabbababaabaababa", "aba" );
  18. zad1.naiwny (true);
  19.  
  20. Zadanie2 zad2 = new Zadanie2 ( 10 );
  21. zad2.MergeSort ();
  22. }
  23.  
  24. }
  25.  
  26.  
  27.  
  28. package lab12;
  29.  
  30. /**
  31. *
  32. * @author Seko
  33. */
  34. public class Zadanie1
  35. {
  36.  
  37. public String tekst;
  38. public String wzorzec;
  39.  
  40. public Zadanie1 ( String tekst, String wzorzec )
  41. {
  42. this.tekst = tekst;
  43. this.wzorzec = wzorzec;
  44. }
  45.  
  46. public void naiwny ( boolean podkreslenie )
  47. {
  48. System.out.println ( "Tekst: \t\t" + this.tekst );
  49. System.out.println ( "Wzorzec: \t" + this.wzorzec );
  50.  
  51. System.out.println ( podkreslenie ? this.tekst : "Wzorzec zaczyna siÄ™ na indeksach numer:" );
  52.  
  53. int ilosc = 0;
  54.  
  55. for ( int i = 0; i <= this.tekst.length () - this.wzorzec.length (); ++ i )
  56. {
  57. int j = 0;
  58. while ( j < this.wzorzec.length () && this.tekst.charAt ( i + j ) == this.wzorzec.charAt ( j ) )
  59. {
  60. ++ j;
  61. }
  62.  
  63. if ( j == this.wzorzec.length () )
  64. {
  65. System.out.print ( podkreslenie ? "^" : i + ", " );
  66. ++ ilosc;
  67. }
  68. else if ( podkreslenie )
  69. {
  70. System.out.print ( " " );
  71. }
  72.  
  73. }
  74.  
  75. System.out.println ( "\nIlość: " + ilosc + "\n" );
  76. }
  77.  
  78. }
  79.  
  80.  
  81.  
  82. package lab12;
  83.  
  84. import java.util.Arrays;
  85. import java.util.Random;
  86.  
  87. /**
  88. *
  89. * @author Seko
  90. */
  91. public class Zadanie2
  92. {
  93.  
  94. public int[] tab;
  95. public int[] tmp;
  96.  
  97. public Zadanie2 ( int n )
  98. {
  99. this.tmp = new int[ n ];
  100. Random rand = new Random ();
  101. this.tab = new int[ n ];
  102. for ( int i = 0; i < n; ++ i )
  103. {
  104. this.tab[ i ] = rand.nextInt ( 100 );
  105. }
  106. }
  107.  
  108. public void MergeSort ()
  109. {
  110. System.out.println ( "Przed sortowaniem:\n" + Arrays.toString ( this.tab ) );
  111. MergeSort ( 0, this.tab.length - 1 );
  112. System.out.println ( "Po sortowaniu:\n" + Arrays.toString ( this.tab ) );
  113. }
  114.  
  115. private void MergeSort ( int indexPoczatkowy, int indexKoncowy )
  116. {
  117. int indexSrodek = ( indexPoczatkowy + indexKoncowy + 1 ) / 2;
  118.  
  119. if ( indexSrodek - indexPoczatkowy > 1 )
  120. {
  121. MergeSort ( indexPoczatkowy, indexSrodek - 1 );
  122. }
  123. if ( indexKoncowy - indexSrodek > 0 )
  124. {
  125. MergeSort ( indexSrodek, indexKoncowy );
  126. }
  127.  
  128. int indexLewego = indexPoczatkowy;
  129. int indexPrawego = indexSrodek;
  130.  
  131. for ( int i = indexPoczatkowy; i <= indexKoncowy; ++ i )
  132. {
  133. this.tmp[ i ] = ( ( indexLewego == indexSrodek ) || ( indexPrawego <= indexKoncowy && this.tab[ indexLewego ] > this.tab[ indexPrawego ] ) ) ? this.tab[ indexPrawego ++ ] : this.tab[ indexLewego ++ ];
  134. }
  135.  
  136. for ( int i = indexPoczatkowy; i <= indexKoncowy; ++ i )
  137. {
  138. this.tab[ i ] = this.tmp[ i ];
  139. }
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement