标签:-- code ring index for span tac pack public
1 package com.aixuexi.contact; 2 3 public class BubbleSort { 4 public static void main(String[] args) { 5 int arr[] = new int[] {985,2,6}; 6 //冒泡 O(N*N) 7 for(int end = arr.length-1; end > 0; end--) { 8 for(int i = 0; i < end; i++) { 9 if(arr[i] > arr[i+1]) { 10 int tmp = arr[i]; 11 arr[i] = arr[i+1]; 12 arr[i+1] = tmp; 13 } 14 } 15 } 16 17 //选择 O(N*N) 18 for(int i = 0; i < arr.length -1; ++i) { 19 20 int Indexmin = i; 21 22 for(int j = i + 1; j < arr.length; ++j) { 23 Indexmin = arr[j] < arr[Indexmin] ? j : Indexmin; //返回下表 互换下标 24 } 25 26 //换数 不是换下标 27 int tmp = arr[i]; 28 arr[i] = arr[Indexmin]; 29 arr[Indexmin] = tmp; 30 } 31 32 33 //插入 O(N) 34 for(int i = 1; i < arr.length; i++) { 35 for(int j = i - 1; j >= 0 && arr[j] > arr[j+1]; j--) { 36 arr[i] = arr[j] ^ arr[j+1]; 37 arr[j] = arr[j] ^ arr[j+1]; 38 arr[i] = arr[j] ^ arr[j+1]; 39 } 40 } 41 42 43 System.out.println(); 44 for(int i = 0; i <= arr.length-1; i++) { 45 System.out.printf("%d ",arr[i]); 46 } 47 48 49 } 50 }
标签:-- code ring index for span tac pack public
原文地址:https://www.cnblogs.com/yaozhenhua/p/11185151.html