java代碼測試---插入排序和選擇排序

來源:互聯網
上載者:User

標籤:

  1 public class QuickSort {  2      3     //插入排序  4     //插入前的序列是排序好的,將新插入的數值與之前的數值比較  5     //直到找到合適的位置  6     public static int[] quickSort(int[] arr){  7           8         for(int j=1;j<arr.length;j++){  9             int key = arr[j]; 10             int i = j-1; 11              12             while(i>=0 && arr[i]<key){ 13                 arr[i+1] = arr[i]; 14                 i = i -1; 15                 arr[i+1] = key; 16             } 17         } 18          19         return arr; 20     } 21      22     //查詢一個值V是否在數組內,如果在數組內輸出下標,否則輸出‘NIL’ 23     public static String quickSortIn(int[] arr,int V){ 24         List<Integer> list = new ArrayList<Integer>(); 25         for(int i = 0;i<arr.length;i++){ 26             if(V == arr[i]){ 27                 list.add(i);//如果V在數組內,將下標存入list 28             } 29         } 30          31         if(list != null && !list.isEmpty()){//如果list不為空白,迴圈輸出 32  33             StringBuilder str = new StringBuilder(""); 34             for(int i = 0; i < list.size(); i++){ 35                 str.append("V = "+"arr["+list.get(i)+"]  "); 36             } 37             return str.toString(); 38              39         }else{//如果list為空白,輸出‘NIL’ 40             return "NIL"; 41         } 42     } 43      44     //選擇排序 45     public static void selectSort(int[] a) { 46         int i;        // 有序區的末尾位置 47         int j;        // 無序區的起始位置 48         int min;    // 無序區中最小元素位置 49  50         for(i=0; i<a.length; i++) { 51             min=i; 52  53             // 找出"a[i+1] ... a[n]"之間的最小元素,並賦值給min。 54             for(j=i+1; j<a.length; j++) { 55                 if(a[j] < a[min]) 56                     min=j; 57             } 58  59             // 若min!=i,則交換 a[i] 和 a[min]。 60             // 交換之後,保證了a[0] ... a[i] 之間的元素是有序的。 61             if(min != i) { 62                 int tmp = a[i]; 63                 a[i] = a[min]; 64                 a[min] = tmp; 65             } 66         } 67     } 68      69     //查詢數組的最大值 70     public static int selectMax(int[] arr){ 71         int max = arr[0]; 72         for(int i =0;i<arr.length;i++){ 73             if(arr[i]>max){ 74                 max = arr[i]; 75             } 76         }         77         return max; 78     } 79      80     //查詢數組的最小值 81     public static int selectMin(int[] arr){ 82         int min = arr[0]; 83         for(int i =0;i<arr.length;i++){ 84             if(arr[i]<min){ 85                 min = arr[i]; 86             } 87         }         88         return min; 89     } 90      91      92     public static void main(String[] args) { 93         int[] aaa = {1,5,2,3,4,55,11,22,33,4,22,1}; 94         int[] bbb = {31,41,59,26,41,58}; 95         selectSort(bbb); 96         for(int a : bbb){ 97             System.out.print(a+"  "); 98         } 99         System.out.println();100         System.out.println(quickSortIn(bbb,41));101         System.out.println(selectMin(bbb));102     }103 }

 

java代碼測試---插入排序和選擇排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.