Interview often test various sorting algorithm summary. (C #)

Source: Internet
Author: User

I. Interview and the examination process will appear a sorting algorithm interview questions, in order to deepen the understanding of the sorting algorithm, in this I have a summary of various sorting algorithms to summarize.

1. Bubble sorting algorithm (Bubblesort)

1  PublicClass Sortdemo2 {3       Public voidBubblesort (intarr)4      {5             inttemp=0;6           //need to go to arr. Length-1 Trip7             for(intI=0; I<arr. length-1; i++)8            {9                  //number of comparisons required per tripTen                   for(intj=0, J<arr. length-i-1; j + +) One                  { A                      //Sort Ascending -                       if(arr[j]>arr[j+1]) -                       { theTEMP=ARR[J];//save a large variable in a temporary variable -arr[j]=arr[j+1]; -arr[j+1]=temp; -                        }    +                  } -             } +           A      } at}

2. Direct Insert sort (insertionsort)

 PublicClass sortdemo{ Public voidInsertionsort (int[] arr) {             inttemp=0; //traverse the number to be inserted (starting from the second bit)              for(intI=1; I<arr. length;i++)             {                     intj=i-1;//(J is a sorted ordinal of the position to be inserted)//If the sorted number is greater than the number to be inserted, move backward one                      while(j>=0&&arr[j]>Arr[i]) {Arr[j+1]=Arr[j]; J--; } arr[j+1]=arr[i];//put the number you want to insert into the insertion position              }      }}

3. Select Sort (selectionsort)

 Public void Selectionsort (int[] arr)     {
int temp;
for (int i=0;i<arr.) length-1;i++)
{
int minval=arr[i];
int minindex=i;
for (int j=i+1;j<arr. length;j++)
{
if (Minval>arr[j])

MINVAL=ARR[J];
Minindex=j;
}
}
Temp=arr[i];
Arr[i]=minval;
Arr[minindex]=temp;
} }}

Interview often test various sorting algorithm summary. (C #)

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.