Java data structures and algorithms the second edition after the lesson third chapter

Source: Internet
Author: User

In Exercise 1, theBubblesort.java program (listing 3.1) and the Bubblesort Special applet, the in index variable is moved from left to right until the largest data item is found and moved to the right out variable. Modify the Bubblesort () method so that it becomes two-way moving. In this way, the in index moves the largest data item from left to right as before, and when it reaches the out variable position, it turns around and moves the smallest item of data from right to left. Requires two external index variables, one on the right (the previous out variable), and the other on the left.

public static void Bubblesort (int nelem,int [] source) {
int leftout=0,rightout=nelem-1,in;
while (Leftout<=rightout) {
for (in = leftout;in<rightout;in++) {
if (source[in]>source[in+1])
Swap (source,in,in+1);
}
for (in=rightout-1;in>leftout;in--) {
if (Source[in]<source[in-1])
Swap (source, in, in-1);
}
++leftout;
--rightout;
for (int i=0;i<nelem;i++)
System.out.print (source[i]+ ",");
System.out.println ("");
}

Exercise 3, add a method named Nodups () in the Insertsort.java program (listing 3.3), which has been o (N2), at least in the case of a lot of duplicate data items. In the algorithm that is designed, no matter how many

public static int[] Nodub (int[] source,int nelem) {
int[] result = new Int[nelem];
int j=0;      

for (int i=0;i<nelem-1;i++) {
if (source[i]!=source[i+1]) {
Result[j] = source[i];
++j;
}                                      //If two elements are equal, nothing is done, just wait for the next step to point the index to the next, that is, the element being inserted is equal to the last element in the element
}
Result[j]=source[nelem-1];        //The last element is not processed in the for loop, whether it is equal to or unequal to the previous number should be inserted.
return result;
}

Exercise 4, there is also a simple sorting algorithm is the odd-even sort. The idea is to repeat the two-pass scan in the array. The first scan selects all data item pairs, A[j] and a[j+1],j are odd (j=1,3,5,......). If the values of their keywords are reversed in order, they are swapped. The second scan does the same for all even-numbered data items (j=2,4,6,...... )。 Repeat this two -pass order until the array is all in order. Replace the Bubblesort () method in the Bubblesort.java program (listing 3.1) with the Oddevensort () method . Make sure it runs in a sort of different amount of data, and that you need to figure out the number of two scans. Parity Ordering is actually useful in multiprocessor environments where the processor can process each odd pair at the same time, and then handle even pairs at the same time. Because the odd pairs are independent of each other, each pair can be compared and exchanged with different processors. This can be sorted infrequently and quickly.

public static void Oddevensort (int[] Source,int Nelem) {

Boolean changed = TRUE;

while (changed) {

Changed = FALSE;

for (int i=0;i<nelem;i=i+2) {

if (A[i] > a[i+2]) {

Swap (i,i+2);

changed = TRUE;

}

}

for (int i=1;i<nelem;i=i+2) {

if (A[i] > a[i+2]) {

Swap (i,i+2);

changed = TRUE;

}

}

}

}

exercise 5, modify the Insertionsort () method in the Insertsort.java program (listing 3.3) so that it can be computed sorted over   Use this program to measure the number of copies and comparisons of various numbers of reverse order data. Did the result satisfy O (N2)? Is it the same as the data that has been basically  

public static void Insertionsort (int[] Source,int Nelem) {
int out,in;
int compare=0,copy=0,temp;
for (out=1;out<nelem;out++) {
temp = Source[out];
for (in = out;in>0;in--) {
if (source[in-1]>temp) {
Source[in] = source[in-1];
++copy;
++compare;
}
else{
++compare;
Break
}
}
Source[in] = temp;
}
for (int i=0;i<source.length;i++)
System.out.print (source[i]+ ",");
System.out.println ("");
System.out.println ("Compaer =" +compare+ "copy =" +copy);
}

Java data structures and algorithms the second edition after the lesson third chapter

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.