Java Basics (16), advanced applications for arrays-bubble sort, select sort

Source: Internet
Author: User
Tags array length

Array sorting

Directory

One. Bubble sort

Two. Select sort

Three. Optimize selection sorting

one. Bubble sort

Example of an array element as "small to large"

Idea: Compare the nearest two elements in turn, place the maximum value at the end of the array, and then compare the remaining elements to the last of the remaining elements ... In this loop, the last element is sorted from small to large.

1.1. Before doing this, understand this: put the maximum value of the array at the end

Comparing element 1 and Element 2, if the value of element 1 is large, then the value of element 2 and element 1 is interchanged (at this point 2 is a large value), and the element 2 and element 3 are compared. .  (large values are retained).  .  Element 3 and Element 4 contrast.  .  . And so on, after comparing the elements of the complete part (as compared to the current element and the next element, so that the current element is the second-to-last element, you can complete the element comparison, that is, the "array length-1 times"), then the final element is the maximum value;

1  for  (int  i = 0; i < arr.length-1; I++ 2  //  3  if  (Arr[i] > Arr[i+1]) { 4  int  temp = Arr[i];  5  arr[i] = Arr[i+1];  6  arr[i+1] = temp;  7 }  

1.2. On the basis of "1.1", the remaining elements are sorted from small to cycle:

Using the "1.1" method, the remaining elements of the maximum value of the comparison (the number of times compared to "1.1" less once, can be compared to the remainder of the elements); Loop through this idea (once, the large value is placed after the "loop element"); When you loop to the second element, the first element is the minimum value. That is, "array length-1 times").

1         int[] arr = {1,3,5,1,2,7,9};2         //2, Loop once, put a large value behind the array; When looping to the second element, the first element is already the minimum value, so the loop "array length-1 times" can be. 3          for(intj = 0; J < Arr.length-1; J + +){    4              for(inti = 0; I < arr.length-1-J; i++) {5                 //1, if the current element is larger than the next element, then the two elements are interchanged6                 if(Arr[i] > arr[i+1]){    7                     inttemp =Arr[i];8Arr[i] = arr[i+1];9ARR[I+1] =temp;Ten                 } One             } A         } -          for(inti = 0; i < arr.length; i++) { -System.out.print (arr[i]+ ""); the}

two. Select the sorting Method

Example of an array element as "small to large"

Idea: Compare the first element to the following element, and if there is a value larger than the element, the two elements are interchanged (the first element is guaranteed to be the minimum value), and then the second element is compared to the subsequent element.  . . Loop in turn.

2.1. Before you do this, understand this: put the minimum value of the array first

Idea: Compare the first element, the element with which it is, and, if there is a value larger than it, swap the two elements (loop to the "array length-1 times" to complete the comparison of all elements).

         for (int i = 0; i < arr.length-1; i++) {            // If the first element is larger than the subsequent element, it is interchanged with its elements, and after the loop is complete, arr[0] is the minimum value of the array.             if(arr[0] > arr[i+1]) {                int temp = arr[0];                arr[0] = arr[i + 1];                 + 1] = temp;            }        }

2.2. On the basis of "2.1", the remaining elements are sorted from small to cycle:

After using the "2.1" (inner) loop, the second element is compared with all subsequent elements (because the second element starts, so the number of cycles is less than "2.1"), and the Thought (outer) loop (the number of times each cycle compares each other less than the last time)

Analysis 1: (outer) loop for the first time, compare with the first element and other elements; loop the second time, using the second element to compare with the other elements.  .    . Therefore, the number of (external) cycles "I" and the index of the element to be compared with "arr[i"

Analysis 2: (internal) cycle comparison of the number of times, each time than the last time, the number of external loops "I", in line with the inner loop of the initial value.

1         int[] arr = {1,3,5,1,2,7,9};2         3          for(intj = 0; J < Arr.length; J + +) {4             //2, due to each completion of the cycle, the next cycle times need to be less, the external loop value to the internal loop, can achieve this effect5              for(inti = j; i < arr.length-1; i++) {6                 //1. If the first element is larger than the following element, it is swapped with its elements, and after the loop is finished, Arr[j] is the smallest value within and after the element. 7                 if(Arr[j] > arr[i+1]){8                     inttemp =Arr[j];9ARR[J] = arr[i + 1];TenArr[i + 1] =temp; One                 } A             } -}

three. Optimized selection sorting Method

Example of an array element as "small to large"

Because the selection of the ordering method requires the comparison of elements with all subsequent elements, and the process of exchanging elements, if the length of the array is very long, then the element interchange needs more resources

Optimization ideas: Based on the alternative sorting method, set the variable index (the index representing the starting point comparison Element), and the variable value (which represents the value of the starting element), assigning the value and index of the element to the variable when there is an element that is smaller than its value (the resource cost is significantly lower than the element interchange) The index of the variable representing the minimum value is then compared to the index of the starting element, and if it is not the same, the starting element is interchanged with the minimum element (so that the order is completed by a smaller number of interchange cycles).

1          for(inti = 0; i < arr.length-1; i++) {2             intindex =i;3             intValue =Arr[i];4             //If there is a better element than the starting element value, assign the element's index and value to the start element variable. 5              for(intj = i + 1; J < Arr.length; J + +) {6                 if(Value >Arr[j]) {7Value =Arr[j];8index =J;9                 }Ten             } One             //if the index of the minimum value is different from the start index, the element with the minimum value is interchanged with the start element A             if(I! =index) { -                 inttemp =Arr[i]; -Arr[i] =Arr[index]; theArr[index] =temp; -             } -}

Java Basics (16), advanced applications for arrays-bubble sort, select sort

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.