Created
April 22, 2017 22:04
-
-
Save ReneHollander/4225c215acc68d4cd44b150acda992b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package at.renehollander.euler; | |
import java.util.*; | |
public class Sort { | |
static int n = 10; | |
static List<Integer> list = new ArrayList<>(); | |
static final int RED = 0; | |
static final int GREEN = 1; | |
static final int BLUE = 2; | |
public static void main(String[] args) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < n; j++) { | |
list.add(i); | |
} | |
} | |
Collections.shuffle(list); | |
System.out.println(list); | |
Queue<Integer> redQueue = new ArrayDeque<>(); | |
Queue<Integer> greenQueue = new ArrayDeque<>(); | |
list.clear(); | |
list.addAll(Arrays.asList(RED, RED, RED, RED, GREEN, GREEN, GREEN, BLUE, BLUE, BLUE, BLUE, GREEN)); | |
n = 4; | |
for (int i = 0; i < n; i++) { | |
if (Examine(i) != RED) { | |
redQueue.add(i); | |
} | |
System.out.println(i + ": " + list); | |
} | |
for (int i = n * 1; i < n * 2; i++) { | |
if (Examine(i) == RED) { | |
Swap(i, redQueue.poll()); | |
} | |
if (Examine(i) != GREEN) { | |
greenQueue.add(i); | |
} | |
System.out.println(i + ": " + list); | |
} | |
for (int i = n * 2; i < n * 3; i++) { | |
if (Examine(i) == RED) { | |
Swap(i, redQueue.poll()); | |
} | |
if (Examine(i) == GREEN) { | |
Swap(i, greenQueue.poll()); | |
} | |
System.out.println(i + ": " + list); | |
} | |
} | |
public static int Examine(int i) { | |
return list.get(i); | |
} | |
public static void Swap(int i, int j) { | |
int iv = list.get(i); | |
int jv = list.get(j); | |
list.set(i, jv); | |
list.set(j, iv); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment