<div class="it610-blog-content-contain" style="font-size: 14px"></div>
BubbleSort
package SortTest; public class BubbleSort { int[] a={196,182,123,104,111,106,185,194,192,159}; int count=a.length; public void exchange(int index1,int index2){ int temp; temp=a[index1]; a[index1]=a[index2]; a[index2]=temp; } public static void main(String[] args) { BubbleSort bs=new BubbleSort(); for(int n=bs.count-1;n>0;n--){ for(int i=0;i<n;i++){ if(bs.a[i]>bs.a[i+1]) bs.exchange(i,i+1); } System.out.println("the " + (10-n) + " time: "); for(int m=0;m<bs.count;m++){ System.out.print(bs.a[m] +" "); } System.out.println(); } } }
MergeSort
package SortTest; import java.util.ArrayList; import java.util.List; public class MergeSort { int iaArray[] = {24, 5, 3, 35, 14, 23, 19}; int start=0; int end=0; void Merge(MergeSort ms,List<Integer> ipA, int iEnd1, int iEnd2) { int i = 0; int j = iEnd1; int k = 0; int[] ipTemp = new int[iEnd2]; // Take each next smallest element while (i < iEnd1 && j < iEnd2) { if (ipA.get(i) <ipA.get(j) ) { ipTemp[k] = ipA.get(i) ; ++i; } else { ipTemp[k] = ipA.get(j); ++j; } ++k; } // Copy any remaining elements of the 1st array while (i < iEnd1) { ipTemp[k] = ipA.get(i); ++i; ++k; } // Copy any remaining elements of the 2nd array while (j < iEnd2) { ipTemp[k] = ipA.get(j); ++j; ++k; } // Copy the merged array back to the original for (int n=start; n <= end; n++) { ms.iaArray[n]=ipTemp[n-start]; } } public static void main(String[] args) { MergeSort ms=new MergeSort(); int iSize = 7; // Merge Sort for (int i = 1; i < iSize; i *= 2) { for (int j = 0; j < iSize - i; j += 2*i) { int iEnd2 = (2*i < iSize - j) ? 2*i : iSize - j; ArrayList<Integer> beMerged=new ArrayList<Integer>(); ms.start=j; ms.end=j+iEnd2-1; for(int n=j;n<j+iEnd2;n++){ beMerged.add(ms.iaArray[n]); } ms.Merge(ms,beMerged, i, iEnd2); } } // Output sorted array for (int i = 0; i < iSize; i++){ System.out.println(ms.iaArray[i]); } } }
HeapSort
package SortTest;
public class HeapSort {
int Left(int iIndex) {
return ((iIndex << 1) + 1);
}
int Right(int iIndex) {
return ((iIndex << 1) + 2);
}
int Parent(int iIndex) {
return ((iIndex - 1) >> 1);
}
void Swap(int X, int Y,int[] array) {
int iTemp =array[X];
array[X] =array[Y];
array[Y] =iTemp;
}
int SwapWithChild(int iIndex, int[] ipHeap, int iSize) {
int iLeft = Left(iIndex);
int iRight = Right(iIndex);
int iLargest = iIndex;
if (iRight < iSize) {
if (ipHeap[iLeft] < ipHeap[iRight]) {
iLargest = iRight;
} else {
iLargest = iLeft;
}
if (ipHeap[iIndex] > ipHeap[iLargest]) {
iLargest = iIndex;
}
} else if (iLeft < iSize) {
if (ipHeap[iIndex] < ipHeap[iLeft]) {
iLargest = iLeft;
}
}
if (ipHeap[iIndex] < ipHeap[iLargest]) {
Swap(iIndex, iLargest,ipHeap);
}
return iLargest;
}
void RemoveRoot(int[] ipHeap, int iSize) {
// Swap the last element with the root
Swap(0, iSize - 1,ipHeap);
--iSize;
int iLasti = 0;
int i = SwapWithChild(0, ipHeap, iSize);
while (i != iLasti) {
iLasti = i;
i = SwapWithChild(i, ipHeap, iSize);
}
}
int SwapWithParent(int i, int[] ipHeap) {
if (i < 1) {
return i;
}
int iParent = Parent(i);
if (ipHeap[i] > ipHeap[iParent]) {
Swap(i, iParent,ipHeap);
return iParent;
} else {
return i;
}
}
void AddElement(int iNewEntry, int[] ipHeap, int iSize) {
ipHeap[iSize] = iNewEntry;
int iLasti = iSize;
int i = SwapWithParent(iLasti, ipHeap);
while (iLasti != i) {
iLasti = i;
i = SwapWithParent(i, ipHeap);
}
}
void OutputArray(int[] ipArray, int iBar, int iSize) {
for (int i = 0; i < iSize; ++i) {
if (i == iBar) {
System.out.print("| ");
}
System.out.print(ipArray[i] + " " );
}
System.out.println();
}
public static void main(String args[]){
HeapSort hs=new HeapSort();
int iaArray[]={67,65,77,38,97,3,33,49,32,44,56,78,54,87,447};
// Output the heap after each element that we add
for (int i = 0; i < 15; ++i) {
hs.AddElement(iaArray[i], iaArray, i);
hs.OutputArray(iaArray, i + 1, 15);
System.out.println( "---------------------------------------------");
}
// Output the heap after each element that we remove
for (int i = 0; i < 14; ++i) {
hs.RemoveRoot(iaArray, 15 - i);
hs.OutputArray(iaArray, 14 - i, 15);
System.out.println( "---------------------------------------------");
}
}
}