JAVA Insert Sort Stability __ Part of ACM algorithm solution

Source: Internet
Author: User
package;

The public final class Demo {

	/* is sorted by weight, output hat color
	 * For example:	5 mice, weight of 15 30 5 9 30, the color of the hat in turn cl_red, Cl_blue, Cl_blue, cl_y Ellow, Cl_gray,
	 * Then sort the output of the hat color sequence should be for Cl_blue, Cl_yellow, cl_red, Cl_blue, Cl_gray.
		If there is no mouse, output null.
	input Parameters: Mouse Array */	
		
	public
	static mouse_color[] Sortmouse (mouse[] MOUSE)
	{
		if mouse==null| | MOUSE.LENGTH<=0) return null;
		Mouse_color[] Result=new mouse_color[mouse.length];
		Insert sort for
		(int i=1;i<mouse.length;i++)
		{
			int J;
			for (j=i-1;j>=0;j--)
				if (mouse[j].weight<=mouse[i].weight) break
					;
			Mouse nowmouse=new Mouse (mouse[i].weight,mouse[i].color);
			for (int k=i-1;k>j;k--)
			{
				mouse[k+1].weight=mouse[k].weight;
				Mouse[k+1].color=mouse[k].color;
			}
			Mouse[j+1]=nowmouse;
		}
		for (int i=0;i<mouse.length;i++)
		{
			Result[i]=mouse[i].color
		}
		return result;
	}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.