public class test {
public void mergesort(int[] nums,int low,int high)
{
int[] temp=new int[nums.length];
if(low<high)
{
//System.out.println("!!!!!!!!!!!!!");
int mid=(low+high)/2;
mergesort(nums,low,mid);
mergesort(nums,mid+1,high);
merge(nums,low,high,mid,temp);
}
}
public void merge(int[] nums, int low,int high,int mid,int []temp)
{
for(int i=low;i<=high;i++)
{
temp[i]=nums[i];
}
int k=low;int i=low;int j=mid+1;
while(i<=mid&&j<=high)
{
if(temp[i]<=temp[j])
{
nums[k++]=temp[i++];
}
else
{
nums[k++]=temp[j++];
}
}
while(i<=mid)nums[k++]=temp[i++];
while(j<=high)nums[k++]=temp[j++];
}
public void selesort(int [] nums)
{
sort(nums,0,nums.length-1);
}
public void sort(int[] nums,int low,int high)
{
int p=partition(nums,low,high);
if(low<high)
{
sort(nums,low,p-1);
sort(nums,p+1,high);
}
}
public int partition(int[] nums,int low,int high)
{
int privot=nums[low];
while(low<high)
{
while(low<high&&nums[high]>=privot) --high;
if(nums[high]<privot) nums[low++]=nums[high];
while(low<high&&nums[low]<=privot) ++low;
if(nums[low]>privot) nums[high--]=nums[low];
}
nums[low]=privot;
return low;
}
public static void main(String[] args) {
test t=new test();
int[] nms=new int[]{4,2,1,6,3,6,0,-5,1,1};
t.mergesort(nms,0,9);
for(int i=0;i<nms.length;i++)
{
System.out.println(nms[i]);
}
}
}