Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Targil 8.1
- public static void bubler(int [] arr) {
- int ln = arr.length;
- int temp = 0;
- for (int i = 0; i < ln; i += 1) {
- for (int j = 1; j < ln - i; j += 1) {
- if (arr[j - 1] > arr[j]) {
- temp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = temp;
- }
- }
- }
- }
- //Targil 8.2
- public static void diagonalBubler(int [] [] arr, int row, int column) {
- int diaLength = (row+column)/2; //to get a right amount of diagonal length row+column/2
- int temp = 0;
- for (int i = 0; i <diaLength; i += 1) {
- for (int j = 1; j < diaLength - i; j += 1) {
- if(arr[j-1][j-1]>arr[j][j]) {
- temp = arr[j - 1][j-1];
- arr[j-1][j-1] = arr[j][j];
- arr[j][j] = temp;
- }
- }
- }
- }
- //Targil 8.3
- public static String abSort(String str){
- //Transformate our string to array of chars
- char [] arrChar = str.toCharArray();
- char temp;
- String result="";
- //buble sort for array of chars
- for(int i=0; i<arrChar.length; i+=1){
- for(int j=1; j<arrChar.length; j+=1){
- if(isBigger(arrChar[j-1],arrChar[j])){
- temp = arrChar[j-1];
- arrChar[j-1] = arrChar[j];
- arrChar[j] = temp;
- }
- }
- }
- //conver our array of chars to string
- for(int i=0; i<arrChar.length; i+=1){
- result+=arrChar[i];
- }
- return result;
- }
- /*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*/
- public static boolean isBigger(char ch1, char ch2){
- if(ch1>='A' && ch1<='Z'){
- if(ch2>='a' && ch2<='z'){
- ch2-=32;//if first letter is upper and second lower makes second to lower
- }
- }else if(ch2>='A' && ch2<='Z'){
- ch2+=32;//if first letter is lower and second is upper makes second to lower
- }
- return ch1>ch2?true:false;//compares already the same-case letters
- }
- //Targil 8.4
- public static void cord(int[][] arr, int num) {
- boolean isNotFound = true;//In case we'll not find our number
- for (int i = 0; i < arr.length; i += 1) {
- for (int j = 0; j < arr.length; j += 1) {
- if (arr[j][i] == num) { //we should to check elements by row and just after all row to change a column
- System.out.printf("The number: %n found at row: %d and column: %d\n", i, j);//if number found
- isNotFound = false;//means the number found
- } else if (arr[j][i] > num) {
- /*if anumber in current row is bigger than our number
- it means we'll not find it in current column
- so we need to change our column
- (to bring a pointer to the last element in current column) */
- j = arr.length;
- }
- }
- }
- //If number was't find prints massage
- if (isNotFound) {
- System.out.println("No matches");
- }
- }
- //Targil 8.5
- public static void matrixBubler(int[][] arr) {
- int ln = arr.length;
- int temp = 0;
- //for 16 numbers we have 16 loops for every number to push him forward
- for(int i=0; i<ln*ln-1; i+=1) {
- //buble sort for first number in a matrix
- for (int k = 0; k < ln; k += 1) {
- for (int j = 1; j < ln; j += 1) {
- //push number forward if it bigger than one forward to him
- if (arr[k][j - 1] > arr[k][j]) {
- temp = arr[k][j - 1];
- arr[k][j - 1] = arr[k][j];
- arr[k][j] = temp;
- }
- //when the last item in row compares him to first item in next row
- if ((j == ln - 1) && (k + 1 < ln) && (arr[k][j] > arr[k + 1][0])) {
- temp = arr[k + 1][0];
- arr[k + 1][0] = arr[k][j];
- arr[k][j] = temp;
- }
- }
- }
- }
- }
- //Targil 8.6
- public static void sortMatrixBySelection(int[][] arr) {
- int ln = arr.length;//for just one request of array proprety
- int minValue_row = 0, minValue_col = 0;//index holders for minimum value
- int temp;
- int x_pos;//save current x position
- //outer loops gives us values one by one from first to last element of array. arr[i][j] is current value
- for (int i = 0; i < ln; i += 1) {
- for (int j = 0; j < ln; j += 1) {
- x_pos = j;//to star from lsat position in row
- //min indexes start from the index of last element we took with outer loops
- minValue_row = i;
- minValue_col = j;
- //in inner loops we search for a minimum value start from the last element we got with outer loops
- for (int y = i; y < ln; y += 1) {
- for (int x = x_pos; x < ln; x += 1) {
- //if any element less than curren minimum value replace minimum indexes
- if (arr[y][x] < arr[minValue_row][minValue_col]) {
- minValue_row = y;
- minValue_col = x;
- }
- }
- x_pos = 0;//to start next line from first object in a row
- }
- //replace current value and min value
- temp = arr[minValue_row][minValue_col];
- arr[minValue_row][minValue_col] = arr[i][j];
- arr[i][j] = temp;
- }
- }
- }
- =========MAIN====================================================================
- //Targil 8.1
- int [] array = {70, 66, 79, 63, 68, 60, 67, 71};
- bubler(array);
- for(int i=0; i < array.length; i++){
- System.out.print(array[i] + " ");
- }
- //Targil 8.2
- int rows = 4, columns = 4;
- int [] [] array ={{9,3,6,5},
- {2,4,8,2},
- {7,2,2,1},
- {9,8,3,6}};
- diagonalBubler(array, rows, columns);
- for(int i = 0; i<columns; i+=1) {
- for (int j = 0; j < rows; j += 1) {
- System.out.print(array[i][j]);
- }
- System.out.println();
- }
- //Targil 8.3
- String str ="zdhyTbjCK";
- System.out.println(abSort(str));
- //Targil 8.4
- int [][] arr = {{2,1,5,1}, {3,6,6,2}, {8,7,7,3}, {9,8,9,4}}; //sorted array
- int num = 7;//number to find
- cord(arr, num);
- //Targil 8.5
- int [][] array = {{16, 15, 14, 13},
- {12, 11, 10, 9},
- {8, 7, 6, 5},
- {4, 3, 2, 1}};
- //sorting array
- matrixBubler(array);
- //print array after sorting
- for (int i = 0; i < array.length; i += 1) {
- for (int j = 0; j < array.length; j += 1) {
- System.out.print(array[i][j]+" ");
- }
- System.out.println();
- }
- //Targil 8.6
- int[][] someArray = {{16, 15, 14, 13},
- {12, 11, 10, 9},
- {8, 7, 6, 5},
- {4, 3, 2, 1}};
- sortMatrixBySelection(someArray);
- for (int i = 0; i < someArray.length; i += 1) {
- for (int j = 0; j < someArray.length; j += 1) {
- System.out.print(someArray[i][j] + " ");
- }
- System.out.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment