Given an array of integers, rearrange the array so that the left of the array is odd and the right is an even number. __ algorithm

Source: Internet
Author: User
Given an array of integers, rearrange the array so that the left of the array is odd and the right is an even number.

Requirements: Space complexity O (1), Time complexity of O (n).

Import java.util.ArrayList;
Import java.util.List;

public class Exchangeoddeven {public
	static void solution (List<integer> list) {
		if (null = = list) {
			return;
		}
		int = =; int Q=list.size ()-1;
		while (P<Q) {while
			(List.get (p)%2==1) {
				++p;
			}
			while (List.get (q)%2==0) {
				--q;
			}
			int Tmp=list.get (p);
			List.set (P, List.get (q));
			List.set (q, TMP);
		}
	public static void Main (string[] args) {
		list<integer> List = new arraylist<integer> ();
		List.add (1);
		List.add (4);
		List.add (7);
		List.add (a);
		Solution (list);
		SYSTEM.OUT.PRINTLN (list);
		
	}


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.