Advertisement
Guest User

Untitled

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