Skip to content

Instantly share code, notes, and snippets.

@jiyeqian
Last active January 1, 2016 14:29
Show Gist options
  • Save jiyeqian/8157829 to your computer and use it in GitHub Desktop.
Save jiyeqian/8157829 to your computer and use it in GitHub Desktop.
Sort Algorithms
void QuickSort(int *seq, const int begin, const int end)
{
if (begin < end) {
int position = begin; // 选择起始元素作为划分参考
int value = seq[position];
for (int i = begin + 1; i < end; ++i) {
if (seq[i] < value) {
position++;
if (position != i) {
Swap(&seq[position], &seq[i]);
}
}
}
Swap(&seq[position], &seq[begin]);
QuickSort(seq, begin, position);
QuickSort(seq, position + 1, end);
}
}
// http://blog.2byoung.us/archives/83085.html
void shakerSort(int n)
{
int i,left = 0, right = n-1,shift = 0;
while(left <right)
{
for (i = left;i<right;i++)
{
if (a[i] > a[i+1])
{
swap(a[i],a[i+1]);
shift = i;
}
}
right = shift;
for (i = right;i>left;i--)
{
if (a[i] < a[i-1])
{
swap(a[i],a[i-1]);
shift = i;
}
}
left = shift;
}
}
// http://openhome.cc/Gossip/AlgorithmGossip/ShellSort.htm
// http://www.cs.princeton.edu/~rs/shell/shell.c
shellsort(itemType a[], int l, int r)
{ int i, j, k, h; itemType v;
int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
13776, 4592, 1968, 861, 336,
112, 48, 21, 7, 3, 1 };
for ( k = 0; k < 16; k++)
for (h = incs[k], i = l+h; i <= r; i++)
{
v = a[i]; j = i;
while (j >= h && a[j-h] > v)
{ a[j] = a[j-h]; j -= h; }
a[j] = v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment