Data structure and algorithm foundation Module VI

Source: Internet
Author: User

Simple selection Sort:

Sets the number of records for the sorted sequence to n. I take the,..., n-1, and from All N-i+1 Records (R,r[i+1],..., R[n] to find the smallest record of the sort code, exchange with the first record. The sequencing of the record sequence is completed after the n-1 is executed.

The code is as follows:


        public  void Simpleselect ()          {            int[] Inputintarray = new Int[8] {8, 4, 7, 5, 2, 3, 6, 1};            for (int i = 1; i < Inputintarray.leng Th i++)             {                 int t = i-1;                 for (int j = i; J < Inputintarray.length; J + +)    & nbsp;            {                     if (Inputintarray[t] > INPUTINTARRAY[J]) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;       {                         T = j;                    }                }                 int temp = inputintarray[t];                 Inputintarray[t] = inputintarray[i-1];                 Inputintarray[i-1] = temp;                 Printsortedresult (Inputintarray, i);           }         }         public  void Printsortedresult (int[] inputIntArray, int num) & nbsp;       {            StringBuilder sb = new StringBuilder ();            for (int i = 0; i < inputintarray.length; i++)             {                 if (i = = 0)                  {                     sb. Append (Inputintarray[i]. ToString ());               }                 ELSE&NBSP;&NBSP;&NBsp;             {                     sb. Append ("," + inputintarray[i). ToString ());               }            }             Console.WriteLine ("First" + num + "Order results: " + sb.) ToString () + "");              } bubble sort

1: Principle

This compares the next two elements, each time the most complete word runs to the end of this round.
Purpose: Sort by small to large.
Method :
Suppose there are arrays: 72, 54, 59, 30, 31, 78, 2, 77, 82, 72
The first round compares adjacent two elements, and if the left element is greater than the right element, the interchange.
The result of comparisons between 72 and 54 is that 54 is in front and 72 is behind;
Then the results of 72 and 59 comparisons, 59 in front, 72 in the rear;
And so on, the result after the first round comparison is: 54, 59, 30, 31, 72, 2, 77, 78, 72, 82
After the first round of comparisons, the largest element ran to the last, so the second round, the last element did not need to be compared.
The second round or from the index 0 and 1 to compare, just don't want to compare the last, the algorithm is the same. Third round, fourth round and so on.
Results after sorting: 2, 30, 31, 54, 59, 72, 72, 77, 78, 82

2: Code


static list<int> List = new List<int> () {2,-------* tic void Main (string[] args) {    Bubble ();    printlist ();  static void Bubble () {    int temp = 0;    for (int i = list. Count; i > 0; i--)     {        for (int j = 0; J < I-1; J + +) &NBSP;&NBSP;&NBSP;&NB sp;    {            if (List[j] > list[j + 1 ])             {                 temp = list[j];                 List[j] = list[j + 1];                 list[j + 1] = TEMP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;    }       }         Printlist ();   }} private static void Printlist () {    foreach (var item in List)     {        Console.Write (string. Format ("{0}", item));   }    Console.WriteLine ();}

Data structure and algorithm base module six

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.