java基础 数组及数组排序方法
来源:互联网 发布:矩阵论 清华 编辑:程序博客网 时间:2024/04/29 10:24
1. 数组:所谓数组就是一个用来存储固定数目的单一数据类型的容器对象,数组中所有元素都有相同的数组名;
一维数组的创建 如:
1> int[] arr = new int[10];
这是一个长度是10的int类型数组,该数组中没有元素,初始化数组的元素:
arr[0] = 1;
arr[2] = 2;
.......
2>int[] arr = {3,5,7,4,2}
这是一个长度为5的int类型数组,给数组中包含3,5,7,4,2元素
二维数组的创建 如:
int[][] arr = new int[3][2]; 或者
int[][] arr = {{1,2},{3,5},{7,8}};
2.数组的排序方法:
<span style="font-size:18px;"><strong>1>快速排序法:</strong></span>
<span style="font-size:18px;"><strong> public class Text1 { public int partition(int[] a, int i, int j) {//分割排序 int key = a[i]; while(i < j) { while(i < j && a[j] >= key)//找出第一个比key小,并记下j值 j--; a[i] = a[j]; //将a[j]移至a[i]处 while(i < j && a[i] <= key)//找出第一个比key大,并记下i值 i++; a[j] = a[i]; //将a[i]移至a[j]处 } a[i] = key;//此时完成一趟排序 return i;//此时i=j,记下i的值 } public void sort(int[] a, int i, int j) {//递归调用分割 if(i < j) { int n = partition(a,i,j);//排一次序列,并获取关键值的位置 sort(a,i,n-1);//左递归 sort(a,n+1,j);//右递归 } } public static void main(String[] args) { int[] a = {15,9,20,6,4,27,36}; Text2 t = new Text2(); t.sort(a, 0, 6); for(int k : a) {//增强for循环遍历数组 System.out.print(k + ","); } } } </strong></span>
<span style="font-size:18px;"><strong>2>选择排序法:</strong></span>
<span style="font-size:18px;"><strong> public class Text2 { /* * 选择排序 * */ public static void main(String[] args) { int[] a = {3,5,2,7,5,9,6,1}; printArray(a); }//定义方法 public static void printArray(int[] arr){ for(int i = 0; i < arr.length-1; i++){ for(int j = i; j < arr.length; j++){ if(arr[i] > arr[j]){ //交换位置 int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for(int i = 0; i < arr.length; i++){//数组遍历 System.out.print(arr[i] + " "); } } }</strong></span>
<span style="font-size:18px;"><strong> 3>冒泡排序法:</strong></span>
<span style="font-size:18px;"><strong> public class Text3 { //冒泡排序法 public static void main(String[] args) { //定义一个数组 int[] arr = {4, 1, 2, 6, 3, 8, 9, 10, 7, 5}; for (int i = 0; i < arr.length; i++){ //-i的目的,每一次比较的元素减少 //-1的目的,避免交表越界 for (int j = 0; j < arr.length - i - 1; j++){ if (arr[j] > arr[j + 1]){ //换位置操作 int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } //取出排序后数组中的值 for (int i = 0; i < 10; i++){ System.out.print(arr[i] + " "); } } }</strong></span>
<span style="font-size:18px;"><strong> 4>插入排序法</strong></span>
<span style="font-size:18px;"><strong> public static int[] insertSort(int[] args){//插入排序算法 for(int i=1;i<args.length;i++){ for(int j=i;j>0;j--){ if (args[j]<args[j-1]){ int temp=args[j-1]; args[j-1]=args[j]; args[j]=temp; }else break; } } return args; }</strong></span>
<span style="font-size:18px;"><strong> 数组的折半查找</strong></span>
<span style="font-size:18px;"><strong> class ArrayText { public static void main(String [] args) { int[] arr = {1,3,5,6,8,9,12}; int index = halfSearch(arr, 8); System.out.print("index:"+ index); } //折半的一种方式 public static int halfSearch(int[] arr, int key) { int min = 0, max = arr.length-1, mid; while(min <= max) { mid = (min+max)/2; if(key > arr[mid]) min = mid + 1; else if (key < arr[mid]) max = mid - 1; else return mid; } return -1; } }</strong></span>
<span style="font-size:18px;"><strong> 数组的最大值及最小值求发:以最大值为例</strong></span>
<span style="font-size:18px;"><strong> public class ArrayMax { public static void main(String[] args) { int[] arr = {4,2,7,9,23}; int max = getMax(arr); System.out.println(max); } //获取最大值方法 public static int getMax(int[] arr){ int max = arr[0]; for(int i = 0; i < arr.length; i++){ if(arr[i] > max){ max = arr[i]; } } return max; } }</strong></span>
自己学习的总结,希望多初学者有所帮助
- java基础 数组及数组排序方法
- java基础-数组及排序
- java基础数组排序
- Java数组排序方法
- Java基础---方法,数组
- JavaScript基础_06数组及数组方法
- 黑马程序员 Java基础<一> 数组及排序
- java基础之 数组排序
- java基础--数组(排序)
- java基础(数组 排序)
- java基础之数组排序
- java基础复习-数组排序
- java基础整理の数组的排序方法
- Java 数组排序及查找
- java对数组、List、Map排序及常用方法
- java数组排序方法笔记
- 【java】输入数组、数组排序及查找数组位置
- Java 数组、方法、基础笔记
- 流行趋势:27个引领时尚的网页设计作品【上篇】
- poj 2492 A Bug's Life
- escape加号被过滤解决方法
- springMVC详解
- 借鉴LifeAsia的table-driven 在前台POS系统 实现类似功能
- java基础 数组及数组排序方法
- css3.0 滚动的云 图形构成练习二
- You are my brother
- 最火的Android开源项目
- 流行浏览器内核分类及样式区别
- 几个ZigBee协议栈和操作系统
- gitosis/git服务器搭建
- hdu 1575
- Floyd算法