C # Bubble sort

Source: Internet
Author: User
C # Bubble sort

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text; Namespace sort {Class Bubblesorter {public static int[] Sort (int[] a) {Bubble              Sort (a);          return A; } private static void Bubblesort (int[] myArray) {for (int i = 0; i < myarray.length; i+          +)//number of cycles {for (int j = 0; J < myarray.length-1-I; j + +)//number of cycles per cycle {          Compare adjacent elements, move the value to the back = = "The last number is the largest of each cycle."                      So each cycle is 1 less than the number of previous cycles, i.e. J < myarray.length-1-I if (Myarray[j] > myarray[j + 1])                      {Swap (ref myarray[j], ref MYARRAY[J + 1]); }}}}//reference parameter with value parameter private static void Swap (ref int left, ref int rig              HT) {int temp;              temp = left;              left = right; Right = temp; }}} The bubbling sort algorithm works as follows:

The bubbling sorting algorithm works as follows:

1, compare the adjacent elements. If the first one is bigger than the second one, swap them both.

2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number. = = "The last number does not need to be compared, because it is the largest."

3. Repeat the above steps for all elements except the last one.

4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.

Above is the content of C # bubble sort, more related content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.