C # Implementation Bubbling, fast, select, and insert sorting algorithms

Source: Internet
Author: User

Last November, using bubble sort, using JS to achieve a ticket query results according to the ticket price and the departure date of the page without refreshing sort. This period of time customers also asked to change according to the actual payment price and date of the order, hastily changed, feel their own algorithm and data structure of the ability to almost deserted (as if there has not been such a capacity before ^_^). Here to organize a common sorting algorithm, implemented in C # for future re-use. The Code is cheap. See the concrete implementation.

1. Bubble sort

The sorted array of records R[1..N] is arranged vertically, and each record r[i] is considered to be a bubble with a weight of r[i].key. According to the principle that the light bubbles cannot be under the heavy bubbles, scan the array r from the bottom up: Whenever a light bubble is scanned against this principle, it "floats" (hence the name of bubbling). So repeatedly, until the end of any two bubbles are light on the top, the heavy in the next.

Using System;
Using System.Collections;
Using System.Collections.Generic;

Namespace Bubblesort
{
public class Bubblesorter
{
<summary>
Bubble sort
</summary>
<param name= "Numarr" ></param>
public void Sort (int[] numarr)
{
int tmpnum;
BOOL flag = FALSE; Exchange Flag
for (int i = 1; i < numarr.length i++)//MAX do numarr.length-1 trip sort
{
Flag = false;
for (int j = numarr.length-1 J >= i; j--)//scan the current unordered region from the bottom up
{
if (Numarr[j] < numarr[j-1])//Current unordered area: light below, "bubbling" to the top
{
Tmpnum = Numarr[j];
NUMARR[J] = numarr[j-1];
Numarr[j-1] = Tmpnum;
Flag = true;
}
}
if (!flag)//If no exchange occurs, terminate the algorithm
Return
}
}

public class Program
{
public static void Main ()
{
int[] Testarr = new int[] {1, 5, 11, 6, 4, 21, 99, 2, 15, 11, 34, 0, 33, 47};
Bubblesorter sh = new Bubblesorter ();
Sh. Sort (Testarr);
for (int m = 0; m < testarr.length; m++)
Console.Write ("{0}", Testarr[m]);
Console.read ();
}
}
}
}

Bubble Algorithm Summary:

Because each sort of order has added a bubble in the ordered area, after the n-1, in the ordered area there are n-1 bubbles, and the bubble in the disordered area is always greater than the weight of the bubble in the ordered area, so the whole bubble sort process needs to be n-1. If the exchange of bubbles is not found in a certain sort of order, then all bubbles in the unordered area to be sorted satisfy the light on and the heavier, so the bubbling sort process can be terminated after this trip sort. To do this, in the algorithm given below, introduce a Boolean flag, set it to false before each sort begins, and set it to true if an exchange occurs during the sorting process. Check the flag at the end of each trip, and stop the algorithm if no swap has occurred. (sorting can be done without flag, but it will result in unnecessary loops and comparisons).

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.