package sort;
public class SortTest {
public static void bubleSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length-1;i++)
{
for(int j=0;j<length-i-1;j++)
{
if(a[j]<a[j+1])
{
a[j]=a[j]^a[j+1];
a[j+1]=a[j]^a[j+1];
a[j]=a[j]^a[j+1];
}
}
out(a);
}
}
public static void selectSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length;i++)
{
for(int j=i+1;j<length;j++)
{
if(a[j]>a[i])
{
a[j]=a[j]^a[i];
a[i]=a[j]^a[i];
a[j]=a[j]^a[i];
}
}
out(a);
}
}
public static void insertSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length;i++)
{
int j=0;
while(a[i]>a[j]&&j<i)
{
j++;
}
int temp=a[i];
for(int n=i;n>j;n--)
{
a[n]=a[n-1];
}
a[j]=temp;
out(a);
}
}
public static void quick(int[] a,int s,int e)
{
int x=s;
if(s<e)
{
int point=a[s];
int m=s,n=e;
while(m<n)
{
while(m<n&&a[n]>=point)
{
n--;
}
if(m<n)
{
a[m++]=a[n];
}
while(m<n&&a[m]<=point)
{
m++;
}
if(m<n)
{
a[n--]=a[m];
}
}
a[m]=point;
out(a,s,e);
quick(a,s,m-1);
quick(a,m+1,e);
}
}
/***************************heap sort*********************************/
public static void heapJust(int[] a,int i,int len)
{
int left=2*i+1;
int right=2*i+2;
int largest=i;
while(left<len||right<len)
{
if(left<len&&a[largest]<a[left])
{
largest=left;
}
if(right<len&&a[largest]<a[right])
{
largest=right;
}
if(largest!=i)
{
a[largest]=a[largest]^a[i];
a[i]=a[largest]^a[i];
a[largest]=a[largest]^a[i];
i=largest;
left=2*i+1;
right=2*i+2;
}
else
{
break;
}
}
}
public static void buildHeap(int a[],int len)
{
int temp=len/2-1;
for(int i=temp;i>=0;i--)
{
heapJust(a,i,len);
}
}
public static void heapSort(int a[])
{
int len=a.length;
buildHeap(a,len);
out(a);
while(len>1)
{
swap(a,0,len-1);
heapJust(a,0,--len);
}
}
/***************************heap sort end*********************************/
public static void mergeSort(int[] a,int s,int e)
{
if(s==e)
{
return;
}
int mid=(s+e)/2;
mergeSort(a,s,mid);
mergeSort(a,mid+1,e);
int[] temp=new int[e-s+1];
int index=0;
int i=s,j=mid+1;
while(i<=mid&&j<=e)
{
if(a[i]<a[j])
{
temp[index++]=a[i++];
}
else
{
temp[index++]=a[j++];
}
}
while(i<=mid)
{
temp[index++]=a[i++];
}
while(j<=e)
{
temp[index++]=a[j++];
}
for(int ii=0;ii<temp.length;ii++)
{
a[s++]=temp[ii];
}
}
public static void out(int a[])
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void out(int a[],int s,int e)
{
for(int i=s;i<e;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void swap(int[] a,int i,int j)
{
a[i]=a[i]^a[j];
a[j]=a[i]^a[j];
a[i]=a[i]^a[j];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] b={3,5,1};
int[] a={9,11,23,0,33,-23,143,234,654,4322};
out(a);
//bubleSort(a);
System.out.println();
//selectSort(a);
//insertSort(a);
//quick(a,0,a.length-1);
// heapSort(a);
mergeSort(a,0,a.length-1);
out(a);
}
}
public class SortTest {
public static void bubleSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length-1;i++)
{
for(int j=0;j<length-i-1;j++)
{
if(a[j]<a[j+1])
{
a[j]=a[j]^a[j+1];
a[j+1]=a[j]^a[j+1];
a[j]=a[j]^a[j+1];
}
}
out(a);
}
}
public static void selectSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length;i++)
{
for(int j=i+1;j<length;j++)
{
if(a[j]>a[i])
{
a[j]=a[j]^a[i];
a[i]=a[j]^a[i];
a[j]=a[j]^a[i];
}
}
out(a);
}
}
public static void insertSort(int[] a)
{
if(a==null)
{
return;
}
int length=a.length;
for(int i=0;i<length;i++)
{
int j=0;
while(a[i]>a[j]&&j<i)
{
j++;
}
int temp=a[i];
for(int n=i;n>j;n--)
{
a[n]=a[n-1];
}
a[j]=temp;
out(a);
}
}
public static void quick(int[] a,int s,int e)
{
int x=s;
if(s<e)
{
int point=a[s];
int m=s,n=e;
while(m<n)
{
while(m<n&&a[n]>=point)
{
n--;
}
if(m<n)
{
a[m++]=a[n];
}
while(m<n&&a[m]<=point)
{
m++;
}
if(m<n)
{
a[n--]=a[m];
}
}
a[m]=point;
out(a,s,e);
quick(a,s,m-1);
quick(a,m+1,e);
}
}
/***************************heap sort*********************************/
public static void heapJust(int[] a,int i,int len)
{
int left=2*i+1;
int right=2*i+2;
int largest=i;
while(left<len||right<len)
{
if(left<len&&a[largest]<a[left])
{
largest=left;
}
if(right<len&&a[largest]<a[right])
{
largest=right;
}
if(largest!=i)
{
a[largest]=a[largest]^a[i];
a[i]=a[largest]^a[i];
a[largest]=a[largest]^a[i];
i=largest;
left=2*i+1;
right=2*i+2;
}
else
{
break;
}
}
}
public static void buildHeap(int a[],int len)
{
int temp=len/2-1;
for(int i=temp;i>=0;i--)
{
heapJust(a,i,len);
}
}
public static void heapSort(int a[])
{
int len=a.length;
buildHeap(a,len);
out(a);
while(len>1)
{
swap(a,0,len-1);
heapJust(a,0,--len);
}
}
/***************************heap sort end*********************************/
public static void mergeSort(int[] a,int s,int e)
{
if(s==e)
{
return;
}
int mid=(s+e)/2;
mergeSort(a,s,mid);
mergeSort(a,mid+1,e);
int[] temp=new int[e-s+1];
int index=0;
int i=s,j=mid+1;
while(i<=mid&&j<=e)
{
if(a[i]<a[j])
{
temp[index++]=a[i++];
}
else
{
temp[index++]=a[j++];
}
}
while(i<=mid)
{
temp[index++]=a[i++];
}
while(j<=e)
{
temp[index++]=a[j++];
}
for(int ii=0;ii<temp.length;ii++)
{
a[s++]=temp[ii];
}
}
public static void out(int a[])
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void out(int a[],int s,int e)
{
for(int i=s;i<e;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void swap(int[] a,int i,int j)
{
a[i]=a[i]^a[j];
a[j]=a[i]^a[j];
a[i]=a[i]^a[j];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] b={3,5,1};
int[] a={9,11,23,0,33,-23,143,234,654,4322};
out(a);
//bubleSort(a);
System.out.println();
//selectSort(a);
//insertSort(a);
//quick(a,0,a.length-1);
// heapSort(a);
mergeSort(a,0,a.length-1);
out(a);
}
}