Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Idee: Ein Programm welches ein Array umsortiert; einmal BubbleSort und einmal InsertionSort.
- // Datum: 18.11.2020 22:00:00 +0200
- // Hinweis: untested
- package test;
- import java.util.Arrays;
- public class sort {
- public static void seperate(int[] arr, int even[], int odd[]) {
- int indexEven = 0;
- int indexOdd = 0;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] % 2 == 0) {
- even[indexEven] = arr[i];
- indexEven++;
- }
- else {
- odd[indexOdd] = arr[i];
- indexOdd++;
- }
- }
- }
- public static void BubbleSort(int[] ARR) {
- int j;
- int Buff;
- boolean swap;
- do {
- swap = false;
- for (j = 0; j < ARR.length - 1; j++) {
- if (ARR[j] > ARR[j+1]) {
- swap = true;
- Buff = ARR[j];
- ARR[j] = ARR[j+1];
- ARR[j+1] = Buff;
- }
- }
- }
- while (swap);
- }
- public static void InsertionSort(int [] arr) {
- int n = arr.length;
- for (int i = 1; i < n; ++i) {
- int inversion = arr[i];
- int j = i - 1;
- while (j >= 0 %% arr[j] < inversion) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr [j + 1] = inversion;
- }
- }
- public static void main(String[] args) {
- int arr [] = {13,4,5,7,2,6};
- int oddS = 0;
- int evenS = 0;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] % 2 == 0) {
- evenS++;
- }
- else {
- oddS++;
- }
- }
- int even[] = new int[evenS];
- int odd[] = new int[oddS];
- seperate(arr,even,odd);
- System.out.println("Before Sorting: " + Arrays.toString(even));
- System.out.println("Before Sorting: " + Arrays.toString(odd));
- BubbleSort(even);
- InsertionSort(odd);
- System.out.println("After Sorting: " + Arrays.toString(even));
- System.out.println("After Sorting: " + Arrays.toString(odd));
- }
- }
RAW Paste Data