好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java数组高级算法与Arrays类常见操作小结【排序、查找】

本文实例讲述了java数组高级算法与arrays类常见操作。分享给大家供大家参考,具体如下:

冒泡排序

冒泡排序原理

冒泡排序代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

package cn.itcast_01;

/*

  * 数组排序之冒泡排序:

  *     相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处

  */

public class arraydemo {

   public static void main(string[] args) {

     // 定义一个数组

     int[] arr = { 24, 69, 80, 57, 13 };

     system.out.println("排序前:");

     printarray(arr);

     bubblesort(arr);

     system.out.println("排序后:");

     printarray(arr);

   }

   //冒泡排序代码

   /*总共需要比较数组长度-1次,x < arr.length - 1

    *每一次比较完,下一次就会减少一次元素的比较。第一次比较有0个元素不比,第二次有1个元素不比,,,,所以 y < arr.length - 1 - x

    *两两比较,大的往后放

    * */

   public static void bubblesort( int [] arr){

     for ( int x = 0 ; x < arr.length - 1 ; x++) {

       for ( int y = 0 ; y < arr.length - 1 - x; y++) {

         if (arr[y] > arr[y + 1 ]) {

           int temp = arr[y];

           arr[y] = arr[y + 1 ];

           arr[y + 1 ] = temp;

         }

       }

     }

   }

   // 遍历功能

   public static void printarray( int [] arr) {

     system.out.print( "[" );

     for ( int x = 0 ; x < arr.length; x++) {

       if (x == arr.length - 1 ) {

         system.out.print(arr[x]);

       } else {

         system.out.print(arr[x] + ", " );

       }

     }

     system.out.println( "]" );

   }

}

选择排序

选择排序原理图

选择排序代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

package cn.itcast_02;

/*

  * 数组排序之选择排序:

  *     从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处

  */

public class arraydemo {

   public static void main(string[] args) {

     // 定义一个数组

     int[] arr = { 24, 69, 80, 57, 13 };

     system.out.println("排序前:");

     printarray(arr);

     //用方法改进

     selectsort(arr);

     system.out.println("排序后:");

     printarray(arr);

   }

   /*

    * 数组排序

    * */

   public static void selectsort( int [] arr){

     for ( int x= 0 ; x<arr.length- 1 ; x++){

       for ( int y=x+ 1 ; y<arr.length; y++){

         if (arr[y] <arr[x]){

           int temp = arr[x];

           arr[x] = arr[y];

            arr[y] = temp;

         }

       }

     }

   }

   // 遍历功能

   public static void printarray( int [] arr) {

     system.out.print( "[" );

     for ( int x = 0 ; x < arr.length; x++) {

       if (x == arr.length - 1 ) {

         system.out.print(arr[x]);

       } else {

         system.out.print(arr[x] + ", " );

       }

     }

     system.out.println( "]" );

   }

}

二分查找法

二分查找法原理

二分法的代码实现:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

package cn.itcast_04;

/*

  * 查找:

  *     基本查找:数组元素无序(从头找到尾)

  *     二分查找(折半查找):数组元素有序

  *

  * 分析:

  *     a:定义最大索引,最小索引

  *     b:计算出中间索引

  *     c:拿中间索引的值和要查找的值进行比较

  *       相等:就返回当前的中间索引

  *       不相等:

  *         大  左边找

  *         小  右边找

  *     d:重新计算出中间索引

  *       大  左边找

  *         max = mid - 1;

  *       小  右边找

  *         min = mid + 1;

  *     e:回到b

  */

public class arraydemo {

   public static void main(string[] args) {

     //定义一个数组

     int[] arr = {11,22,33,44,55,66,77};

     //写功能实现

     int index = getindex(arr, 33);

     system.out.println("index:"+index);

     //假如这个元素不存在后有什么现象呢?

     index = getindex(arr, 333);

     system.out.println("index:"+index);

   }

   /*

    * 两个明确:

    * 返回值类型:int

    * 参数列表:int[] arr,int value

    */

   public static int getindex( int [] arr, int value){

     //定义最大索引,最小索引

     int max = arr.length - 1 ;

     int min = 0 ;

     //计算出中间索引

     int mid = (max +min)/ 2 ;

     //拿中间索引的值和要查找的值进行比较

     while (arr[mid] != value){

       if (arr[mid]>value){

         max = mid - 1 ;

       } else if (arr[mid]<value){

         min = mid + 1 ;

       }

       //加入判断

       if (min > max){

         return - 1 ;

       }

       mid = (max +min)/ 2 ;

     }

     return mid;

   }

}

arrays类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package cn.itcast_05;

import java.util.arrays;

/*

  * arrays:针对数组进行操作的工具类。比如说排序和查找。

  * 1:public static string tostring(int[] a) 把数组转成字符串

  * 2:public static void sort(int[] a) 对数组进行排序

  * 3:public static int binarysearch(int[] a,int key) 二分查找

  */

public class arraysdemo {

   public static void main(string[] args) {

     // 定义一个数组

     int [] arr = { 24 , 69 , 80 , 57 , 13 };

     // public static string tostring(int[] a) 把数组转成字符串

     system.out.println( "排序前:" + arrays.tostring(arr));

     // public static void sort(int[] a) 对数组进行排序

     arrays.sort(arr);

     system.out.println( "排序后:" + arrays.tostring(arr));

     // [13, 24, 57, 69, 80]

     // public static int binarysearch(int[] a,int key) 二分查找

     system.out.println( "binarysearch:" + arrays.binarysearch(arr, 57 ));

     system.out.println( "binarysearch:" + arrays.binarysearch(arr, 577 ));

   }

}

希望本文所述对大家java程序设计有所帮助。

原文链接:https://HdhCmsTestcnblogs测试数据/baiyangyuanzi/p/6860453.html

查看更多关于Java数组高级算法与Arrays类常见操作小结【排序、查找】的详细内容...

  阅读:10次