I genius official free Tutorial 28: Selection Sorting and bubbling sorting of the Java sorting algorithm

Source: Internet
Author: User

Select Sort

Step one, select the data on the starting position (subscript) in a set of data, and compare the data on the subsequent positions (subscript), if the data on the starting position (subscript) is large (ascending) or small (descending), the data in the two locations is exchanged; So after a round of comparisons, The data at the starting position is the smallest or largest.
Step two, select the data from the second position again, and compare the data at each location. With this repetition, the data can be sorted.

Example:package algorithm.sort;/** *  demo selection sorting algorithm  *  @author   Genius Federation  -  Yukun  */ Public class selectionsortdemo {public static void main (String[] args)  {//Create two arrays int[] arr1 = { 3, 5, 1, 4, 2 };/* *  Select sort  *  as the name implies, from the array in order to select from one of the orders  *  ascending sort: *  The first step to take out the value of the array subscript 0, and each subsequent value comparison  *  If the small label is 0 value is greater than the following value, then two values are exchanged, the continuation and the subsequent ratio of  *  will be guaranteed that the position of the subscript 0 is kept to a minimum value  *  in the removal of the value labeled 1 and the subsequent value of a comparison, and so on  */ for  (int i = 0; i < arr1.length - 1; i++)  {// Take the second-to-last value, there is no other value at the end of the/* * , there is no need to compare the inner loop of the next to each value and the outer loop to get the value comparison  *  so the outer loop to remove the value labeled 0, For the first time, you should take the value labeled 1 position  *  the outer loop to remove the value labeled 1, where the first time to take the value labeled 2 position  *  and so on, to get J initialization should be equal to i+1 */for  ( int j = i+1; j < arr1.length; j++)  {//Determine the size if (arr1[i] > &NBSP;ARR1[J]) {//NOTE: The values in the array are exchanged INT&NBSP;TEMP&NBSP;=&NBsp;arr1[i];arr1[i] = arr1[j];arr1[j] = temp;}}} SYSTEM.OUT.PRINTLN ("---  select sort after sorting  ---");for  (int i = 0; i <  arr1.length; i++)  {system.out.print (arr1[i] +  "  ");}} Run Result:---  select sort after sorting  ---1  2  3  4  5


Bubble Sort

Step One: Select the data from the starting position (subscript) in a set of data, compare it to the data at the second position, and if the data on the starting position (subscript) is large (ascending) or small (descending), the data in the two locations is exchanged, then the data at the second position is compared with the data in the third position. ; Third and fourth ... Until the last one, so that a comparison is completed, so that the data at the last position is the largest or smallest
Step two: The data is still selected from the starting position, and the data in the second position is compared to the second to the penultimate data;
And so on, until only the first and second data are compared, and the comparison is completed and finished.

Example:package algorithm.sort;/** *  demo bubble sort algorithm  *  @author   Genius Federation  -  Yukun  */ Public class bubblesortdemo {public static void main (String[] args)  {/ /Create two arrays int[] arr1 = { 3, 5, 1, 4, 2 };/* *  bubble sort  *   As the name implies, like bubbling, the greater the greater  *  ascending sort: *  The first step to take the value of the array subscript 0, and the next adjacent value comparison  *  if the following value is small, Exchange two values (equivalent to one position up)  *  after the interchange continues to compare with this large value and subsequent values  *  and so on, after the first comparison completes, the maximum value is at the last subscript position  */for  ( int i = arr1.length - 1; i > 0 ; i--)  {// The function of the outer loop is to identify the end position of the bubble, and the end position is to move forward/* *  each time from the first value to the top of the bubble  *  know the end position of the outer loop identification  */for  (int  j = 0; j < arr1.length - 1; j++)  {//Determine the size of the adjacent two values if (Arr1[j]  > arr1[j + 1]) {//NOTE: The values in the array are exchanged int temp = arr1[j];arr1[j] =  Arr1[j + 1];arr1[j + 1] = temp;}}} SYSTEM.OUT.PRINTLN ("---  bubble sort after sorting  ---");for  (int i = 0; i <  arr1.length; i++)  {system.out.print (arr1[i] +  "  ");}} Run Result:---  bubble sort after sorting  ---1  2  3  4  5

There are a lot of sorting algorithms, for beginners, at least at least to understand and skillfully apply one of them.












This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius official free Tutorial 28: Selection Sorting and bubbling sorting of the Java sorting algorithm

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.