您的位置:首页 > 编程语言 > Java开发

JavaSE第三十五讲:冒泡排序、交换排序及快速排序原理与实现

2012-11-29 15:02 603 查看
1.写个程序将a数组的前三个元素内容拷贝的b数组中:

public class ArrayCopy{
public static void main(String[] args){
int[] a = new int[]{1, 2, 3, 4};
int[] b = new int[4];
}
}
【说明】:如上程序所示一般实现的方法是比较传统的方法一(如下所示)

【方法一】:用循环将a中的数组内容取出然后放在b数组中(这里不再实现)
【方法二】:查看JDK文档API,用Java为我们提供System类(主要讲这个程序)

java.lang

Class System

  java.lang.Object 

    java.lang.System  

public final class System

extends Object

The System class contains several useful class fields and methods. It cannot be instantiated. 

Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment
variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

Since: 

JDK1.0

查看



图35-1
参数说明:
src - the source array.   源数组,待拷贝数组

srcPos - starting position in the source array.  源数组当中的起始位置,从源数组中的哪个位置开始拷贝。

dest - the destination array.  目标数组,元素拷贝到哪个数组中去。

destPos - starting position in the destination data.  拷贝到目标数组中从目标数组

length - the number of array elements to be copied.   待拷贝的数组的个数,也就是需要拷多少长度的数组。

这是一个static方法,直接用就可以,位于java.lang包下不需要导入

程序方法如下:

public class ArrayCopy{
public static void main(String[] args){
int[] a = new int[]{1, 2, 3, 4};
int[] b = new int[4];

System.arraycopy(a, 0, b, 0, 3);
for(int i = 0; i < b.length; i++){
System.out.println(b[i] + " ");
}
}
}
编译执行结果:

D:\src>java ArrayCopy

1

2

3

0

2.三维数组

  三维数组实际开发中用的很少,一般用再游戏开发中,游戏开发需要很多数学知识,矩阵,傅立叶等。

  三维数组定义格式:type[][][] a = new type[2][3][4]; 

  三维数组可以理解为:数组的数组的数组。

  三维数组也可以是不规则的。

public class ThreeDimensionArrayTest{
public static void main(String[] args) {
int[][][] a = new int[2][3][4];

System.out.println(a instanceof Object);
System.out.println(a instanceof int[][][]);
System.out.println(a[0] instanceof int[][]);
System.out.println(a[0][0] instanceof int[]);

for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
for(int k = 0; k < a[i][j].length; k++){
a[i][j][k] = 100;
}
}
}

}
}
编译执行结果:

D:\src>java ThreeDimensionArrayTest

true

true

true

true

【说明】:System.out.println(a instanceof Object);为真,说明数组a也是一个对象。

System.out.println(a instanceof int[][][]);为真,说明数组a是一个三维数组,类型是int[][][]。
System.out.println(a[0] instanceof int[][]);为真,说明数组a[0]是一个二维数组,类型是int[][]。
System.out.println(a[0][0] instanceof int[]);为真,说明数组a[0][0]是一个一维数组,类型是int[]。


3.数组跟数据结构是紧密相关的。

  数组中常用的操作有:排序、查找、搜索

先来讲一下排序中的冒泡排序原理:

实现数组的冒泡排序升序,数组中有元素为:5  4  7  9  3

第一趟:  5  4  7  9  3 ---> 4  5  7  9  3 ---> 4  5  7  9  3 ---> 4  5  7  9
 3 ---> 此时第一轮比较完结果为:4  5  7  3  9

第二趟:  4  5  7  3  9 ---> 4  5  7  3  9 ---> 4  5  7  3  9 ---> 4  5  3  7
 9
 ---> 此时第二轮比较完结果为:4  5  3  7  9

第三趟:  4  5  3  7  9 ---> 4  5  3  7  9 ---> 4  3  5  7  9 ---> 4  3  5  7
 9
---> 此时第三轮比较完结果为:4  3  5  7  9

第四趟:  4  3  5  7  9 ---> 3  4  5  7  9 ---> 3  4  5  7  9 ---> 3  4  5  7
 9
 ---> 自此所有结果比较完毕为:3  4  5  7  9

下面例子为我自己写的冒泡排序:

public class BubbleSort{
public static void bubbleSort(int[] a){
for(int j = 0; j < a.length-1; j++){
for(int k = 0; k < a.length-1; k++){
int temp;
if(a[k] > a[k + 1]){
temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = new int[]{5, 4, 7, 9, 3};
BubbleSort.bubbleSort(a);
for(int i =0; i < a.length; i++){
System.out.println(a[i]);
}
}
}
编译执行结果:

D:\src>java BubbleSort

3

4

5

7

9

【说明】:这个程序是根据冒泡排序的原理,用两个for循环,外层循环用来控制对数组从前两个比较到后两个比较要跑几趟(这个是在不知道怎么表达),内层循环是用来控制整个数组需要比较几次,总结上面的排序的过程可以知道内层和外层的控制最大都是数组长度length-1。

以下是张龙老师写的冒泡排序:

public class BubbleSortTest
{
public static void bubbleSort(int[] array)
{
for(int i = 0; i < array.length - 1; i++)
{
for(int j = 0; j < array.length - i - 1; j++)
{
if(array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}

}
System.out.println("The" + (i + 1) + "Time Sort");
for(int k = 0; k < array.length; k++){
System.out.print(array[k] + " ");
}
System.out.println();
}
}

public static void main(String[] args)
{
int[] array = {5, 4, 7, 9, 3};

bubbleSort(array);

}
}
编译执行结果:

D:\src>java BubbleSortTest

The1Time Sort

4 5 7 3 9

The2Time Sort

4 5 3 7 9

The3Time Sort

4 3 5 7 9

The4Time Sort

3 4 5 7 9

【说明】:程序段System.out.println("The" + (i + 1) + "Time Sort");
for(int k = 0; k < array.length; k++){
System.out.print(array[k] + " ");
}
System.out.println();

表示的是将每轮排序后的结果打印出来,也就是排在最后的元素是最大的元素。

System.out.println();表示另外再起一行,相当于回车。

比较程序我自己写的程序与张龙老师的程序可以方向再内层for循环中最大长度不同:

对于:for(int j = 0; j < array.length - i - 1; j++){} 再数组末端,末端前面的已经比较出来大小了,后面的几趟就不需要再比较了。

对于:for(int k = 0; k < a.length-1; k++){} 是全部进行进行比较。

所以选择前者是比较好的一种方式。已经比较出来大小的后面就可以不用再比较了,而如何知道哪些已经比较出来大小了,是根据规律,第三轮比较后最后面的两个数一定是最大和第二大了,后面就不需要对这两个数进行比较了?所以是array.length
- i - 1。




这样的程序最好多写多练才能真正体会数据的走向。最后交换排序,快速排序原理与实现待下回更新。

作业:

1.冒泡排序。(掌握交换排序,快速排序的原理与实现方式) 

2.二分查找(Binary Search):待查找的数组要有序。 

3.随机生成 50 个数字(整数),每个数字的范围是[10, 50],统计每个数字出现的次数以及 出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来,如果某个数 字出现次数为 0,则不要打印它。打印时按照数字的升序排列。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐