C # Common sorting algorithm

Source: Internet
Author: User

 //Bubble Sort Algorithm        Static voidMain (string[] args) {            int[] Array = {2,3,7,1,4,9,6,8,Ten }; varNewarrry =Bubblesort (array);  for(inti =0; I < Newarrry. Length; i++) {Console.WriteLine (newarrry[i]);        } console.read (); }         Public Static int[] Bubblesort (int[] Array) {            inttemp =0;  for(inti =0; I < array. Length-1; i++)            {                 for(intj = i +1; J < Array. Length; J + +)                {                    //if (Array[j] < array[i])//Bubbling Ascending//{                    //temp = array[i]; //Array[i] = array[j]; //Array[j] = temp; //}                    if(Array[j] > Array[i])//Bubble Descending{Temp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; }                }            }            returnArray; }        //Select sorting Algorithm        Static voidMain (string[] args) {            int[] Array = {2,3,7,1,4,9,6,8,Ten }; varNewarrry =Selectsort (array);  for(inti =0; I < Newarrry. Length; i++) {Console.WriteLine (newarrry[i]);        } console.read (); }         Public Static int[] Selectsort (int[] Array) {            inttemp =0; intMinindex =0;  for(inti =0; I < array. Length-1; i++) {Minindex=i;  for(intj = i; J < Array. Length; J + +)                {                    if(Array[j] < Array[minindex])//Each loop selects the smallest number to insert into the array{Minindex=J; }} Temp=Array[minindex]; Array[minindex]=Array[i]; Array[i]=temp; }            returnArray; }        //Insert Sort Algorithm        Static voidMain (string[] args) {            int[] Array = {2,3,7,1,4,9,6,8,Ten }; varNewarrry =Insertsort (array);  for(inti =0; I < Newarrry. Length; i++) {Console.WriteLine (newarrry[i]);        } console.read (); }         Public Static int[] Insertsort (int[] Array) {             for(inti =1; I < array. Length; i++)            {                intt =Array[i]; intj =i;  while(J >0) && (Array[j-1] >t)) {ARRAY[J]= Array[j-1]; J--; } Array[j]=T; }            returnArray; }        //Hill Sort Algorithm        Static voidMain (string[] args) {            int[] Array = {2,3,7,1,4,9,6,8,Ten }; varNewarrry =Shellsort (array);  for(inti =0; I < Newarrry. Length; i++) {Console.WriteLine (newarrry[i]);        } console.read (); }         Public Static int[] Shellsort (int[] Array) {            intInc;  for(inc =1; Inc <= Array. Length/9; inc =3* Inc +1) ;  for(; Inc >0; Inc/=3)            {                 for(inti = inc +1; I <= array. Length; i + =Inc) {                    intt = array[i-1]; intj =i;  while(J > Inc) && (Array[j-inc-1] >t)) {Array[j-1] = Array[j-inc-1]; J-=Inc; } array[j-1] =T; }            }            returnArray; }

C # Common sorting algorithm

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.