数组排序实例
来源:互联网 发布:js鼠标经过事件 编辑:程序博客网 时间:2024/05/17 05:15
请打印出一组数组数据并按从小到大的顺序排列。
public class NumSort {
public static void main(String[]args) {int[] a = new int[args.length];
for (int i = 0;i < args.length;i++) {
a[i] = Integer.parseInt(args[i]);
}
print(a);
selectionSort(a);
print(a);
}
private static void selectionSort(int[] a) {
for(int i = 0;i < a.length;i++) {
for(int j = i + 1;j < a.length;j++) {
if(a[j] < a[i]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
private static void print(int[] a) {
for (int i = 0;i < a.length;i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
例:500个孩牵手围城一个圈,变数3退一,求最好一个人是在哪个位置。
public class Count3Quit {
public static void main(String [] args) {
boolean[] arr = new boolean[500];
for(int i = 0;i < arr.length;i++) {
arr[i] = true;
}
int leftCount = arr.length;
int countNum = 0;
int index = 0;
while(leftCount > 1) {
if(arr[index] == true) {
countNum++;
if(countNum == 3) {
countNum = 0;
arr[index] = false;
leftCount --;
}
}
index ++;
if(index == arr.length) {
index = 0;
}
}
for(int i=0;i < arr.length;i++) {
if(arr[i] == true) {
System.out.println(i);
}
}
}
}
- 数组排序实例
- Javascript数组排序实例
- 数组排序实例
- 【Java】数组实例--自动排序
- Collections类对数组 排序实例
- java数组排序实例-起泡法
- 数据结构实例<一>(数组排序)入门
- 最好理解的快速排序 :实例解析输入数组,排序
- JAVA基础程序设计——数组排序、算法(数组实例+自己写的算法实例)
- php对二维数组进行排序的简单实例
- php对二维数组进行排序的简单实例
- Foundation框架--数组中内容的排序种类 实例
- Java实例—数组的排序和搜索
- 指针数组和二级指针的排序应用实例
- php排序 asort usort实例 对数组的多个字段排序 稳定排序
- 黑马程序员_Java基础_04数组main函数args详解、数组排序、二分查找实例
- 小实例-实现对成绩总分由高到低的排序-Java中数组对象的排序
- 排序实例
- Arcgis Engine二次开发,如何获取符号库文件中的符号
- MyEclipse 注册码
- DWZ主从结构计算
- 单例模式的5种写法
- Servlet和JSP的线程安全问题
- 数组排序实例
- No accelerated colorspace conversion found from yuv420p to rgba.的问题
- 需求调研步骤和方法
- 【热门主题】蓝色妖姬电脑桌面主题
- 关于ghostscript not found 解决方案
- HIVE元数据库改为mysql
- 数据库 ORDER BY 整理
- 关于闭包
- android 集成第三方静态库的编译方法