5 排序演算法2

2022-08-26 13:03:13 字數 3115 閱讀 6510

* 選擇排序

* * 在未排序序列中找到最小元素,存放到排序序列的起始位置 再從剩餘未排序元素中繼續尋找最小元素,然後放到排序序列末尾。

* 以此類推,直到所有元素均排序完畢。

*/int num = ;

//控制遍歷次數

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

// 控制遍歷次數

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

//控制遍歷次數

for (int i = 1; i < num2.length; i++)

//j=0;num[0]=2;2,3,4...

num2[j] = temp;

//列印每次移動後的陣列

system.out.println("第"+i+"次移動後的陣列:");

for (int k : num2)

system.out.println();

}//輸出

system.out.println("經過直接插入排序之後的陣列:");

for (int i : num2)

/*

* 階乘的方法遞迴演算法

(1)申請空間,使其大小為兩個已經排序序列之和,該空間用來存放合併後的序列

(2)設定兩個指標,最初位置分別為兩個已經排序序列的起始位置

(3)比較兩個指標所指向的元素,選擇相對小的元素放入到合併空間,並移動指標到下一位置

(4)重複步驟3直到某一指標達到序列尾

(5)將另一串行剩下的所有元素直接複製到合併序列尾

*/public static void merge(int num, int low, int mid, int high) else

}// 把左邊剩餘的數移入陣列

while (left <= mid)

// 把右邊邊剩餘的數移入陣列

while (right <= high)

// 把新陣列中的數覆蓋nums陣列

for (int i = 0; i < temp.length; i++)

system.out.println(arrays.tostring(temp));

}public static int mergesort(int num, int low, int high)

return num;

}public static void main(string args) ;

int num0 = mergesort(num3, 0, num3.length-1);

system.out.println("排序結果:" + arrays.tostring(num0));

}

body

body>*:first-child

body>*:last-child

p, blockquote, ul, ol, dl, table, pre

h1, h2, h3, h4, h5, h6

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code

h1 h2

h3 h4

h5 h6

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p

a a:hover

ul, ol

ul li>:first-child, ol li>:first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:first-of-type, ol li ul:first-of-type

ul ul, ul ol, ol ol, ol ul

dl dl dt

dl dt:first-child

dl dt>:first-child

dl dt>:last-child

dl dd

dl dd>:first-child

dl dd>:last-child

pre, code, tt

code, tt

pre>code

pre

pre code, pre tt

kbd

blockquote

blockquote>:first-child

blockquote>:last-child

hr table th

table th, table td

table tr

table tr:nth-child(2n)

img

2 排序演算法 快速排序

問題描述 利用快速排序演算法對下列例項排序,在演算法執行過程中,寫出陣列 a第一次排序後被分割的過程。a 65,70,75,80,85,55,50,2 解題思想 在快速排序中,記錄的比較和交換是從兩端向中間進行的,關鍵字較大的記錄一次就能交換到後面的單元,總的比較和移動次數較少。對於輸入的陣列a p...

2 排序演算法 氣泡排序

氣泡排序 bubble sort 也是一種簡單直觀的排序演算法。它重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重複地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個演算法的名字由來是因為越小的元素會經由交換慢慢 浮 到數列的頂端。作為最簡單...

演算法筆記2 排序

1.選擇排序 首先,找到陣列中最小的那個元素,其次,將他和陣列第乙個元素交換位置,再次,在剩下的元素中找到最小的元素,將他和陣列的第二個元素交換位置。如此反覆,直到將整個陣列排序。不斷的選擇剩餘元素的最小值 2.插入排序 對部分有序陣列很有效 為了給要插入的元素騰出空間,我們需要將其餘所有元素在插入...