Three sorts of Java: bubbling, selecting, inserting sort

Source: Internet
Author: User

Three sorts: Bubble, select, insert Sort

        public static void Bubblesort (int[] source) {//Exchange class sort thought: 22 compare the keywords to be sorted, and find that the records are exchanged in reverse until there are no reversed records. for (int i = source.length-1, i > 0; i--) {for (int j = 0; J < i; J + +) {if (source[                    J] > Source[j + 1]) {swap (source, J, j+1); }}}} public static void Selectsort (int[] source) {//Select class sort thought            : First finds the smallest element in an unordered sequence, holds it to the starting position of the sort sequence, and then finds the next smallest element from the remaining unsorted elements and places it at the end of the sorted sequence.                    for (int i = 0, i < source.length; i++) {for (int j = i+1; J < Source.length; J + +) {                    if (Source[j] > Source[i]) {Swap (source, I, j); }}}}//Starting with the first element, the element can assume that the element//is taken out of the next element, scanned from behind in the already sequenced element, if the element is greater than the new one, the element is moved to the next public static void Insertsort (int[] source) {for (int i = 1; i < source.length; i++) {for (int j = i; (J > 0) && (Source[j] < source[j-1]);                j--) {Swap (source, J, j-1); }}} private static void Swap (int[] source, int x, int y) {in            T temp = source[x];            SOURCE[X] = Source[y];        Source[y] = temp;            public static void Main (string[] args) {int[] a = {4, 2, 1, 3, 4, 6, 7, 8, 0};            int i;            Bubblesort (a);            for (i = 0;i<a.length;i++) {System.out.printf ("%d", a[i]);             }

  

Three sorts of Java: bubbling, selecting, inserting 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.