Stable sort algorithms sort repeated elements in the same order that they appear in the input.

When sorting some kinds of data, only part of the data is examined when determining the sort order. 

330px Sorting stability playing cards

要交换两个变量中的值,如何做?

temp = a;
a = b;
b = temp;

Right shift

Inserting element in front of an array

Using Iteration to do a left shift in an array

Mergesort

In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.

The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower, and sometimes pluralized) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod.

Factorial

A classic example of a recursive procedure is the function used to calculate the factorial of a natural number:

operatorname {fact} (n)={begin{cases}1&{mbox{if }}n=0ncdot operatorname {fact} (n-1)&{mbox{if }}n>0end{cases}}

Bubble sort

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swapsthem if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.[2] It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

In computer sciencebinary search, also known as half-interval searchlogarithmic search, or binary chop, is a search algorithmthat finds the position of a target value within a sorted array.

  1.   Pick up phone book
  2.   Open to middle of phone book
  3.   Look at page
  4.   If Smith is on page
  5.       Call Mike
  6.   Else if Smith is earlier in book
  7.       Open to middle of left half of book
  8.       Go back to line 3
  9.   Else if Smith is later in book
  10.       Open to middle of right half of book
  11.       Go back to line 3
  12.   Else
  13.       Quit