簡単な選択、泡の並べ替え、2分の検索法といくつかのfor循環の柔軟な運用


以下の通りです

import java.util.Arrays;
//    
public class Test {

  public static void main(String[] args) {
    int[] array = { 31, 22, 15, 77, 52, 32, 18, 25, 16, 7 };
    //    -->      -->               
     //                  
    System.out.println(Arrays.toString(array));
    for (int j = 0; j < array.length; j++) {
      for (int i = 0; i < array.length - 1 - j; i++) {
        //                  
        if (array[i] < array[i + 1]) {
          int temp = array[i];
          array[i] = array[i + 1];
          array[i + 1] = temp;
        }
      }
    }
    System.out.println(Arrays.toString(array));
  }
}

import java.util.Arrays;
//    
public class Test {

  public static void main(String[] args) {
    int[] array = { 31, 22, 15, 77, 52, 32, 18, 25, 16, 7 };
    //    -->                 
    System.out.println(Arrays.toString(array));
    for (int i = 0; i < array.length; i++) {
      for (int j = i + 1; j < array.length; j++) {
        if (array[i] < array[j]) {
          //    array[0] array[1] ,    
          //      temp
          int temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
      }
    }
    System.out.println(Arrays.toString(array));
  }
}

import java.util.Arrays;
import java.util.Scanner;

//                  ,   。    
public class Test11 {

  public static void main(String[] args) {
    //           
    int[] array = { 3, 10, 15, 22, 33, 51, 77, 88 };
    System.out.println(Arrays.toString(array));
    Scanner input = new Scanner(System.in);
    do {

      System.out.print("         :");
      int number = input.nextInt();
       //                 
      int left = 0;
      int right = array.length - 1;
      int result = recursion(array, left, right, number);
      System.out.println(result);
      int[] newArray = new int[array.length + 1];
      for (int i = 0; i < newArray.length; i++) {
        if (i < result) {
          newArray[i] = array[i];
        } else if (i == result) {
          newArray[i] = number;
        } else {
          newArray[i] = array[i - 1];
        }
      }
      array = newArray;
      System.out.println(Arrays.toString(array));
    } while (true);
  }

    //  
  public static int recursion(int[] array, int left, int right, int number) {
    //       ,      
    if (array[0] > number)
      return 0;
    else if (array[array.length - 1] < number)
      return array.length;
      
    int center = (left + right) / 2;
    if (left == right - 1)
      return right;
    if (array[center] > number) {
      return recursion(array, left, center, number);
    } else {
      return recursion(array, center, right, number);
    }
  }

}

//for      
public class Rect {

  public void show(int width, int height) {

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        if (i == 0 || j == 0 || i == width - 1 || j == height - 1)
          System.out.print("*");
        else
          System.out.print(" ");
      }
      System.out.println();
    }
  }
}

//for      
public class Rhombus {

  public void show(int height) {
    int half = height / 2;
    for (int i = 1; i <= half; i++) {
      //     
      for (int j = half - i + 1; j > 0; j--) {
        System.out.print(" ");
      }
      for (int j = 0; j < 2 * i - 1; j++) {
        System.out.print("*");
      }
      System.out.println();
    }

    for (int i = 0; i < 2 * half + 1; i++) {
      System.out.print("*");
    }
    System.out.println();

    for (int i = 1; i <= half; i++) {
      //     
      for (int j = i; j > 0; j--) {
        System.out.print(" ");
      }
      for (int j = 0; j < 2 * (half - i) + 1; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }

}

//for         
public class Isosceles {

  public void show(int height) {
    for (int i = 1; i <= height; i++) {
      //     
      for (int j = height - i; j > 0; j--) {
        System.out.print(" ");
      }
      for (int j = 0; j < 2 * i - 1; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}

//for         
public class Triangle {

  public void show(String str, int height) {
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < i + 1; j++) {
        System.out.print(str);
      }
      System.out.println();
    }
  }

}
以上のこの簡単な選択、泡が並べられています。二分の検索法といくつかのfor循環の柔軟な運用は小編が皆さんに共有している内容です。参考にしてほしいです。皆さんもよろしくお願いします。