The principle and difference of bubble sort, direct selection sorting, reverse ordering in array

Source: Internet
Author: User
Tags array length

Bubble sort

Bubble sorting is one of the most commonly used sorting algorithms, it is sorted by comparing the values of adjacent elements, if the conditions are met to exchange element values, the smaller elements moved to the front of the array, the larger elements moved to the back of the array, because similar to the bubble in the water upward action, so called bubble sort.

The bubbling sort has a double loop, the outer loop is used to control the number of loops, the inner loop is used to compare the size of the values, so the number of outer loops = array Length-1, the number of inner loops = Each compares the array length-1, and the inner loop is affected by the outer loop.

The bubbling Sort implementation code is as follows:

 Public classBubblesort { Public Static voidMain (string[] args) {//creating a one-dimensional array        intArr[] = {4, -,1, the,3, the}; //creating an Instance objectBubblesort Bubblesort =NewBubblesort (); //call the Sort method, the object being sorted is the ARR arrayBubblesort.sort (arr); }        /** * Bubble sort * Put decimals forward, large numbers back*/     Public voidSortintarr[]) {        /** Outer loop control sort Wheel number * sort Count = array Length-1 * Inner loop control size * Inner layer count is affected by the number of outer layers, each comparison length = length-1 */         for(intI=1; i<arr.length;i++){            //compared with two adjacent elements, a larger             for(intj=0; j<arr.length-i;j++){                if(arr[j]>arr[j+1]){                    //define a temporary variable                    inttemp =Arr[j]; ARR[J]= arr[j+1]; Arr[j+1] =temp; }            }        }        //output all elements after sortingShowarray (arr); }        /** * Display array * @param arr*/     Public voidShowarray (intarr[]) {         for(intX:arr) {System. out. print (x +"<"); }    }}

Direct Select sort

Note the difference from bubble sorting, instead of swapping adjacent elements, instead of the entire array, select the largest position in the sorted array relative to the top or bottom, and then backward

Compared with bubble sort, the number of exchanges is less and faster.

The direct selection sort implementation code is as follows:

 Public classSelectsort { Public Static voidMain (string[] args) {//creating a one-dimensional array        intArr[] = {4, -,1, the,3, the}; //creating an Instance objectSelectsort Selectsort =NewSelectsort (); //call the Sort method, the object being sorted is the ARR arraySelectsort.sort (arr); }        /** Direct Sort * Note the difference from the bubble sort * instead of swapping adjacent elements, instead of the entire array, select the largest position in the top or bottom of the sorted array, and then backward * The number of exchanges is less and faster than the bubble sort .*/     Public voidSortintarr[]) {        intindex; //Outer Loop         for(intI=1; i<arr.length-1; i++) {Index=0; //Inner Loop//System.out.println ("i =" + i);             for(intj=1; j<=arr.length-i;j++){                //System.out.println ("front: index =" + Index + ", j =" + J ");                if(arr[j]>Arr[index]) {                    /** Index is used to select the largest number in the array * Assign the selected number to Arr[arr.length-i] */Index=J; //System.out.println ("after: index =" + Index + ", j =" + J ");                }            }            //System.out.println ("****************"); //Swap Location            inttemp = arr[arr.length-i]; Arr[arr.length-I] =Arr[index]; Arr[index]=temp;    } showarray (arr); }        /** * Display array * @param arr*/     Public voidShowarray (intarr[]) {         for(intX:arr) {System. out. print (x +"<"); }    }}

Reverse order

Can only be applied to arrays that are already sorted, and want to change their ordering.

For example, change the array from large to small to a large order.

The reverse order implementation code is as follows:

 Public classReversesort { Public Static voidMain (string[] args) {//creating a one-dimensional array        intArr[] = {Ten, -, -, +, -}; //creating an Instance objectReversesort Reversesort =NewReversesort (); //call the Sort method, the object being sorted is the ARR arrayReversesort.sort (arr); }        /** * Reverse Sort * can only be applied to arrays that have already been ordered, and want to change their sorting methods * For example, change the array from large to small to large order*/     Public voidSortintarr[]) {        //Pre-sort array original contentsSystem. out. println ("The original contents of the array:");        Showarray (arr); inttemp; intLen =arr.length;  for(intI=0; i<len/2; i++) {Temp=Arr[i]; Arr[i]= arr[len-1-i]; Arr[len-1-I] =temp; } System. out. println ("\ n"+"The contents of the array after inversion:"); //post-sorted contentShowarray (arr); }        /** * Display array * @param arr*/     Public voidShowarray (intarr[]) {         for(intX:arr) {System. out. Print ("\ t"+x); }    }}

The difference between the three:

Both the bubbling and direct selection sorts can sort the unordered array, and the reverse order can only sort the ordered array, such as changing the array from large to small to large.

The principle and difference of bubble sort, direct selection sorting, reverse ordering in array

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.