On Java bidirectional bubble sort algorithm _java

Source: Internet
Author: User
Tags rand

An example of an integer ascending order is a brief description of the process of bidirectional bubbling ordering: Start by moving the largest number to the last, and then, in turn, move the smallest number forward from the back to the top of the array, which is the first round, and then repeat the process, and eventually the entire array will be arranged from small to large. The bidirectional bubble sort is slightly better than the traditional bubble sort, since both ends of the array are sorted in a bidirectional sort, we only need to deal with the middle part of the array, whereas a one-way, traditional bubble sort has only the tail elements in order, and each round of processing needs to be processed from the beginning to the previous element of the ordered element. Although it has some improvement in efficiency, it does not greatly improve the efficiency of its sorting, which is determined by the basic process of bubble sequencing. Based on this, the following code can be used to sort odd even numbers

Bidirectional bubble Sort Source code:

Copy Code code as follows:

Package com.zc.manythread;
Import Java.util.Random;
/**
* Bidirectional bubble sort
* @author Me Yes
*
*/
public class Bbsort {
Bidirectional bubbling algorithm, which greatly reduces the number of cyclic sorting
Public int[] Sort (int[] a) throws exception{
Int J;
int limit=a.length;
int st=-1;
while (St<limit) {
The ST and limit must be assigned a value, otherwise if the array is ordered at the beginning
st++;
limit--;
Boolean swapped=false;
The first loop places the maximum value at the end
for (j = St; J < Limit; J + +) {
if (A[j]>a[j+1]) {
int T=A[J];
A[J]=A[J+1];
a[j+1]=t;
Swapped=true;
}
}
if (!swapped) {
return A;
}else {
Swapped=false;
The second loop places the smallest value at the beginning
for (j = limit;--j>=st;) {
if (A[j]>a[j+1]) {
int T=A[J];
A[J]=A[J+1];
a[j+1]=t;
Swapped=true;
}
}
if (!swapped) {
return A;
}
}
}
return A;
}
private static int[] CreateDate (int count) {
/**
* No repeat array
*/
Int[] Data=new Int[count];
Random rand = new Random ();
boolean[] bool = new BOOLEAN[100];
int num = 0;
for (int i = 0; i < count; i++) {
do {
If the resulting number is the same as continuing the loop
num = rand.nextint (100);
while (Bool[num]);
Bool[num] = true;
/* LIST.ADD (num); *///list list
Data[i]=num;
}
return data;
}
public static void Main (string[] args) {
final int count=10;
Int[] Data=createdate (count);
for (int n:data) {
System.out.print (n+ "T");
}
System.out.println ();
Bsrot bsrot=new Bsrot (data);
try {
Int[] A=bsrot.sort (data);
for (int n:a) {
System.out.print (n+ "T");
}
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Run Result:

Related Article

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.