排序算法有很多种,主要分为插入排序(直接插入排序、二分法插入排序)、交换排序(冒泡排序、快速排序)、选择排序(简单选择排序、堆排序)等,这里不说原理了,只贴代码。原理可以看严蔚敏的数据结构或者网上有人讲的原理很详细。给大家推荐一个链接点击打开链接
1.直接插入排序
package sortpackage;
import java.util.Arrays;
public class insertsort {
public void sort(int a[]){
for(int i=1;i<a.length;i++){
if(a[i]<a[i-1]){
int j,temp;
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
}
}
public static void main(String args[]){
insertsort object=new insertsort();
int a[]={3,2,6,5,8,9,0,2,1,23,12,45,32};
object.sort(a);
System.out.println(Arrays.toString(a));
}
}
2.二分法直接插入排序(借助了二分法查找位置)
package sortpackage;
import java.util.Arrays;
public class insertsortbybinarysearch {
public void binarysearchsort(int a[]) {
int low, high, middle, temp;
if (a != null) {
for (int i = 1; i < a.length; i++) {
temp = a[i];
low = 0;
high = i - 1;
while (low >= 0 && low <= high) {
middle = (low + high) / 2;
if (a[middle] > a[i]) {
high = middle - 1;
} else {
low = middle + 1;
}
}
for (int j = i - 1; j >= low; j--) {
a[j + 1] = a[j];
}
a[low] = temp;
}
}
}
public static void main(String args[]) {
insertsortbybinarysearch object = new insertsortbybinarysearch();
int a[] = { 3, 1, 6, 2, 8, 9, 6, 5, 4, 12, 45, 23, 66 };
object.binarysearchsort(a);
System.out.println(Arrays.toString(a));
}
}3.希尔排序package sortpackage;
import java.util.Arrays;
public class insertsortbybinarysearch {
public void binarysearchsort(int a[]) {
int low, high, middle, temp;
if (a != null) {
for (int i = 1; i < a.length; i++) {
temp = a[i];
low = 0;
high = i - 1;
while (low >= 0 && low <= high) {
middle = (low + high) / 2;
if (a[middle] > a[i]) {
high = middle - 1;
} else {
low = middle + 1;
}
}
for (int j = i - 1; j >= low; j--) {
a[j + 1] = a[j];
}
a[low] = temp;
}
}
}
public static void main(String args[]) {
insertsortbybinarysearch object = new insertsortbybinarysearch();
int a[] = { 3, 1, 6, 2, 8, 9, 6, 5, 4, 12, 45, 23, 66 };
object.binarysearchsort(a);
System.out.println(Arrays.toString(a));
}
}4.冒泡排序
package sortpackage;
import java.util.Arrays;
public class bubblesort {
public void sort(int a[]){
for(int i=0;i<a.length-1;i++){
boolean flag=true;
for(int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=false;
}
}
if(flag){
return;
}
}
}
public static void main(String[] args) {
int[] a={3,7,1,4,8,4,3,9,0,12,2};
//int[] b={1,2,3,4,8};
bubblesort object=new bubblesort();
object.sort(a);
System.out.println(Arrays.toString(a));
}
}
5.快速排序算法
package sortpackage;
import java.util.Arrays;
public class quicksort {
public void sort(int l, int r, int a[]) {//快速排序第一种方法,一个个的填坑。
if (l < r) {
int i = l, j = r, temp = a[i];
while (i < j) {
while (a[j] >= temp && i < j) {
j--;
}
if (i < j) {
a[i++] = a[j];
}
while (a[i] < temp && i < j) {
i++;
}
if (i < j) {
a[j--] = a[i];
}
}
a[i] = temp;
sort(i + 1, r, a); // 递归调用
sort(l, i - 1, a);
}
}
public void sort1(int l,int r,int a[]){//快排第二种方法,从两边分别找到小于与大于初始值的位置,将这两个元素交换。
if(l<0||r>a.length-1||a==null||l>r)
{
return;
}
int i=l,j=r;
while(i<j){
while(i<j&&a[j]>=a[l])
j--;
while(i<j&&a[i]<=a[l])
i++;
if(i<j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
int t=a[l];
a[l]=a[i];
a[i]=t;
sort1( l,i-1,a);
sort1( i+1,r,a);
}
public static void main(String[] args) {
int[] a = { 3, 7, 1, 4, 8, 4, 3, 9, 0, 12, 2 };
quicksort object = new quicksort();
object.sort1(0, a.length - 1, a);
System.out.println("快速排序算法");
System.out.println(Arrays.toString(a));
}
}
6.简单选择排序(算法如其名字,原理比较简单)package sortpackage;
import java.util.Arrays;
public class simpleselect {
public void sort(int a[]){
for(int i=0;i<a.length-1;i++)
{
int min=i;
for(int j=i+1;j<a.length;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
public static void main(String[] args) {
int[] a = { 3, 7, 1, 4, 8, 4, 3, 9, 0, 12, 2 };
simpleselect object = new simpleselect();
object.sort( a);
System.out.println(Arrays.toString(a));
}
}
原文地址:http://blog.csdn.net/woaishuoshihuo/article/details/44833373