Skip to content

Instantly share code, notes, and snippets.

@Fundibalus
Created September 5, 2017 19:38
Show Gist options
  • Save Fundibalus/b2ea541a5b9b74271383e072ec087aeb to your computer and use it in GitHub Desktop.
Save Fundibalus/b2ea541a5b9b74271383e072ec087aeb 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 array;
import java.util.ArrayList;
/**
*
* @author Marlon
*/
public class Array {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> liste = new ArrayList<>();
liste.add(1);
liste.add(10);
liste.add(4);
liste.add(12);
liste.add(2);
liste.add(3);
liste.add(17);
liste.add(5);
Sortierung s = new Sortierung();
s.BubbleSort(liste);
System.out.println(liste);
}
}
/*
* 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 array;
import java.util.ArrayList;
/**
*
* @author Marlon
*/
public class Sortierung {
public ArrayList<Integer> BubbleSort(ArrayList<Integer> liste) {
boolean swapped = true;
while (swapped == true) {
swapped = false;
for (int i = 1; i < liste.size(); i++) {
if (liste.get(i) < liste.get(i - 1)) {
int zahl1 = liste.get(i);
int zahl2 = liste.get(i - 1);
liste.set(i - 1, zahl1);
liste.set(i, zahl2);
swapped = true;
}
}
}
return liste;
}
public static ArrayList<Integer> InsertionSort(ArrayList<Integer> liste){
int wert;
for (int i = 0; i<liste.size(); i++){
for (int j = liste.size()-1; j> 0; j--){
if (liste.get(j+1)<liste.get(j)){
wert = liste.get(j);
liste.set(j, liste.get(j-1));
liste.set(j-1, wert);
}
}
}
return liste;
}
public static ArrayList<Integer> SelectionSort(ArrayList<Integer> liste){
int wert1;
int wert2;
for (int i = liste.size()-1; i>=1; i--) {
wert1 = 0;
for (int o = 1; o<=i; o++) {
if(liste.get(o) > liste.get(wert1)) {
wert1=o;
}
}
wert2 = liste.get(wert1);
liste.set(wert1, liste.get(i));
liste.set(i, liste.get(wert1));
}
return liste;
}
}
/*
* 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 array;
import java.util.ArrayList;
/**
*
* @author Marlon
*/
public class Array {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> liste = new ArrayList<>();
liste.add(1);
liste.add(10);
liste.add(4);
liste.add(12);
liste.add(2);
liste.add(3);
liste.add(17);
liste.add(5);
Sortierung s = new Sortierung();
s.BubbleSort(liste);
System.out.println(liste);
}
}
/*
* 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 array;
import java.util.ArrayList;
/**
*
* @author Marlon
*/
public class Sortierung {
public ArrayList<Integer> BubbleSort(ArrayList<Integer> liste) {
boolean swapped = true;
while (swapped == true) {
swapped = false;
for (int i = 1; i < liste.size(); i++) {
if (liste.get(i) < liste.get(i - 1)) {
int zahl1 = liste.get(i);
int zahl2 = liste.get(i - 1);
liste.set(i - 1, zahl1);
liste.set(i, zahl2);
swapped = true;
}
}
}
return liste;
}
public static ArrayList<Integer> InsertionSort(ArrayList<Integer> liste){
int wert;
for (int i = 0; i<liste.size(); i++){
for (int j = liste.size()-1; j> 0; j--){
if (liste.get(j+1)<liste.get(j)){
wert = liste.get(j);
liste.set(j, liste.get(j-1));
liste.set(j-1, wert);
}
}
}
return liste;
}
public static ArrayList<Integer> SelectionSort(ArrayList<Integer> liste){
int wert1;
int wert2;
for (int i = liste.size()-1; i>=1; i--) {
wert1 = 0;
for (int o = 1; o<=i; o++) {
if(liste.get(o) > liste.get(wert1)) {
wert1=o;
}
}
wert2 = liste.get(wert1);
liste.set(wert1, liste.get(i));
liste.set(i, liste.get(wert1));
}
return liste;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment