public class Test {
/**
* 选择排序法的第一层循环从起始元素开始选到倒数第二个元素,主要是在每次进入的第二层循环之前,
* (第二层循环从第一层的起始元素+1开始至最后一个元素)
* 将外层循环的下标赋值给临时变量,接下来的第二层循环中,如果发现有比这个最小位置处的元素更小的元素,
* 则将那个更小的元素的下标赋给临时变量,最后,在二层循环退出后,如果临时变量改变,
* 则说明,有比当前外层循环位置更小的元素,需要将这两个元素交换.
*
*/
public static void main(String[] args) {
Test test = new Test();
int[] array = { 6, 5, 9, 3, 4, 0, 7, 1, 8,2 };
// 冒泡
test.bubbleSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
System.out.print(",");
}
// 122345这六个数字,用java写一个main函数,打印出六位数不同的排列,
// 如:512234,122345等,要求"4"不能在第三位,其不能相连出现35 53不能相连
System.out.println("==pop sort end==");
for (int i = 122345; i <= 543221; i++) {
if (!filter(i)) {// 关键在这里
System.out.println(i);
}
}
}
// bubble 交换排序
public void bubbleSort(int[] array) {
int temp = 0;
for (int i = 0; i < array.length-1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[i]) {
// 从小到大
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
public static boolean filter(int n) {
String s = "" + n;
if (s.charAt(2) == '4') {
return true;
}
if (s.indexOf("35") != -1 || s.indexOf("53") != -1) {// 以下算法真是妙不可言
return true;
}
int i = s.indexOf("2");
if (i < s.length() - 1 && s.charAt(i + 1) == '2') {
return true;
}
return false;
}
/**
* 选择排序法的第一层循环从起始元素开始选到倒数第二个元素,主要是在每次进入的第二层循环之前,
* (第二层循环从第一层的起始元素+1开始至最后一个元素)
* 将外层循环的下标赋值给临时变量,接下来的第二层循环中,如果发现有比这个最小位置处的元素更小的元素,
* 则将那个更小的元素的下标赋给临时变量,最后,在二层循环退出后,如果临时变量改变,
* 则说明,有比当前外层循环位置更小的元素,需要将这两个元素交换.
*
*/
public static void main(String[] args) {
Test test = new Test();
int[] array = { 6, 5, 9, 3, 4, 0, 7, 1, 8,2 };
// 冒泡
test.bubbleSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
System.out.print(",");
}
// 122345这六个数字,用java写一个main函数,打印出六位数不同的排列,
// 如:512234,122345等,要求"4"不能在第三位,其不能相连出现35 53不能相连
System.out.println("==pop sort end==");
for (int i = 122345; i <= 543221; i++) {
if (!filter(i)) {// 关键在这里
System.out.println(i);
}
}
}
// bubble 交换排序
public void bubbleSort(int[] array) {
int temp = 0;
for (int i = 0; i < array.length-1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[i]) {
// 从小到大
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
public static boolean filter(int n) {
String s = "" + n;
if (s.charAt(2) == '4') {
return true;
}
if (s.indexOf("35") != -1 || s.indexOf("53") != -1) {// 以下算法真是妙不可言
return true;
}
int i = s.indexOf("2");
if (i < s.length() - 1 && s.charAt(i + 1) == '2') {
return true;
}
return false;
}