Java bubble sort and direct selection sort code essay

Source: Internet
Author: User
Tags array sort

Bubble sort: There are many kinds of enshin, some first determine the maximum value put to the back, some first determine the minimum value to the front, there is the reverse, first determine the minimum value of the position, but the essence is: constantly 22 comparison, exchange position ... The first trip to determine a maximum (minimum) value placed in front (behind), the second trip, the exclusion of the selected value of the continuation of the platoon .... The number of trips is outside the loop control, the exchange position of the matter to the inner loop.
Direct selection Sort: this should be faster than bubble sort, because it is only the inner loop to determine the maximum (small) index value, and then inside the outer loop only one time to exchange the most value, and bubble sort as long as the value is not the same, each time to exchange the relative maximum value, see the code to understand again.

In nested loops, the variables of the outer loop are generally imported into the inner loop condition, so that the external loop variable is changed, which will lead to the change of the inner cycle condition and reduce the number of inner cycles.

Because just looked at a simple factory model, so use the factory model to try to practice the sort, including bubbling, direct selection and a sort reversal (in fact, should not be classified into the sorting interface), because just contact Factory mode, code, just use the int[] type array, do not consider other types of arrays and positive sequence/reverse order, The positive order/reverse order is to modify the exchange condition in the inner loop greater than or less than the change.

Package javafirst;/** * This interface defines the Sort method * */interface sort{/** * Sorting method * @param arr needs to sort int array * @return Returns an array of type int */public in T[] Sort (int[] arr);} /** * Implement bubble sort */class Bubblesort implements Sort{public int[] Sort (int[] arr) {//because the outer for loop is the control loop number, starting from 1 more intuitive for (int i = 1; i < Arr.length; i + +) {for (int k = 0; k < arr.length-i; k++) {if (Arr[k] > arr[k+1]) {int temp = Arr[k];arr[k] = arr[k+1];arr[k+1] = temp;}}} return arr;}} /** * Implement direct sort */class Selectsort implements Sort{public int[] sort (int[] arr) {for (int i = 1; i < arr.length; i++) {int ind ex = 0;//The purpose of this inner loop is to find the index of the maximum value for (int k = 1; k <= arr.length-i; k++) {if (Arr[k] > Arr[index])//Here is just the Exchange index, not the exchange value index = k; }//when the upper loop loop is complete, a maximum value is selected, its index value is indexed, and then the value exchanged with the array is located//here is worth exchanging int temp = arr[arr.length-i];arr[arr.length-i] = Arr[index ];arr[index] = temp;} return arr;}} /** * Implement reverse sort */class Reversesort implements Sort{public int[] sort (int[] arr) {//is the relative position before and after the swap array for (int i = 0; i < Arr.leng TH/2; i + +) {int temp = Arr[i];arr[i] = arr[arr.length-1-i];arr[arr.length-1-I] = temp;} return arr;}} /** * Sort class Instance factory */class sortfactory{/** * instantiation * @param sortname Sort Method * @return Returns a sort instance */public sort Createsort (String sor Tname) {String str = sortname.tolowercase (); Sort sor = Null;switch (str) {case "Bubblesort": sor = new Bubblesort (); Break;case "Selectsort": sor = new Selectsort (); Case "Reversesort": sor = new Reversesort (); return SOR;}} /** * Traversal Array class */class show{public void Showarray (int[] arr) {for (int i:arr) {System.out.print (i + "");} System.out.println ();}} public class Test13 {public static void main (string[] args) {Show show = new Show ()//Use bubble sort int[] arr1 = new int[]{2,4,5,3, 6,1,12,14,17,22,15,18,16,12,13,18,10}; System.out.println ("Output original array:"); Show.showarray (ARR1); Sort so1 = new Sortfactory (). Createsort ("Bubblesort"); int[] arrbubble = So1.sort (arr1); System.out.println ("bubble sort Result:"); Show.showarray (arrbubble);//Use direct sort int[] arr2 = new int[]{ 2,4,5,3,6,1,12,14,17,22,15,18,16,12,13,18,10}; System.out.println ("Output original array:"); Show.showarrAy (arr2); Sort SO2 = new Sortfactory (). Createsort ("Selectsort"); int[] Arrselect = So2.sort (ARR2); System.out.println ("Direct sort result:"); Show.showarray (arrselect);//reverse sort to use sorted array sort SO3 = new Sortfactory (). Createsort (" Reversesort "); int[] Arrreverse = So3.sort (ARR2); SYSTEM.OUT.PRINTLN ("invert sorted ordinal group result:"); Show.showarray (Arrreverse);}}

Output results

Java bubble sort and direct selection sort code essay

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.