Array sorting cannot assign a random value to max. Java programming (basic) Learning Diary

Source: Internet
Author: User

Java programming (BASICS)

Content: Chapter 6 -- Array

The selectionsort method in the program list 6-8 was not read at the time. I typed the code myself and did not expect an error: max = 0.

Package java06;

Public class selectionsort {
Public static void main (string [] ARGs ){
Printarray (selectionsort (New Double [] {-2,-3,-1}); // test sentence
System. Out. println (); // empty row
Printarray (selectionsort (New Double [] {2, 1, 4 }));
}

 Public static double [] selectionsort (double [] list ){

For (INT I = List. Length-1; I> = 1; I --) {// a descending queue needs to be compared and arranged
// Double max = 0; // This is a bug,

// The output result is

///-3.0 0.0 0.0
// 1.0 2.0 4.0

// Values of Max and Min cannot be assigned with values such as 0. Three negative numbers cannot be compared with max (0. Therefore, you need to change it to a visible number in the number group. Now I choose list [0]. I don't know if it is appropriate ......

Double max = list [0]; // changed it to this (later I found that the original question in the book is actually this)
Int indexofmax = 0;
For (Int J = 0; j <= I; j ++) {// find the maximum value of the current row
If (max <list [J]) {
Max = list [J];
Indexofmax = J;
}
}
// Convert the array element with the maximum value and the last exchange value
List [indexofmax] = list [I];

List [I] = max;
}
}
Return list;
}
Public static void printarray (double [] list ){
For (INT I = 0; I <list. length; I ++)
System. Out. Print (list [I] + "");
}

}

At present, I can only get stuck with the basic knowledge all day long. I mean it is so bad now!

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.