eEhdsa

Prepare for the coming interview...Data Structure

1, Exchange sorts

Bubble sort | Cocktail sort | Odd-even sort | Comb sort | Gnome sort | Quicksort

I think the top two popular algorithms are Bubble sort and Quick sort, expecially the quick sort which is widely used.

Bubble sort

#region Bubble sort
public static void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array.Length - i - 1; j++)
{
if (array[j] > array[j + 1])
{
array[j] = array[j] + array[j + 1];
array[j + 1] = array[j] - array[j + 1];
array[j] = array[j] - array[j + 1];
}
}
}
}
It compares each pair of adjacent items and swap them if they are in the wrong order, the pass through the list is repeated until no swaps are needed, then we get the sorted array.
After increment of the outer circle we get the biggest number from the current not sorted array.

 

Qucik sort

#region Quick sort
/// 
/// Average: O(nlog2N)
/// The worst: O(n^2)
/// 
///

public static void QuickSort(int[] array)
{
_QuickSort(array, 0, array.Length - 1);
}
private static void _QuickSort(int[] array, int low, int high)
{
if (low <= high)
{
int pivot = array[low]; //we can change this as we like
int i = low - 1;
int j = high + 1;
while (true)
{
while (array[++i] < pivot) ;
while (array[--j] > pivot) ;
if (i >= j)
{
break;
}
array[i] = array[i] + array[j];
array[j] = array[i] - array[j];
array[i] = array[i] - array[j];
}
//recursion
_QuickSort(array, low, i - 1);
_QuickSort(array, j + 1, high);
}
}
I think this is the most famous one in all these algorithms
1: choose a pivot from the list
2: separate the list to two parts, and ensure the left of the pivot numbers are all less then pivot, and the right of the pivot numbers are all bigger the pivot
3: then recursion the left and right parts

 

2, Insertion sorts

Insertion sort | Shell sort | Tree sort | Library sort | Patience sorting
The first two algorithms are well-known

Insertion sort

/// 
/// 基本有序时效率明显高,把第二次循环控制在循环体外
/// 完全有序(升) ---> 线性O(n)
/// 完全有序(降) ---> 最坏O(n^2)
/// 
///

public static void Insertionsort(int[] array)
{
int j;
for (int i = 1; i < array.Length; i++)
{
int toInsertElement = array[i];
for (j = i; j > 0 && toInsertElement < array[j - 1]; j--)
{
array[j] = array[j - 1]; //Move a[j-1] to a[j]
}
array[j] = toInsertElement; //insert here
}
}
Loop the items in the list and compare the item with the other items which in the left hand of it, and if the item is bigger then it, move it back, until we find the correct position and insert it there.

 

Shell sort

/// 
/// It's just like insertion sort, change the 1 to d, and when d comes 1, we get the sorted array
/// usually we think it's complexity is O(n^(3/2))
/// The better choice of d is such as ...121,40,13,4,1
/// S(i) = 3S(i-1)+1
/// 
///

///

public static void ShellSort(int[] array, int d)
{
int j;
for (int i = d; i < array.Length; i++)
{
int toInsertElement = array[i];
for (j = i; j > d - 1 && toInsertElement < array[j - d]; j -= d)
{
array[j] = array[j - d];
}
array[j] = toInsertElement;
}
}
It's just like Insertion sort, the difference is shell sort use a custom "d" instead of "1"

 

3, Selection sorts

Selection sort | Heapsort | Smoothsort | Cartesian tree sort | Tournament sort
Also the first two algorithms are well-known

Selection sort

public static void SelectionSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
{
array[i] = array[i] + array[j];
array[j] = array[i] - array[j];
array[i] = array[i] - array[j];
}
}
}
}
Select the smallest one to the head, and until the list sorted.

 

posted on 2009-10-28 11:00  eEhdsa  阅读(298)  评论(0编辑  收藏  举报

导航