Bubble sort and simple selection sort--java implementation

Source: Internet
Author: User

1. Bubble sort

1) Principle: iterate over the sequence to be sorted, a comparison of two elements at a time, assuming they are in the wrong order to exchange them. The work of visiting the series is repeated until there is no need to exchange, that is to say, the sequence is sorted out.

2) Code implementation:

Package Com.test.sort;public class bubblesort{public    static void sort (int[] data)    {for        (int i = 0; i < da Ta.length; i++)        {            for (int j = data.length-1; j > i; j--)            {                if (Data[j] < data[j-1])                {                    swap (data, J , j-1);    }}} private static void Swap (int[] data, int a, int b)    {        int temp = Data[a];        Data[a] = data[b];        DATA[B] = temp;    }    public static void Main (string[] args)    {        int[] data = new int[] {3, 5, 3,,, 3423, 675, 4567};
   sort (data);        System.out.print ("result is: {");        for (int temp:data)        {            System.out.print (temp + "  ");        }        System.out.print ("}");}    }


2, simple selection of sorting

1) Principle: Simple selection of the basic idea of sorting is easy, that is: the first trip, from n elements to find keyword the smallest element and the first element exchange; The second pass, in the n-1 element starting from the second element, keyword the smallest element to be exchanged with the second element; , K-pass, the keyword smallest element is selected from the N-k+1 element beginning with the K element to be exchanged with the K element until the entire sequence is ordered in keyword order.

2) Code implementation:

Package Com.test.sort;public class selectsort{public    static void sort (int[] data)    {for        (int i = 0; i < da Ta.length; i++)        {            int index = i;            for (int j = data.length-1; j > i; j--)            {                if (Data[j] < Data[index])                {                    index = j;                }            }            Swap (data, I, index);        }    }    private static void Swap (int[] data, int a, int b)    {        int temp = Data[a];        Data[a] = data[b];        DATA[B] = temp;    }    public static void Main (string[] args)    {        int[] data = new int[] {3, 5, 3,,, 3423, 675, 4567};
   sort (data);        System.out.print ("Select sort result is: {");        for (int temp:data)        {            System.out.print (temp + "  ");        }        System.out.print ("}");}    }


Bubble sort and simple selection sort--java implementation

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.