日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转载] java实现四种常用排序算法

發布時間:2025/3/11 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] java实现四种常用排序算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接: 用Java排序

四種常用排序算法?

##注:從小到大排?

##冒泡排序## 特點:效率低,實現簡單 思想:每一趟將待排序序列中最大元素移到最后,剩下的為新的待排序序列,重復上述步驟直到排完所有元素。這只是冒泡排序的一種,當然也可以從后往前排。?

public void bubbleSort(int array[]) {

? ? ? ? int t = 0;

? ? ? ? for (int i = 0; i < array.length - 1; i++)

? ? ? ? ? ? for (int j = 0; j < array.length - 1 - i; j++)

? ? ? ? ? ? ? ? if (array[j] > array[j + 1]) {

? ? ? ? ? ? ? ? ? ? t = array[j];

? ? ? ? ? ? ? ? ? ? array[j] = array[j + 1];

? ? ? ? ? ? ? ? ? ? array[j + 1] = t;

? ? ? ? ? ? ? ? }

? ? }

?

?

##選擇排序## 特點:效率低,容易實現。 思想:每一趟從待排序序列選擇一個最小的元素放到已排好序序列的末尾,剩下的為待排序序列,重復上述步驟直到完成排序。?

public void selectSort(int array[]) {

? ? ? ? int t = 0;

? ? ? ? for (int i = 0; i < array.length - 1; i++){

? ? ? ? ? ? int index=i;

? ? ? ? ? ? for (int j = i + 1; j < array.length; j++)

? ? ? ? ? ? ? ? if (array[index] > array[j])

? ? ? ? ? ? ? ? ? ? index=j;

? ? ? ? ? ? if(index!=i){ //找到了比array[i]小的則與array[i]交換位置

? ? ? ? ? ? ? ? t = array[i];

? ? ? ? ? ? ? ? array[i] = array[index];

? ? ? ? ? ? ? ? array[index] = t;

? ? ? ? ? ? }

? ? ? ? }

? ? }

?

?

##插入排序## 特點:效率低,容易實現。 思想:將數組分為兩部分,將后部分元素逐一與前部分元素比較,如果前部分元素比array[i]小,就將前部分元素往后移動。當沒有比array[i]小的元素,即是合理位置,在此位置插入array[i]?

public void insertionSort(int array[]) {

? ? ? ? int i, j, t = 0;

? ? ? ? for (i = 1; i < array.length; i++) {

? ? ? ? ? ? if(a[i]<a[i-1]){

? ? ? ? ? ? ? ? t = array[i];

? ? ? ? ? ? ? ? for (j = i - 1; j >= 0 && t < array[j]; j--)

? ? ? ? ? ? ? ? ? ? array[j + 1] = array[j];

? ? ? ? ? ? ? ? //插入array[i]

? ? ? ? ? ? ? ? array[j + 1] = t;

? ? ? ? ? ? }

? ? ? ? }

}

?

快速排序?

特點:高效,時間復雜度為nlogn。 采用分治法的思想:首先設置一個軸值pivot,然后以這個軸值為劃分基準將待排序序列分成比pivot大和比pivot小的兩部分,接下來對劃分完的子序列進行快排直到子序列為一個元素為止。?

public void quickSort(int array[], int low, int high) {// 傳入low=0,high=array.length-1;

? ? ? ? int pivot, p_pos, i, t;// pivot->位索引;p_pos->軸值。

? ? ? ? if (low < high) {

? ? ? ? ? ? p_pos = low;

? ? ? ? ? ? pivot = array[p_pos];

? ? ? ? ? ? for (i = low + 1; i <= high; i++)

? ? ? ? ? ? ? ? if (array[i] > pivot) {

? ? ? ? ? ? ? ? ? ? p_pos++;

? ? ? ? ? ? ? ? ? ? t = array[p_pos];

? ? ? ? ? ? ? ? ? ? array[p_pos] = array[i];

? ? ? ? ? ? ? ? ? ? array[i] = t;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? t = array[low];

? ? ? ? ? ? array[low] = array[p_pos];

? ? ? ? ? ? array[p_pos] = t;

? ? ? ? ? ? // 分而治之

? ? ? ? ? ? quickSort(array, low, p_pos - 1);// 排序左半部分

? ? ? ? ? ? quickSort(array, p_pos + 1, high);// 排序右半部分

? ? ? ? }

?

?

測試demo:?

import java.util.Arrays;

public class sortTest {

? ? // 冒泡排序

? ? public void bubbleSort(int array[]) {

? ? ? ? int t = 0;

? ? ? ? for (int i = 0; i < array.length - 1; i++)

? ? ? ? ? ? for (int j = 0; j < array.length - 1 - i; j++)

? ? ? ? ? ? ? ? if (array[j] > array[j + 1]) {

? ? ? ? ? ? ? ? ? ? t = array[j];

? ? ? ? ? ? ? ? ? ? array[j] = array[j + 1];

? ? ? ? ? ? ? ? ? ? array[j + 1] = t;

? ? ? ? ? ? ? ? }

? ? }

?

? ? // 選擇排序

? ? public void selectSort(int array[]) {

? ? ? ? int t = 0;

? ? ? ? for (int i = 0; i < array.length - 1; i++){

? ? ? ? ? ? int index=i;

? ? ? ? ? ? for (int j = i + 1; j < array.length; j++)

? ? ? ? ? ? ? ? if (array[index] > array[j])

? ? ? ? ? ? ? ? ? ? index=j;

? ? ? ? ? ? if(index!=i){ //找到了比array[i]小的則與array[i]交換位置

? ? ? ? ? ? ? ? t = array[i];

? ? ? ? ? ? ? ? array[i] = array[index];

? ? ? ? ? ? ? ? array[index] = t;

? ? ? ? ? ? }

? ? ? ? }

? ? }

?

public void insertionSort(int array[]) {

? ? ? ? int i, j, t = 0;

? ? ? ? for (i = 1; i < array.length; i++) {

? ? ? ? ? ? if(a[i]<a[i-1]){

? ? ? ? ? ? ? ? t = array[i];

? ? ? ? ? ? ? ? for (j = i - 1; j >= 0 && t < array[j]; j--)

? ? ? ? ? ? ? ? ? ? array[j + 1] = array[j];

? ? ? ? ? ? ? ? //插入array[i]

? ? ? ? ? ? ? ? array[j + 1] = t;

? ? ? ? ? ? }

? ? ? ? }

}

?

? ? // 分治法快速排序

? ? public void quickSort(int array[], int low, int high) {// 傳入low=0,high=array.length-1;

? ? ? ? int pivot, p_pos, i, t;// pivot->位索引;p_pos->軸值。

? ? ? ? if (low < high) {

? ? ? ? ? ? p_pos = low;

? ? ? ? ? ? pivot = array[p_pos];

? ? ? ? ? ? for (i = low + 1; i <= high; i++)

? ? ? ? ? ? ? ? if (array[i] > pivot) {

? ? ? ? ? ? ? ? ? ? p_pos++;

? ? ? ? ? ? ? ? ? ? t = array[p_pos];

? ? ? ? ? ? ? ? ? ? array[p_pos] = array[i];

? ? ? ? ? ? ? ? ? ? array[i] = t;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? t = array[low];

? ? ? ? ? ? array[low] = array[p_pos];

? ? ? ? ? ? array[p_pos] = t;

? ? ? ? ? ? // 分而治之

? ? ? ? ? ? quickSort(array, low, p_pos - 1);// 排序左半部分

? ? ? ? ? ? quickSort(array, p_pos + 1, high);// 排序右半部分

? ? ? ? }

? ? }

?

? ? public static void main(String[] args) {

? ? ? ? // TODO Auto-generated method stub

? ? ? ? int[] array = { 37, 47, 23, 100, 19, 56, 56, 99, 9 };

? ? ? ? sortTest st = new sortTest();

? ? ? ? // st.bubbleSort(array);

? ? ? ? // st.selectSort(array);

? ? ? ? // st.insertionSort(array);

? ? ? ? st.quickSort(array, 0, array.length - 1);

? ? ? ? System.out.println("排序后:" + Arrays.toString(array));

? ? }

}

總結

以上是生活随笔為你收集整理的[转载] java实现四种常用排序算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。