Array positive negative order __ array positive Negative order

Source: Internet
Author: User

Given an integer array, all negative values are transferred to the left of the array, and all positive values are transferred to the right. For example: 1,-2,-4, 5, 9, 3,-8, 6, after adjustment can be-2,-4, 3,-8, 1, 5, 9, 6.

The specific implementation code is as follows:

Package com.threeTop.www;

/***
 * Array positive negative order
 * @author WJGS * * * */Public
class ArraySort {  
     /**
      * Draw on the idea of a quick sort, sort
      * @ param Array
      *
	/public static void Sort (int [] array)
	{
		int i=0;
		int j=array.length-1;
		
		while (I!=j)
		{while
			(i<j&&array[i]<0)
			{
				i++
			}
			while (i<j&&array[j]>0)
			{
				j--
			}
			Exchange if
			(i<j)
			{
				int temp=array[i]
				If the above condition is not satisfied; ARRAY[I]=ARRAY[J];
				Array[j]=temp
			}
		}
		
		System.out.println ("Draw upon the sort of quick sort of results as follows:");
		
		for (int k=0;k<array.length;k++)
		{
			System.out.print (array[k]+ ",");
		}
        		
		
		
	}
	public static void Main (string[] args) {
	   
         int []array={1,-2,-4,5,9,-3,-8,6};
         Arraysort.sort (array);
	}

}

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.