【Daily Blue Bridge】14.13 Provincial Java Group Real Question "Sequence of Three Parts"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Sort by Three

There are many classic algorithms for general sorting, such as quick sorting, hill sorting, etc.

But in practical applications, there are often more or less special requirements. We don’t need to apply those classic algorithms. We can build better solutions based on actual conditions.

For example: sort and sort the numbers in an integer array:

The negative numbers are all to the left, the positive numbers are to the right, and 0 is in the middle. Pay attention to the characteristics of the problem: the negative and positive areas do not require order. You can use this feature to perform a linear scan to end the battle! ! !

The following program achieves this goal:

static void sort(int[] x) 
{
	int p = 0;
	int left = 0;
	int right = x.length - 1;
	
	while (p <= right) {
	    if (x[p] < 0) {
	        int t = x[p];
                x[p] = x[left];
	        x[left] = t;
			
	        left++;
	        p++;
	    }
	    else if (x[p] > 0) {
		int t = x[p];
		x[p] = x[right];
		x[right] = t;
				
		right--;
	    }else {
		_______________________
	}		
}
		
	}

If an array is given:

25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0

After sorting:

-3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25

 

Please analyze the code logic, and guess the code at the underline, and submit it through the web

Note: Only use the missing code as the answer, do not fill in extra codes, symbols or explanatory text!

Answer source code:

package 一三年省赛真题;

public class Year2013_Bt6 {

	static public void sort(int[] x) 
	{
		int p = 0;
		int left = 0;
		int right = x.length - 1;
		
		while (p <= right) {
			if (x[p] < 0) {
				int t = x[p];
				x[p] = x[left];
				x[left] = t;
				
				left++;
				p++;
			}
			else if (x[p] > 0) {
				int t = x[p];
				x[p] = x[right];
				x[right] = t;
				
				right--;
			}else {
//				___________________________
				p++;
			}		
		}
		
	}
	
	public static void main(String[] args) {
		int[] x = {25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0};
		sort(x);
		for (int i = 0; i < x.length; i++) {
			System.out.print(x[i] + ",");
		}
		
	}

}

 

 

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113050095