Once a headache bubble, now look at the __java

Source: Internet
Author: User

For each cycle, the next two elements to compare the size, if the conditions to meet the exchange, the ultimate realization of the largest element to throw to the last side.

Prepared for 2 weeks of Java, C, C + +, data structure and algorithms, the written test was only a test overload and a single example plus 3 times shaking hands, this is an insult to me, more painful is actually brushed me down. Even a bubble-not-test recruitment is an incomplete recruitment.

public class Bubblesort {private int[] ParaL;
		Public Bubblesort () {} is public void Show () {System.out.println ("The sorted record is:");
		for (int para:paral) {System.out.print (para+ "");
	} System.out.println ();
		public void Bsort () {Boolean flagchange = false; int len = paral.length;//whether to Exchange flags for (int i = 0; i < len-1 i++) {////Total traverse n-1 times Flagchange = false;//No exchange for (int j = 0; J < Len-i-1;
					J + +) {if (paral[j]>paral[j+1]) {Flagchange = true;
					PARAL[J] = Paral[j] + paral[j+1];//two variable exchange paral[j+1] = Paral[j]-paral[j+1];
				PARAL[J] = paral[j]-paral[j+1];
			} if (!flagchange) {//If no break is exchanged;
		}} public static void Main (string[] args) {int[] ParaL = {9,8,7,6,5,4,3,2,1};
		Bubblesort qs = new Bubblesort ();
		Qs.setparal (ParaL);
		Qs.bsort ();
	Qs.show ();
	Int[] Getparal () {return paraL;
	} public void Setparal (int[] paraL) {this.paral = ParaL; }
}

20160802 write again.

/**
 * Classname:bubblesort 
 * @Description: Bubbling (traversing the previous n-i element, moving the largest element to the n-i position each time, which is roughly what it means) 
 * @author Panyk
 * @date August 2, 2016 * * Public
class Bubblesort {public
	int[] data;
	public void sort () {
		int temp;
		Boolean change;
		Show ();
		for (int i=0; i<data.length-1;i++) {Change
			= false;
			for (int j=0; j<data.length-1-I; j + +) {
				if (Data[j] > Data[j+1]) {
					temp = data[j+1];
					DATA[J+1] = data[j];
					DATA[J] = temp;
					Change = true;
				}
			}
			Show ();
			if (!change) {break
				;
	}}} private void Show () {for
		(int temp:data) {
			System.out.print ("  " + temp);
		}
		System.out.println ();
	}
	public static void Main (string[] args) {
		bubblesort bs = new Bubblesort ();
		int[] data = new Int[]{1,  3,  5,  2,  4,  4,  1,  2};
		Bs.data = data;
		Bs.sort ();
	}



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.