Classical algorithms, algorithms that appear in every language

Source: Internet
Author: User

The basic concept of a bubbling sort (bubblesort) is to compare the adjacent two numbers in turn, place decimals in front, and large numbers behind. That is, first compare the 1th and 2nd numbers, put the decimals before the large number. Then compare the 2nd and 3rd numbers, place the decimal before the large number, and then continue until you compare the last two numbers, put the decimals before the decimal, and put the large number behind. Repeat the above process, still starting from the first logarithm comparison (because it is possible because of the 2nd and 3rd number of exchanges, so that the 1th number is no longer less than the 2nd number), the decimal place before, after the large number of places, has been compared to the maximum number of adjacent pairs, the decimal place before the large number, the second end, Get a new maximum number in the penultimate number. So go on until the final sort is finished.
Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, so called bubble sort.
With the double loop, the outer loop variable is set to I and the inner loop variable is set to J. The outer loop repeats 9 times, and the inner loop repeats 9,8,...,1 times sequentially. Each of the two elements of the comparison is related to the inner loop j, they can be identified by a[j] and a[j+1], the value of I sequentially,..., 9, for each I, the value of J is in sequence,... 10-i.
Produce

In many programming, we need to sort a sequence of numbers to facilitate statistics, the common sort method is bubble sort, binary tree sort, select sort and so on. And bubble sort has been favored because of its concise thinking method and relatively high efficiency.
Sorting process

Imagine the sorted array r(1..n) vertically erect, each data element as a weight of bubbles, according to the principle of light bubbles can not under the heavy bubbles, from the bottom of the scan array r, where scanning to violate the principle of light bubbles, so that it "floating upward", so repeated, until the last two bubbles are light on the , the heavy is down so far.

Using System;
Using System.Collections;

Namespace ConsoleApplication11
{

public class Program
{

static void Main (string[] args)
{
int swap = 0;
int temp = 0;
Console.WriteLine ("Please input the Totalnumber");
int N = Convert.ToInt32 (Console.ReadLine ());
ArrayList arr=new ArrayList ();
Console.WriteLine ("Please enter n number to bubble sort:");
for (int i = 0; i < N; i++)
{

Arr. ADD (Convert.ToInt32 (Console.ReadLine ()));
}
for (int j=0;j<n-1;j++)
{
swap = 0;
for (int m = 0; m < n-j-1; m++)
{
if ((int) arr[m]> (int) arr[m+1])
{
swap = 1;
temp = (int) arr[m];
Arr[m] = arr[m + 1];
arr[m+ 1] = temp;
}

}
if (swap==0)
Break
}
for (int i = 0; I<arr. Count; i++)
{
Console.Write (arr[i]+ "");
}
Console.ReadLine ();
}
}
}

First, bubble sort (Bubble)
Using System;

Namespace Bubblesorter
{
public class Bubblesorter
{
public void Sort (int[] list)
{
int i,j,temp;
BOOL Done=false;
J=1;
while ((J<list. Length) && (!done))
{
Done=true;
For (i=0;i<list. length-j;i++)
{
if (list[i]>list[i+1])
{
Done=false;
Temp=list[i];
LIST[I]=LIST[I+1];
List[i+1]=temp;
}
}
j + +;
}
}
}

public class MainClass
{
public static void Main ()
{
Int[] Iarrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
Bubblesorter sh=new Bubblesorter ();
Sh. Sort (iarrary);
for (int m=0;m<iarrary.length;m++)
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
}
}
}

Second, choose the Sort (Selection)

Using System;

Namespace Selectionsorter
{
 public class Selectionsorter
 {
  private int min;
  public void Sort (int. [] list)
  {
   for (int i=0;i<list. length-1;i++)
   {
   min=i;
    for (int j=i+1;j<list. length;j++)
    {
    if (list[j]<list[min])
    min=j;
   }
   int t=list[min];
   List[min]=list[i];
   list[i]=t;
  }
 }
 }

public class MainClass
{
public static void Main ()
{
int[] iarrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
Selectionsorter ss=new Selectionsorter ();
Ss. Sort (iarrary);
for (int m=0;m<iarrary.length;m++)
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
}
}
}

Three, insert sort (insertionsorter)

Using System;

Namespace Insertionsorter
{
public class Insertionsorter
{
public void Sort (int [] list)
{
for (int i=1;i<list. length;i++)
{
int t=list[i];
int j=i;
while ((j>0) && (list[j-1]>t))
{
LIST[J]=LIST[J-1];
--j;
}
list[j]=t;
}
}
}

public class MainClass
{
public static void Main ()
{
Int[] Iarrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
Insertionsorter ii=new Insertionsorter ();
Ii. Sort (iarrary);
for (int m=0;m<iarrary.length;m++)
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
}
}
}

Iv. sort of Hill (Shellsorter)

Using System;

Namespace Shellsorter
{
public class Shellsorter
{
public void Sort (int [] list)
{
INT Inc;
For (inc=1;inc<=list. LENGTH/9;INC=3*INC+1);
for (; inc>0;inc/=3)
{
for (int i=inc+1;i<=list. LENGTH;I+=INC)
{
int t=list[i-1];
int j=i;
while ((J>inc) && (list[j-inc-1]>t))
{
LIST[J-1]=LIST[J-INC-1];
J-=inc;
}
list[j-1]=t;
}
}
}
}

public class MainClass
{
public static void Main ()
{
Int[] Iarrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
Shellsorter sh=new Shellsorter ();
Sh. Sort (iarrary);
for (int m=0;m<iarrary.length;m++)
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
}
}
}

Classical algorithms, algorithms that appear in every language

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.