Skip to content

Instantly share code, notes, and snippets.

@ukitazume
Created December 12, 2011 08:19
Show Gist options
  • Select an option

  • Save ukitazume/1465875 to your computer and use it in GitHub Desktop.

Select an option

Save ukitazume/1465875 to your computer and use it in GitHub Desktop.
#include <stdio.h>
typedef struct Nameval Nameval;
struct Nameval {
char *name;
int value;
};
void swap(int v[], int i, int j) {
int tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
void quick_sort(int v[], int n) {
int i, last;
if (n <= 1)
return;
swap(v, 0, rand() % n);
last = 0;
for (i = 1; i < n; i++)
if (v[i] < v[0])
swap(v, ++last, i); // もしちびに分類されるなら、lastよりも前により分けられるイメージ
swap(v, 0, last); // ピポットとlastを入れ替える. lastはピボットより小さい
quick_sort(v, last);
quick_sort(v+last+1, n-last-1); // vはポインタ
}
int main (int argc, const char * argv[])
{
int i;
int stock[] = {
1,
1234,
2436,
3451,
7362,
34,
272,
177,
2,
956,
2411,
9686
};
int size = sizeof(stock) / sizeof(stock[0]); // 一個ずつのデータサイズで、全体を割ったら、いくつあるかわかる
quick_sort(stock, size);
for (i = 0; i < size; i++) {
printf("%i\n", stock[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment