"Fundamentals of Algorithmic Design and analysis" 5, bubble sort and selection sort

Source: Internet
Author: User

Package Cn.xf.algorithm.ch03;import java.util.arraylist;import Java.util.arrays;import java.util.List;/** * Data Sort * * @ Author Xiaof * */public class Sort {/** * sorting a given array select the sorting method each time you select the minimum value after the current sequence, and swap the current sequence data * * @param inputdata * @return */p Ublic static list<long> Selectionsort (List inputdata) {//Traverse data, set a minimum value int n = inputdata.size ();//Queue Length int Minindex = 0; Long min = 0l;//The last data is not aligned for (int i = 0; i < n-1; ++i) {//Initialize the index position of the minimum data Minindex = I;min = (Long) inputdata.get (MinI NDEX);//traverse the back data, compare the minimum for (int j = i + 1; j < n; ++j) {//Determine the size long TEMPJ = (long) inputdata.get (j); if (TEMPJ < min) { Min = tempj;//Minindex = j;//Exchange Data Sort.swap (Inputdata, Minindex, J);}} Returns the data return Inputdata after the sort is finished;} /** * Exchange data for I and J in the array * * @param resourcedata * @param i * @param j */public static void Swap (List resourcedata, int i, int j) {Object TempO = Resourcedata.get (i); Resourcedata.set (I, Resourcedata.get (j)); Resourcedata.set (J, TempO);} /** * Bubble Sort * * @param inputdata * @return */public StatIC list<long> bubblesort (List inputdata) {//bubble sort, is compared with the current element and subsequent data, a layer of bubble sort int n = inputdata.size (); for (int i = 0; I < n; ++i) {//traverse the second layer, for the exchange comparison, a round loop, is to select the last element//and then the next cycle compares the data will be 1 less than the original, that is, the loop n-i-1 cycle for (int j = 0; J < n-i-1; ++j) {Long Temp j = (Long) inputdata.get (j);//Gets the next data to compare long tempJ1 = (long) inputdata.get (j + 1);//From small to large, the current is larger than the back, Exchange data if (TEMPJ > t empJ1) {Sort.swap (Inputdata, J, j + 1);}}} return inputdata;} public static void Main (string[] args) {list<long> data = Arrays.aslist (89l, 45l, 68l, 90l, 29l, 34l, 17l);//LIST&L T long> Resultdata = selectionsort (data); list<long> Resultdata = bubblesort (data), for (Long data1:resultdata) {System.out.print (data1 + "\ t");}}}

  

"Fundamentals of Algorithmic Design and analysis" 5, bubble sort and selection sort

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.