【演算法】排序

來源:互聯網
上載者:User

標籤:file   選擇   i+1   print   public   i++   break   單選   temp   

 1 package main; 2  3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.util.*; 8 import java.util.concurrent.ExecutorService; 9 import java.util.concurrent.Executors;10 import java.util.concurrent.locks.Condition;11 import java.util.concurrent.locks.Lock;12 import java.util.concurrent.locks.ReentrantLock;13 14 public class Main {15     public static void main(String[] args) {16         int[] array = {100, 0, 1, 2, 4, 3, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12};17 18         //冒泡排序19         for (int i = 0; i < array.length; i++) {20             boolean flag = false;21             for (int j = 0; j < array.length - i - 1; j++) {22                 if (array[j] > array[j + 1]) {23                     int temp = array[j];24                     array[j] = array[j + 1];25                     array[j + 1] = temp;26                     flag = true;27                 }28             }29             if (flag) {30                 break;31             }32         }33         System.out.println(Arrays.toString(array));34 35         int[] array_2 = {100, 0, 1, 2, 4, 3, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12};36         //簡單選擇排序37         for (int i = 0; i < array.length; i++) {38             int loc = i;39             for (int j = i + 1; j < array.length; j++) {40                 if (array_2[j] < array_2[loc]) {41                     loc = j;42                 }43             }44             if (loc != i) {45                 int temp = array_2[i];46                 array_2[i] = array_2[loc];47                 array_2[loc] = temp;48             }49         }50         System.out.println(Arrays.toString(array_2));51 52         int[] array_3 = {3,5,4,2,1,6,7,8};53         //快速排序54         quitSort(array_3,0,array_3.length-1);55         System.out.println(Arrays.toString(array_3));56 57     }58 59     static void quitSort(int[] array, int left, int right) {60         int i = left;61         int j = right;62         if (left >= right) {63             return;64         }65         int base = array[i];66         while (i < j) {67             while (i < j && array[j] >= base) {68                 j--;69             }70 71             if (j > i) {72                 array[i] = array[j];73             }74             while (i < j && array[i] <= base) {75                 i++;76             }77             if (i < j) {78                 array[j] = array[i];79             }80         }81         array[i] = base;82         quitSort(array,left,i-1);83         quitSort(array,i+1,right);84     }85 }

 

【演算法】排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.