The first season of Java Arrays Class prequel (sorting case with two points to find the attention problem)

Source: Internet
Author: User

According to the sorting algorithm, some small cases can be solved. Examples are as follows:

/* * Sort the characters in the string. * Example: "DACGEBF" * Result: "ABCDEFG" *  * Analysis: * A: Define a String * B: Convert the string to a character array * C: Sort the character array * D: Turn the sorted character array into string * E: Output the last string */publ IC class Arraytest {public static void main (string[] args) {//define string s = "DACGEBF";//convert string to character array char[] CHS = s.to Chararray ();//Converts this string to a new character array. Method of the String class public char[] ToCharArray ()//The character array is sorted bubblesort (CHS);//Custom Sort method. This is defined as the bubbling algorithm//converting the sorted character array into a string, and valueOf () converts any type to a string. string result = String.valueof (CHS);//method of the String class: public static string ValueOf (char[] data) returns: a newly assigned string// Outputs the last string System.out.println ("Result:" + result);} Bubble sort public static void Bubblesort (char[] CHS) {//bubbling method, the value of the maximum index does not have to be compared with the for (int x = 0; x < chs.length-1; + +) {for (i NT y = 0; Y < chs.length-1-X; y++) {if (Chs[y] > chs[y + 1]) {//front greater than after, swap, always make the back greater than the previous. Char temp = chs[y];//Interchange Chs[y] = chs[y + 1];chs[y + 1] = temp;}}}}

A two-point lookup exists one of the considerations.

A binary lookup is only found in an ordered array. If the given is unordered, it is not possible to use two-point lookups. Here's an example of where the problem is.


public class ArrayDemo2 {public static void main (string[] args) {//definition array int[] arr = {24, 69, 80, 57, 13};//first sort Bubblesor T (arr);//after looking for int index = GetIndex (arr, 80); System.out.println ("index:" + index);} Bubble sort code public static void Bubblesort (int[] arr) {for (int x = 0; x < arr.length-1; + +) {//times, several rounds for (int y = 0; y < arr.length-1-X; y++) {//if (Arr[y] > arr[y + 1]) {//22 compare, put the small number to the front, put the large end in the last int temp = Arr[y];arr[y] = arr[y + 1];arr[y + 1] = temp;} }}}//binary find public static int getindex (int[] arr, int value) {//define maximum index, minimum index int max = Arr.length-1;int min = 0;//Calculate Intermediate index i NT MID = (max + min)/2;//compares the value of the intermediate index with the value to find while (Arr[mid]! = value) {if (Arr[mid] > value) {//Big Max = mid-1;//Left Edge Lookup} else if (Arr[mid] < value) {//small min = mid + 1;//to the right to find}//join to determine if (Min > Max) {return-1;} Mid = (max + min)/2;} return mid;}}

Output Print Index index=4


80 the index in the array is 80, and here it is 4. Obviously this is a problem. The problem is that the index position of the array is changed after sorting. How to solve it?


Use the basic Find method:

Here's just a basic way to find out:

public static int GetIndex (int[] arr,int value) {int index = -1;for (int x=0; x<arr.length; x + +) {//Iterate through the array, comparing if (arr[x] = = value) {index = X;break;}} return index;}

In this way, the problem is solved.



Look at the title is the prequel of the Arrays class. The next article, formally into the arrays class, find and sort, and so on, will become very, very simple.



So far, personal blog posts have officially reached 100 articles. I am very happy, I hope my article, can improve their own at the same time, can help more people. At present, the first quarter of Java is about One-fourth, and the second quarter will be a topic in the form of each focus and some source code analysis to describe. Hope to see the peer of mutual attention, leaving footprints, discuss together!

The first season of Java Arrays Class prequel (sorting case with two points to find the attention problem)

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.