冒泡排序
package 冒泡排序;
import java.util.Arrays;
public class Bubble {
public static int[] sort(int[] a){
for (int i=a.length-1;i>0;i--){
for (int j=0;j<i;j++){
if(greater(a[j],a[j+1])){
exch(a,j,j+1);
}
}
}
return a;
}
private static Boolean greater(int v,int w){
if (v>=w){
return true;
}
return false;
}
private static void exch(int[] a,int i,int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
public static void main(String[] args) {
int[] a={4,5,6,3,2,1};
int[] sort = Bubble.sort(a);
System.out.println(Arrays.toString(sort));
}
}
选择排序
package cn.itcast.algorithm.sort;
public class Selection {
public static void sort(Comparable[] a){
for(int i=0;i<=a.length-2;i++){
int minIndex = i;
for(int j=i+1;j<a.length;j++){
if (greater(a[minIndex],a[j])){
minIndex=j;
}
}
exch(a,i,minIndex);
}
}
private static boolean greater(Comparable v,Comparable w){
return v.compareTo(w)>0;
}
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
插入排序
package 插入排序;
import java.util.Arrays;
public class Insertion {
public static void sort(int[] a){
for (int i=1;i<a.length;i++){
for (int j=i;j>0;j--){
if (greater(a[j-1],a[j])){
exch(a,j-1,j);
}else {
break;
}
}
}
}
private static Boolean greater(int v,int w){
if (v>=w){
return true;
}
return false;
}
private static void exch(int[] a,int i,int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
public static void main(String[] args) {
int[] a={4,3,2,10,12,1,5,6};
Insertion.sort(a);
System.out.println(Arrays.toString(a));
}
}
希尔排序
package 希尔排序;
import java.util.Arrays;
public class Shell {
public static void sort(Comparable[] a) {
int N = a.length;
int h = 1;
while (h < N / 2) {
h = h * 2 + 1;
}
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h; j -= h) {
if (greater(a[j - h], a[j])) {
exch(a, j, j - h);
} else {
break;
}
}
}
h /= 2;
}
}
private static boolean greater(Comparable v, Comparable w) {
return v.compareTo(w) > 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void main(String[] args) {
Integer[] a = {9, 1, 2, 5, 7, 4, 8, 6, 3, 5};
Shell.sort(a);
System.out.println(Arrays.toString(a));
}
}
归并排序
package 归并排序;
import java.util.Arrays;
public class Merge {
private static Comparable[] assist;
public static void sort(Comparable[] a) {
assist = new Comparable[a.length];
int lo = 0;
int hi = a.length - 1;
sort(a, lo, hi);
}
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) {
return;
}
int mid = lo + (hi - lo) / 2;
sort(a, lo, mid);
sort(a, mid + 1, hi);
merge(a, lo, mid, hi);
}
private static void merge(Comparable[] a, int lo, int mid, int hi) {
int i = lo;
int p1 = lo;
int p2 = mid + 1;
while (p1 <= mid && p2 <= hi) {
if (less(a[p1], a[p2])) {
assist[i++] = a[p1++];
} else {
assist[i++] = a[p2++];
}
}
while (p1 <= mid) {
assist[i++] = a[p1++];
}
while (p2 <= hi) {
assist[i++] = a[p2++];
}
for (int index = lo; index <= hi; index++) {
a[index] = assist[index];
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void main(String[] args) throws Exception {
Integer[] arr = {8, 4, 5, 7, 1, 3, 6, 2};
Merge.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
快速排序
package 快速排序;
import java.util.Arrays;
public class Quick {
public static void sort(Comparable[] a) {
int lo = 0;
int hi = a.length - 1;
sort(a, lo, hi);
}
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) {
return;
}
int partition = partition(a, lo, hi);
sort(a, lo, partition - 1);
sort(a, partition + 1, hi);
}
public static int partition(Comparable[] a, int lo, int hi) {
Comparable key = a[lo];
int left = lo;
int right = hi + 1;
while (true) {
while (less(key, a[--right])) {
if (right == lo) {
break;
}
}
while (less(a[++left], key)) {
if (left == hi) {
break;
}
}
if (left >= right) {
break;
} else {
exch(a, left, right);
}
}
exch(a, lo, right);
return right;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
public static void main(String[] args) throws Exception {
Integer[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 8};
Quick.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
|