数据结构与算法排序算法

来源:互联网 发布:电脑网络连接在哪里找 编辑:程序博客网 时间:2024/03/28 22:50

常见的基本排序算法
/*
 * 冒泡排序
 */
public class BubbleSort {
public static void main(String[] args) {
int[] a = { 12, 3, 5, 9, 1, 6 };
int temp = 0;
for (int i = a.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}


/*
 * 选择排序
 */
class SelectSort {
public static void main(String[] args) {
int[] a = { 12, 3, 5, 9, 1, 6 };
int min = 0;
int temp = 0;
if (a == null || a.length == 0)
return;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (min != i) {
temp = a[i];
a[i] = a[min];
a[min] = temp;


}
}
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}


}


/*
 * 插入排序:适用于少量数据的排序
 */
class InsertSort {
public static void main(String[] args) {
int[] a = { 12, 3, 5, 9, 1, 6 };
for (int i = 1; i < a.length; i++) {
if (a[i - 1] > a[i]) {
int temp = a[i];
int j = i;
while (j > 0 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}


/*
 * 归并排序
 */
class recMergeSort {
public static void main(String[] args) {
int[] a = { 12, 3, 5, 9, 1, 6 };
int[] s = Merge(a);
for (int i = 0; i < s.length; i++)
System.out.print(s[i] + "--");
}


public static int[] Merge(int[] a) {
int[] temp = new int[a.length];
recMergeSort(a, temp, 0, a.length - 1);
return a;
}


private static void recMergeSort(int[] a, int[] temp, int low, int high) {
if (low == high)
return;
else {
int Mid = (low + high) / 2;
recMergeSort(a, temp, low, Mid);
recMergeSort(a, temp, Mid + 1, high);
MergeSort(a, temp, low, Mid + 1, high);
}
}


private static void MergeSort(int[] a, int[] temp, int low, int Mid,
int high) {
int j = 0;
int low1 = low;
int mid = Mid - 1;
int n = high - low + 1;
while (low <= mid && Mid <= high)
if (a[low] < a[Mid])
temp[j++] = a[low++];
else
temp[j++] = a[Mid++];
while (low <= mid)
temp[j++] = a[low++];
while (Mid <= high)
temp[j++] = a[Mid++];
for (j = 0; j < n; j++)


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


}
}


/*
 * 快速排序
 */
class QuickSort {
public static void main(String[] args) {
int[] a = { 12, 3, 5, 9, 1, 6 };
int[] s = QSort(a);
for(int i = 0;i < s.length;i ++)
System.out.println(s[i]);
}
 public static int[] QSort(int[]a)
 {
int[] temp = new int[a.length];
QuickSort(a,temp,0,a.length-1);
return a;
 }
public static void QuickSort(int[] a,int[]temp,int left, int right) {
if (left > right)
return;
else {
long pivote = temp[right];
int partition = PartitionIn(a,temp, left, right, pivote);
QuickSort(a, temp,left, partition - 1);
QuickSort(a,temp, partition + 1, right);
}
}


public static int PartitionIn(int[] a,int[]temp, int left, int right, long pivote) {
int leftPtr = left - 1;
int rightPtr = right + 1;
while (true) {
while (leftPtr < right && temp[++leftPtr] < pivote)
;
while (rightPtr > left && temp[--rightPtr] > pivote)
;
if (leftPtr >= rightPtr)
break;
else
swap(a,temp,leftPtr, rightPtr);
}
return leftPtr;
}
public static void swap(int[]a,int[]temp,int x,int y)
{
int t;
if(temp[x] > temp[y])
{
t = temp[y];
temp[y] = temp[x];
temp[x] = t;
}
}
}
/*
*希尔排序
*/
class ShellSort{
public static void main(String[] args) {
int[] a = {12,3,5,9,1,6};
int[] theArray = new int[a.length];
int inner,outer,nElems = 0;
int temp;
int h = 1;
while(h <= nElems/3)
h = h *3 + 1;
while(h > 0)
{
for(outer = h;outer < nElems;outer ++)
{
temp = theArray[outer];
inner = outer;
while(inner > h - 1 && theArray[inner - h] >= temp)
{
theArray[inner] = theArray[inner - h];
inner -= h;
}
theArray[inner] = temp;
}
h = (h - 1)/3;
}
for(int i = 0;i < a.length;i ++)
System.out.println(a[i]);
}
}


0 0