Goodiny777

Targil 8.1 - 8.6

Feb 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.92 KB | None | 0 0
  1.  //Targil 8.1
  2.     public static void bubler(int [] arr) {
  3.         int ln = arr.length;
  4.         int temp = 0;
  5.         for (int i = 0; i < ln; i += 1) {
  6.             for (int j = 1; j < ln - i; j += 1) {
  7.                 if (arr[j - 1] > arr[j]) {
  8.                     temp = arr[j - 1];
  9.                     arr[j - 1] = arr[j];
  10.                     arr[j] = temp;
  11.                 }
  12.             }
  13.         }
  14.     }
  15.  
  16.   //Targil 8.2
  17.     public static void diagonalBubler(int [] [] arr, int row, int column) {
  18.         int diaLength = (row+column)/2; //to get a right amount of diagonal length row+column/2
  19.         int temp = 0;
  20.         for (int i = 0; i <diaLength; i += 1) {
  21.             for (int j = 1; j < diaLength - i; j += 1) {
  22.                 if(arr[j-1][j-1]>arr[j][j]) {
  23.                     temp = arr[j - 1][j-1];
  24.                     arr[j-1][j-1] = arr[j][j];
  25.                     arr[j][j] = temp;
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31. //Targil 8.3
  32.     public static String abSort(String str){
  33.         //Transformate our string to array of chars
  34.         char [] arrChar = str.toCharArray();
  35.         char temp;
  36.         String result="";
  37.        
  38.         //buble sort for array of chars
  39.         for(int i=0; i<arrChar.length; i+=1){
  40.             for(int j=1; j<arrChar.length; j+=1){
  41.                 if(isBigger(arrChar[j-1],arrChar[j])){
  42.                     temp = arrChar[j-1];
  43.                     arrChar[j-1] = arrChar[j];
  44.                     arrChar[j] = temp;
  45.                 }
  46.             }
  47.         }
  48.         //conver our array of chars to string
  49.         for(int i=0; i<arrChar.length; i+=1){
  50.             result+=arrChar[i];
  51.         }
  52.         return result;
  53.     }
  54.    
  55.     /*The function check if characters are in same case(Upper or lower) and if not makes them the same case to right compare. For case a sting contains just letters and no other symbols*/
  56.     public static boolean isBigger(char ch1, char ch2){
  57.         if(ch1>='A' && ch1<='Z'){
  58.             if(ch2>='a' && ch2<='z'){
  59.                 ch2-=32;//if first letter is upper and second lower makes second to lower
  60.             }
  61.         }else if(ch2>='A' && ch2<='Z'){
  62.             ch2+=32;//if first letter is lower and second is upper makes second to lower
  63.         }
  64.         return ch1>ch2?true:false;//compares already the same-case letters
  65.         }
  66.  
  67.  
  68. //Targil 8.4
  69.   public static void cord(int[][] arr, int num) {
  70.         boolean isNotFound = true;//In case we'll not find our number
  71.         for (int i = 0; i < arr.length; i += 1) {
  72.             for (int j = 0; j < arr.length; j += 1) {
  73.                 if (arr[j][i] == num) { //we should to check elements by row and just after all row to change a column
  74.                     System.out.printf("The number: %n found at row: %d and column: %d\n", i, j);//if number found
  75.                     isNotFound = false;//means the number found
  76.                 } else if (arr[j][i] > num) {
  77.                     /*if anumber in current row is bigger than our number
  78.                      it means we'll not find it in current column
  79.                      so we need to change our column
  80.                      (to bring a pointer to the last element in current column) */
  81.                     j = arr.length;
  82.                 }
  83.             }
  84.         }
  85.         //If number was't find prints massage
  86.         if (isNotFound) {
  87.             System.out.println("No matches");
  88.         }
  89.     }
  90.  
  91.  
  92. //Targil 8.5
  93.  
  94.  public static void matrixBubler(int[][] arr) {
  95.         int ln = arr.length;
  96.         int temp = 0;
  97.         //for 16 numbers we have 16 loops for every number to push him forward
  98.         for(int i=0; i<ln*ln-1; i+=1) {
  99.             //buble sort for first number in a matrix
  100.             for (int k = 0; k < ln; k += 1) {
  101.                 for (int j = 1; j < ln; j += 1) {
  102.                     //push number forward if it bigger than one forward to him
  103.                     if (arr[k][j - 1] > arr[k][j]) {
  104.                         temp = arr[k][j - 1];
  105.                         arr[k][j - 1] = arr[k][j];
  106.                         arr[k][j] = temp;
  107.                     }
  108.                     //when the last item in row compares him to first item in next row
  109.                     if ((j == ln - 1) && (k + 1 < ln) && (arr[k][j] > arr[k + 1][0])) {
  110.                         temp = arr[k + 1][0];
  111.                         arr[k + 1][0] = arr[k][j];
  112.                         arr[k][j] = temp;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.  
  120. //Targil 8.6
  121.  
  122.  public static void sortMatrixBySelection(int[][] arr) {
  123.         int ln = arr.length;//for just one request of array proprety
  124.         int minValue_row = 0, minValue_col = 0;//index holders for minimum value
  125.         int temp;
  126.         int x_pos;//save current x position
  127.         //outer loops gives us values one by one from first to last element of array. arr[i][j] is current value
  128.         for (int i = 0; i < ln; i += 1) {
  129.             for (int j = 0; j < ln; j += 1) {
  130.                 x_pos = j;//to star from lsat position in row
  131.                 //min indexes start from the index of last element we took with outer loops
  132.                 minValue_row = i;
  133.                 minValue_col = j;
  134.                 //in inner loops we search for a minimum value start from the last element we got with outer loops
  135.                 for (int y = i; y < ln; y += 1) {
  136.                     for (int x = x_pos; x < ln; x += 1) {
  137.                         //if any element less than curren minimum value replace minimum indexes
  138.                         if (arr[y][x] < arr[minValue_row][minValue_col]) {
  139.                             minValue_row = y;
  140.                             minValue_col = x;
  141.                         }
  142.                     }
  143.                     x_pos = 0;//to start next line from first object in a row
  144.                 }
  145.                 //replace current value and min value
  146.                 temp = arr[minValue_row][minValue_col];
  147.                 arr[minValue_row][minValue_col] = arr[i][j];
  148.                 arr[i][j] = temp;
  149.             }
  150.         }
  151.     }
  152.  
  153.  
  154. =========MAIN====================================================================
  155.  
  156. //Targil 8.1
  157.         int [] array = {70, 66, 79, 63, 68, 60, 67, 71};
  158.         bubler(array);
  159.         for(int i=0; i < array.length; i++){
  160.             System.out.print(array[i] + " ");
  161.         }
  162.  
  163.  //Targil 8.2
  164.         int rows = 4, columns = 4;
  165.         int [] [] array ={{9,3,6,5},
  166.                          {2,4,8,2},
  167.                          {7,2,2,1},
  168.                          {9,8,3,6}};
  169.         diagonalBubler(array, rows, columns);
  170.         for(int i = 0; i<columns; i+=1) {
  171.             for (int j = 0; j < rows; j += 1) {
  172.                 System.out.print(array[i][j]);
  173.             }
  174.             System.out.println();
  175.         }
  176.  
  177. //Targil 8.3
  178.         String str ="zdhyTbjCK";
  179.         System.out.println(abSort(str));
  180.  
  181. //Targil 8.4
  182.         int [][] arr = {{2,1,5,1}, {3,6,6,2}, {8,7,7,3}, {9,8,9,4}}; //sorted array
  183.         int num = 7;//number to find
  184.         cord(arr, num);
  185.  
  186. //Targil 8.5
  187.         int [][] array = {{16, 15, 14, 13},
  188.                           {12, 11, 10, 9},
  189.                           {8, 7, 6, 5},
  190.                           {4, 3, 2, 1}};
  191.         //sorting array
  192.         matrixBubler(array);
  193.         //print array after sorting
  194.         for (int i = 0; i < array.length; i += 1) {
  195.             for (int j = 0; j < array.length; j += 1) {
  196.                 System.out.print(array[i][j]+" ");
  197.             }
  198.             System.out.println();
  199.         }
  200.  
  201.  
  202. //Targil 8.6
  203.          int[][] someArray = {{16, 15, 14, 13},
  204.                             {12, 11, 10, 9},
  205.                             {8, 7, 6, 5},
  206.                             {4, 3, 2, 1}};
  207.  
  208.         sortMatrixBySelection(someArray);
  209.         for (int i = 0; i < someArray.length; i += 1) {
  210.             for (int j = 0; j < someArray.length; j += 1) {
  211.                 System.out.print(someArray[i][j] + " ");
  212.             }
  213.             System.out.println();
  214.         }
Advertisement
Add Comment
Please, Sign In to add comment