最近,编写Java面试宝典的程序,看到排序算法一章发现书中有的程序存在一些错误,现在将所有涉及到的排序算法Java实现如下:
package test1;
/*public class PaiXu {
*//**
* 1.冒泡排序
*//*
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i;
bubbleSort(a);
for(i=0;i<10;i++)
{
System.out.printf("%d",a[i]);
}
}
private static void bubbleSort(int[] a) {
for(int i= a.length - 1;i>0;i--){
for(int j = 0;j<i;j++){
if(a[j]>a[j+1]){
swap(a,j,j+1);
}
}
}
}
private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
*/
/*public class PaiXu {
*//**
* 2.选择排序
*//*
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i;
selectSort(a);
for(i=0;i<10;i++)
{
System.out.printf("%d",a[i]);
}
}
public static void selectSort(int[] a) {
for(int i = 0;i<a.length;i++){
for(int j= i+1;j<a.length;j++){
if(a[j]>a[i]){
swap(a,i,j);
}
}
}
}
private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}*/
/*public class PaiXu {
*//**
* 3.插入排序
*//*
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i;
insertSort(a);
for(i=0;i<10;i++)
{
System.out.printf("%d",a[i]);
}
}
public static void insertSort(int[] a) {
for(int i = 0;i<a.length;i++){
for(int j= i;(j>0)&&(a[j]<a[j-1]);j--){
swap(a,j,j-1);
}
}
}
private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
*/
/*public class PaiXu {
*//**
* 4.Shell排序
*//*
public static int[] a = {4,2,1,6,3,6,0,-5,1,1};
public static int index = a.length;
public static void main(String[] args) {
int i; //循环计数变量
System.out.print("排序前:");
for( i=0;i<index ;i++)
{
System.out.printf("%3s",a[i]);
}
System.out.println("");
shellSort(index - 1);
//排序后的结果
System.out.println("排序后:");
for(i=0;i<index;i++)
{
System.out.printf("%3s",a[i]);
}
System.out.println("");
}
public static void shellSort(int i) {
int j,k; //循环计数变量
int temp; //暂存变量
boolean change; //数据是否改变
int dataLength; //分割集合的间隔长度
int pointer; //进行处理的位置
dataLength = (int)index/2; //初始集合间隔长度
while(dataLength != 0){
//对各个集合进行处理
for(j = dataLength;j<index;j++){
change = false;
temp = a[j]; //暂存data[j]的值,待交换时使用
pointer = j - dataLength; //计算进行处理的位置
//进行集合内数值的比较与交换
while(temp < a[pointer] && pointer >=0 && pointer <= index){
swap(a,pointer,pointer + dataLength);
// a[pointer + dataLength] = a[pointer];
//计算下一个欲进行处理的位置
pointer = pointer - dataLength;
change =true;
if(pointer < 0||pointer >index){
break;
}
//最后的数值交换
a[pointer + dataLength] = temp;
if(change){
//打印目前排序的结果
System.out.print("排序中:");
for(k=0;k<index;k++){
System.out.printf("%3s", a[k]);
}
System.out.println("");
}
}
}
dataLength = dataLength / 2; //计算下次分割的间隔长度
}
}
private static void swap(int[] a2, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
*/
/*public class PaiXu {
*//**
* 5.二分排序
* 排序原理:其实也属于插入法类型,分已排序和未排序部分.然后将未排序
部分元素逐个排序插入,但是插入的过程不同,需要每次求一个
中间位置,和中间位置元素比较大小,然后根据大小情况,将高位
左移或者将低位右移,再求中间元素比较,直到找到合适位置后;
将其后已排序元素全部后移一位,再插入该匀速即可.此方法中
每步打印的high和low关系应为high+1=low
*//*
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i,j;
int low,high,mid;
int temp;
for(i=1;i <10;i++){
temp=a[i];
low = 0;
high= i-1;
while(low<=high){
mid = (low+high)/2;
if(a[mid]>temp)
high = mid -1;
else
low = mid+1;
}
for(j=i-1;j>high;j--){
a[j+1] = a[j];
}
a[high+1] = temp;
}
for(i=0;i<10;i++){
System.out.printf("%d ", a[i]);
}
}
}
*/
/*public class PaiXu {
*//**
* 6.快速排序
*
*//*
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i;
qsort_asc(a,0,a.length-1);
for(i=0;i<10;i++){
System.out.printf("%d",a[i]);
}
}
public static void qsort_asc(int[] a, int low, int high) {
int i,j,x;
if(low<high){
i = low;
j = high;
x = a[i];
while(i<j){
while(i<j && a[j]>x){
j--;
}
if(i<j){
swap(a,i,j);
i++;
}
while(i<j && a[i]<x){
i++;
}
if(i<j){
swap(a,i,j);
j--;
}
}
a[i] = x;
qsort_asc(a,low,i-1);
qsort_asc(a,i+1,high);
}
}
private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}*/
public class PaiXu {
/**
* 7.归并排序
*
*/
public static void main(String[] args) {
int[] a = {4,2,1,6,3,6,0,-5,1,1};
int i;
mergeSort(a,0,a.length-1);
for(i=0;i<10;i++){
System.out.printf("%d",a[i]);
}
}
public static void mergeSort(int[] a, int start, int end) {
if(start < end)
{
int mid = (start+end)/2;
//两路归并
mergeSort(a,start,mid);
mergeSort(a,mid+1,end);
merge(a,start,mid,mid+1,end);
//多路归并
/*int mid = (start+end)/4;
mergeSort(a,start,1*mid);
mergeSort(a,1*mid+1,2*mid);
mergeSort(a,2*mid+1,3*mid);
mergeSort(a,3*mid+1,end);
merge(a,start,1*mid,1*mid+1,2*mid);
merge(a,2*mid+1,3*mid,3*mid+1,end);
merge(a,start,2*mid,2*mid+1,end);*/
}
}
public static void merge(int[] a, int start1, int end1, int start2, int end2) {
int i,j; //i,j分别为表1和表2的游标
i = start1;
j = start2;
int k=0;
int [] temp = new int[end2-start1+1]; //建立一个临时长度为两个子列表长度之和的数组
while(i<=end1&&j<=end2){
if(a[i]>a[j]){
temp[k++] = a[j++];
}
else
{
temp[k++] = a[i++];
}
}
//把剩下的元素依次放入临时数组中
while(i<=end1)
temp[k++] = a[i++];
while(j<=end2)
temp[k++] = a[j++];
k=start1;
for(int e:temp){ //把临时数组元素赋值给原数组
a[k++] = e;
}
}
}