第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

java實(shí)現(xiàn)排序

標(biāo)簽:
Java

import java.util.Arrays;




public class SortDemo {

private static long[] arr = {6,5,2,7,1,8,4,3};

// private static long[] arr = {1,2,3,4,5,6,7,8};

private static int count = 0;

/**

* 冒泡排序

* O(n2)

*/

public static void bubble(){

long temp = Long.MIN_VALUE;

int len = arr.length;

for(int j = 0;j<len;j++){

for(int i = 0;i<len-1;i++){

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

temp = arr[i+1];

arr[i+1] = arr[i];

arr[i] = temp;

}

}

}

}

/**

* 选择排序

* O(n2)

*/

public static void select(){

int len = arr.length;

long temp = Long.MIN_VALUE;

int index = 0;

for(int j = 0;j<len;j++){

index = j;

for(int i = j;i<len;i++){

if(arr[i]<arr[index]){

index = i;

}

}

temp = arr[j];

arr[j] = arr[index];

arr[index] = temp;

print();

}

}

/**

* 插入排序

* O(n2)

*/

public static void insert(){

int len = arr.length;

long temp;

for(int i=1;i<len;i++){

int j = i-1;

temp = arr[i];

while(j>=0 && temp<arr[j]){

arr[j+1] = arr[j];

j--;

}

arr[j+1] = temp;

print();

}

}

/**

* 快速排序

* O(N*longN)

*/

public static void quick(){

quick(arr,0,arr.length-1);

print();

}

private static long[] quick(long[] a,int lo,int hi){

if(hi<lo)return a;

long temp = 0;

int index = lo;

for(int i = lo;i<hi;i++){

if(a[i]<a[hi]){

temp = a[i];

a[i] = a[index];

a[index++] = temp;

}

}

temp = a[hi];

a[hi] = a[index];

a[index] = temp;

if(index==lo)return a;

quick(a,lo,index-1);

quick(a,index+1,hi);

return a;

}

/**

* 归并排序

* O(N*longN)

*/

public static void merge (){

merge(arr,0,arr.length-1);

print();

}

public static void merge(long[] a,int lo,int hi){

int mid = lo +(hi-lo)/2;

if(lo < hi){

merge(a,lo,mid);

merge(a,mid+1,hi);

merge(a,lo,mid,hi);

}

}

public static void merge(long[] a,int lo,int mid,int hi){

long[] temp = new long[hi-lo+1];

int i=lo,j=mid+1,index=0;

while(i<=mid&&j<=hi){

if(a[i]>a[j]){

temp[index++] = a[j++];

}else{

temp[index++] = a[i++];

}

}

while(i<=mid){

temp[index++] = a[i++];

}

while(j<=hi){

temp[index++] = a[j++];

}

System.arraycopy(temp, 0, a, lo, temp.length);

}

public static void print(){

System.out.println(Arrays.toString(arr));

}

public static void main(String[] args) {

merge();

}

}


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開(kāi)發(fā)工程師
手記
粉絲
39
獲贊與收藏
245

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消