C # Sorting Algorithm Summary

Source: Internet
Author: User

1. Bubble Sorting

1. Bubble Sorting vertically sorts the sorted record array R [1. N]. Each record R [I] is considered as a bubble with the weight of R [I]. Key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: any light bubble that scans for a violation of this principle, so that it is "floating" (bubble name ). This is repeated until the last two bubbles are light and heavy. The following is an example of bubble-down.

 class program {//  // bubble sort ///  //   Public void sort (INT [] numarr) {int tmpnum; For (INT I = 0; I 
  
    numarr [J + 1]) {tmpnum = numarr [J]; numarr [J] = numarr [J + 1]; numarr [J + 1] = tmpnum ;}}} static void main (string [] ARGs) {int [] arr = new int [] {,}; program P = new program (); p. sort (ARR); For (int K = 0; k 
   
  

2. selection sort)

The basic idea of sorting is to select the records with the smallest keyword from the records to be sorted, and put them at the end of the sorted sub-files until all the records are sorted.

/// <Summary> /// the basic idea of selection sort is to select the record with the smallest keyword from the record to be sorted, /// the sequence is placed at the end of the sorted subfile until all records are sorted. /// </Summary> /// <Param name = "numarr"> </param> Public void selectionsort (INT [] numarr) {int min, tmpnum; for (INT I = 0; I <numarr. length-1; I ++) {min = I; for (Int J = I + 1; j <numarr. length; j ++) {If (numarr [J] <numarr [Min]) {min = J ;}} tmpnum = numarr [I]; numarr [I] = numarr [Min]; numarr [Min] = tmpnum;} static void main (string [] ARGs) {int [] arr = new int [] {,}; program P = new program (); p. selectionsort (ARR); For (int K = 0; k <arr. length; k ++) {console. write ("{0}", arr [k]) ;}}

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.