Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class obj {
- public static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- System.out.println("Введите кол-во элементов:");
- int n = sc.nextInt();
- int[] arr = new int[n];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = (int) (Math.random() * 10);
- }
- System.out.println("Элементи массива:");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- System.out.println("\n1. Метод простого обмена.\n2. Метод Шелла.");
- int x = sc.nextInt();
- switch (x){
- case 1: bubbleSort(arr);break;
- case 2: sortShell(arr,n);break;
- }
- }
- private static void bubbleSort(int[] arr) {
- for (int i = arr.length - 1; i > 0; i--) {
- for (int j = 0; j < i; j++) {
- if (arr[j] > arr[j + 1]) {
- int tmp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = tmp;
- }
- }
- }
- System.out.println("Введите число B:");
- int B = sc.nextInt();
- int qty = 0; //количество элементов, больших, чем B
- for (int i:arr) {
- if(i>B){
- qty++;
- }
- }
- System.out.println("Всего элементов - " + arr.length);
- System.out.println("Элементов, больших чем b - " + qty);
- }
- private static void sortShell(int[] arr, int n) {
- int step = n/2;
- while (step>0)
- {
- for (int i = 0; i < (n - step); i++)
- {
- int j = i;
- while (j >= 0 && arr[j] > arr[j + step])
- {
- int temp = arr[j];
- arr[j] = arr[j + step];
- arr[j + step] = temp;
- j--;
- }
- }
- step = step / 2;
- }
- for (int i = 0; i < n; i++) {
- System.out.print(arr[i] + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement