lundi 23 mars 2020

sorting using same random array

I want to make an array sorting in three ways using one random array. i have:

import sort.bub;
import sort.ins;
import sort.sel;

import java.util.Arrays;
import java.util.Random;

public class sorting{

    public static int[] arr = new int[10];
    public static bub bubble = new bub();
    public static sel selection = new sel();
    public static ins insertion = new ins();


    public static void main(String[] args) {

        getMathRandom();
        bubble.bub(arr);
        getMathRandom();
        insertion.ins(arr);
        getMathRandom();
        selection.sel(arr);
    }


    public static void getRandom(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt();
            //System.out.println(arr[i]);
        }
        System.out.println(Arrays.toString(arr));

    }

    private static void getMathRandom() {

        for (int j = 0; j < arr.length; j++) {
            int number = (int) (Math.random() * 1000);
            arr[j] = number;
        }
        System.out.println("Random  " + Arrays.toString(arr));
        System.out.println("");
    }   
}

bub.java

import java.util.Arrays;
public class bub {
    public bub() {
    }
   public void bub(int[] arr) {
        System.out.println("bubble sort");
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.println(Arrays.toString(arr));
            for (int index = 0; index < i; index++) {
                if (arr[index] > arr[index + 1]) {
                    int tmp = arr[index];
                    arr[index] = arr[index + 1];
                    arr[index + 1] = tmp;
                }
            }
           // System.out.println(Arrays.toString(arr));
        }
        System.out.println("");
    }
}

sel.java

import java.util.Arrays;
public class sel {
    public sel() {
    }
    public void sel(int[] arr) {
        System.out.println("selection sort: ");
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.println(Arrays.toString(arr));

            for (int index = i + 1; index < arr.length; index++) {
                if (arr[i] > arr[index]) {
                    int tmp = arr[index];
                    arr[index] = arr[i];
                    arr[i] = tmp;
                }
            }
//            System.out.println(Arrays.toString(arr));
        }
        System.out.println("");
    }
}

ins.java

import java.util.Arrays;
public class ins {
    public ins() {
    }
    public void ins(int[] arr) {
        System.out.println("insertion sort: ");
        for (int i = 1; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr));

            int newElement = arr[i];
            int location = i - 1;
            while (location >= 0 && arr[location] > newElement) {
                arr[location + 1] = arr[location];
                location--;
            }
            arr[location + 1] = newElement;
        }

        System.out.println("");
    }
}

output

Random  [928, 469, 309, 30, 344, 230, 924, 628, 319, 275]

bubble sort
[928, 469, 309, 30, 344, 230, 924, 628, 319, 275]
[469, 309, 30, 344, 230, 924, 628, 319, 275, 928]
[309, 30, 344, 230, 469, 628, 319, 275, 924, 928]
[30, 309, 230, 344, 469, 319, 275, 628, 924, 928]
[30, 230, 309, 344, 319, 275, 469, 628, 924, 928]
[30, 230, 309, 319, 275, 344, 469, 628, 924, 928]
[30, 230, 309, 275, 319, 344, 469, 628, 924, 928]
[30, 230, 275, 309, 319, 344, 469, 628, 924, 928]
[30, 230, 275, 309, 319, 344, 469, 628, 924, 928]
[30, 230, 275, 309, 319, 344, 469, 628, 924, 928]

Random  [909, 584, 169, 129, 175, 297, 733, 473, 619, 745]

insertion sort: 
[909, 584, 169, 129, 175, 297, 733, 473, 619, 745]
[584, 909, 169, 129, 175, 297, 733, 473, 619, 745]
[169, 584, 909, 129, 175, 297, 733, 473, 619, 745]
[129, 169, 584, 909, 175, 297, 733, 473, 619, 745]
[129, 169, 175, 584, 909, 297, 733, 473, 619, 745]
[129, 169, 175, 297, 584, 909, 733, 473, 619, 745]
[129, 169, 175, 297, 584, 733, 909, 473, 619, 745]
[129, 169, 175, 297, 473, 584, 733, 909, 619, 745]
[129, 169, 175, 297, 473, 584, 619, 733, 909, 745]

Random  [404, 277, 798, 863, 163, 163, 984, 638, 248, 561]

selection sort: 
[404, 277, 798, 863, 163, 163, 984, 638, 248, 561]
[163, 404, 798, 863, 277, 163, 984, 638, 248, 561]
[163, 163, 798, 863, 404, 277, 984, 638, 248, 561]
[163, 163, 248, 863, 798, 404, 984, 638, 277, 561]
[163, 163, 248, 277, 863, 798, 984, 638, 404, 561]
[163, 163, 248, 277, 404, 863, 984, 798, 638, 561]
[163, 163, 248, 277, 404, 561, 984, 863, 798, 638]
[163, 163, 248, 277, 404, 561, 638, 984, 863, 798]
[163, 163, 248, 277, 404, 561, 638, 798, 984, 863]

i want the array just one random array, but with three different sorting. I tried to use only one getmathrandom but only one sort that runs the other only shows the results of the top sorting. how would I do that? Or any other solution I'd appreciate.




Aucun commentaire:

Enregistrer un commentaire