?? sortways.java
字號(hào):
public class SortWays
{
static void swap(double[] a, int i, int j) { //交換數(shù)組中兩個(gè)數(shù)
double t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
static int Max(double a[], int n) { //在長(zhǎng)度為n的數(shù)組中尋求最大的數(shù)的序號(hào)
int pos = 0;
for (int i = 1; i < n; i++)
if (a[pos] < a[i])
pos = i;
return pos;
}
static void printArray(double a[]) { //輸出數(shù)組,每行15個(gè)元素
int i = 0;
System.out.print("Result: ");
for (i = 0; i < a.length; i++) {
if (i % 15 == 0 && i > 0)
System.out.print("\n ");
System.out.print(a[i] + " ");
}
System.out.println();
}
static void interchange(double m, double n) { //交換兩個(gè)數(shù)的值
double temp;
temp = m;
m = n;
n = temp;
}
static void QuickSort(double[] pData,int[] pDataNum,int left,int right) {
int i,j;
int iTemp;
double middle,strTemp;
i = left;
j = right;
middle = pData[(left+right)/2];
do{
while((pData[i]<middle) && (i<right))
i++;
while((pData[j]>middle) && (j>left))
j--;
if(i<=j){
strTemp = pData[i];
pData[i] = pData[j];
pData[j] = strTemp;
iTemp = pDataNum[i];
pDataNum[i] = pDataNum[j];
pDataNum[j] = iTemp;
i++;
j--;
}
}while(i<=j);//如果兩邊掃描的下標(biāo)交錯(cuò),就停止(完成一次)
if(left<j)
QuickSort(pData,pDataNum,left,j);
if(right>i)
QuickSort(pData,pDataNum,i,right);
}
static void heapInitialize(double a[], int size)
{// Initialize max heap to array
for (int i = size/2; i >= 0; i--) {
double y = a[i]; // root of subtree
// find place to put y
int c = 2*i; // parent of c is target
// location for y
while (c <= size-1) {
// heap[c] should be larger sibling
if (c < size-1 &&a[c] > a[c+1]) c++;
// can we put y in heap[c/2]?
if (y <= a[c]) break; // yes
// no
a[c/2] = a[c]; // move child up
c *= 2; // move down a level
}
a[c/2] = y;
}
}
static void deleteMax(double heap[],int size)
{// Set x to max element and delete
// max element from heap.
// check if heap is empty
if (size ==0)
// empty
;
else{
// restucture heap
double y = heap[--size]; // last element
// find place for y starting at root
int i = 0, // current node of heap
ci = 1; // child of i
while (ci <size) {
// heap[ci] should be larger child of i
if (ci < size -1&& heap[ci] > heap[ci+1]) ci++;
// can we put y in heap[i]?
if (y <= heap[ci]) break; // yes
// no
heap[i] = heap[ci]; // move child up
i = ci; // move down a level
ci *= 2;
}
heap[i] = y;
}
}
static void insertionSort(double a[]) { //插入排序
int n = a.length;
for (int i = 1; i < n; i++) {
double t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
}
static void bubbleSort(double a[]) { //冒泡排序
int n = a.length;
for (int i = n - 1; i > 0; i--)
for (int j = 0; j < i; j++)
if (a[j] > a[j + 1])
swap(a, j + 1, j);
}
static void selectionSort(double a[]) { //選擇排序
int n = a.length;
for (int size = n; size > 1; size--) {
int j = Max(a, size);
swap(a, j, size - 1);
}
}
static void quickSort(double a []){//快速排序
int l=a.length,i;
int pDataNum[]=new int[l];
for (i=0;i<l;i++)
pDataNum[i]=i;
QuickSort(a,pDataNum,0,l-1);
}
static void heapSort(double a[]){//堆排序
int n=a.length;
heapInitialize(a, n);
double []b=new double[ n];
for(int i=0;i<n;i++)
b[i]=a[i];
for(int i=0;i<n;i++){
a[i]=b[0];
deleteMax(b,n-i);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -