Java中數組的幾個常用算法:插入算法,刪除算法,冒泡排序算法


前言:

在Java中我們常常會用數組,提到數組就不得不介紹數組中常用到的幾個算法。

有插入算法,刪除算法,冒泡排序算法等。

在學習這幾個數組的算法前,我們先來了解一下關於數組一些基本知識。

數組的基本知識:

數組的定義:數組(Array)是相同數據類型的數據的有序集合

數組是引用數據類型。

數組的三個特點:

[1]數組長度確定。數組一旦申請完空間長度不能發生變化,用length屬性訪問

[2]數組元素都是同一數據類型。

[3]數組是有序的 。每個元素通過下標/索引標記,索引0開始

關於內存的空間的一些知識:

內存分為兩類:

(stack)內存:基本數據類型分配在棧內存,棧內存空間不需要開發者回收,系統會自動回收。空間占整個內存空間的比例較小。

(heap)內存:引用數據類型分配在堆內存,堆內存一定要開發者通過new 申請開發者申請的內存使用完成后一定要回收。jvm中有專門的垃圾回收機制(gc)回收使用完的堆內存。空間占整個內存空間的比例較大。

數組的幾種聲明方法:

例如

(1)int[] arr = new int[5];

   arr[0] = 10;
        arr[1] = 20;
        arr[2] = 30;
        arr[3] = 40;
        arr[4] = 50;

 ( 2 ) int[] arr2;
        arr2 = new int[]{10,20,30,30,50};

(3)int[] arr2 = {10,20,30,40,50};

數組的算法:

(1)插入算法:一個數組有序,添加一個元素后,數組依然有序。

代碼如下:

 1 import java.util.Scanner;
 2 
 3 public class Test05{
 4     public static void main(String[] args){
 5         //一個數組有序,添加一個元素后,數組依然有序。
 6         int[] arr = {1,3,7,9,10,13,0};
 7         
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("請添加一個數:");
10         int t = input.nextInt();
11         //int t = 8;
12         //(1)找出位置
13         int loc = -1;
14         for(int i = 0;i < arr.length-1;i++){
15             if(arr[i] > t){
16                 loc = i;
17                 break;
18             }
19         }
20         System.out.println("loc=" + loc );
21         //(2)調t的位置
22         if(loc < 0){           //t在最后一位
23             arr[arr.length-1] = t;
24         }else{                         //將loc后面幾位數往后移動
25             for(int j = arr.length-1;j>loc;j--){
26                 arr[j]=arr[j-1];
27             }
28             //將t放進去
29             arr[loc] = t;
30         }
31         //檢驗,遍歷數組
32         for(int i = 0;i < arr.length;i++){
33             System.out.print(arr[i]+ "\t");
34         }          
35     }
36 }

 

(2)刪除算法:一個有序的數組,刪除一個元素后依然有序。

代碼如下:

 1 import java.util.Scanner;
 2 public class Test06{
 3     public static void main(String[] args){
 4         
 5         //一個有序的數組,刪除一個元素后依然有序。
 6         int[] arr = {1,3,5,7,9,10,17,23};
 7         
 8         //刪除t
 9         Scanner input = new Scanner(System.in);
10         System.out.println("請輸入要刪除的數:");
11         int t = input.nextInt();
12         //int t = 3;
13         
14         //(1)找出要刪除的數的位置
15         int loc = -1;
16         for(int i = 0;i < arr.length;i++){
17             if(arr[i] == t){
18                 loc = i;
19                 break;
20             }
21         }
22         
23         //(2)移動元素
24         if(loc < 0){
25             System.out.println(t+"不在數組中");
26         }else{
27             for(int j = loc;j < arr.length-1;j++){
28                 arr[j] = arr[j+1];
29             }
30         }
31         //最后一個元素置0
32         arr[arr.length-1] = 0;
33         //檢驗,遍歷數組
34         for(int i = 0;i < arr.length;i++){
35             System.out.print(arr[i] + "\t");
36         }
37     }
38 }

 

(3)冒泡排序算法作用:將一個無序的數組排列成有序的數組

代碼如下:

 1 public class Test07{
 2     public static void main(String[] args){
 3         //冒泡排序算法:用於將無序的數組排列成有序的
 4         int[] arr = {3,2,1,4,7,5,9};
 5         int temp = 0;
 6         // 外層循環控制趟數
 7         for(int i=0;i<arr.length;i++){
 8             for(int j = 0;j < arr.length-1-i;j++){  //內層循環控制兩兩交換
 9                 if(arr[j] > arr[j+1]){
10                     temp = arr[j];
11                     arr[j] = arr[j+1];
12                     arr[j+1] = temp;
13                 }
14             }
15         }
16         //驗證,遍歷數組
17         for(int i = 0;i < arr.length;i++){
18             System.out.print(arr[i]+"\t");
19         }
20     }
21 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM