Skip to content

Instantly share code, notes, and snippets.

@tobiballof
Created September 13, 2017 07:58
Show Gist options
  • Save tobiballof/66a1e2d6f3e76edb0ae56edb3970aa70 to your computer and use it in GitHub Desktop.
Save tobiballof/66a1e2d6f3e76edb0ae56edb3970aa70 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_003_sortieren;
import java.util.ArrayList;
/**
*
* @author B201 //1,36
*/
class Bubblesort implements Sortierer {
public ArrayList<Integer> sortiere(ArrayList<Integer> xlist) {
ArrayList<Integer> list = ((ArrayList<Integer>) xlist.clone());
int sort;
boolean unsortiert = true;
while (unsortiert) {
unsortiert = false;
for (int i = 0; list.size() - 1 > i; i++) {
if (list.get(i) > list.get(i + 1)) {
sort = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, sort);
unsortiert = true;
}
}
};
return list;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_003_sortieren;
import java.util.ArrayList;
/**
*
* @author B201
*/
class Insertion implements Sortierer{
@Override
public ArrayList<Integer> sortiere(ArrayList<Integer> xlist) {
ArrayList<Integer> list = ((ArrayList<Integer>) xlist.clone());
int sort;
for(int i = 0; i<list.size(); i++){
for(int listlength = list.size() - 1; listlength > 0; listlength--){
sort = list.get(listlength);
list.set(listlength, list.get(listlength -1));
list.set(listlength -1, sort);
}
}
return list;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_003_sortieren;
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author B201
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> zahlenUnsort = new ArrayList<>();
ArrayList<Integer> zahlenSort = new ArrayList<>();
// Random rnd = new Random();
/* for (int i = 0; size > i; i++){
zahlenliste.add(rnd.nextInt());
}
*/
/* zahlenUnsort.add(5);
zahlenUnsort.add(7);
zahlenUnsort.add(10);
zahlenUnsort.add(591);
zahlenUnsort.add(90);
zahlenUnsort.add(51);
zahlenUnsort.add(55);
zahlenUnsort.add(1);*/
for (int i = 0; i <= 100; i++) {
Random rnd = new Random();
zahlenUnsort.add(rnd.nextInt(100));
}
System.out.println("Unsortierte Zahlen:");
for (int i : zahlenUnsort) {
System.out.println(i);
}
System.out.println("BubbleSort:");
Bubblesort bubble = new Bubblesort();
zahlenSort = bubble.sortiere(zahlenUnsort);
for (int i : zahlenSort) {
System.out.println(i);
}
zahlenSort = new ArrayList<>();
System.out.println("Insertsort");
Insertion insert = new Insertion();
zahlenSort = insert.sortiere(zahlenUnsort);
for (int i : zahlenSort) {
System.out.println(i);
}
zahlenSort = new ArrayList<>();
System.out.println("SelectionSort");
Selection select = new Selection();
zahlenSort = select.sortiere(zahlenUnsort);
for (int i : zahlenSort) {
System.out.println(i);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_003_sortieren;
import java.util.ArrayList;
/**
*
* @author B201
*/
class Selection implements Sortierer{
@Override
public ArrayList<Integer> sortiere(ArrayList<Integer> xlist) {
ArrayList<Integer> list = ((ArrayList<Integer>) xlist.clone());
int index, sort;
for(int i = list.size() - 1; i>=1; i--){
index = 0;
for(int j = 1; j<=i; j++){
if(list.get(j) > list.get(index)){
index = j;
}
}
sort = list.get(index);
list.set(index, list.get(i));
list.set(i, sort);
}
return list;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_003_sortieren;
import java.util.ArrayList;
/**
*
* @author B201
*/
public interface Sortierer {
public ArrayList<Integer> sortiere(ArrayList<Integer> list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment