二
27
2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| #include<stdio.h>
int main()
{
char str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char data[100];
printf("Input a number:\n");
int n,m,i=0;
scanf("%d",&n);
while(n!=0)
{
m=n & 0xf;
data[i]=str[m];
n=n >> 4;
i++;
}
data[i]='\0';
for(--i;i>=0;i--)
{
printf("%c",data[i]);
}
printf("\n");
return 0;
} |
其中不得不提的是,m=n & 0xf(除16取余)n=n >> 4(除16取商,右移4位),这样能够提高程序的执行效率,面试经常被问到的问题。
no comments | tags: c | posted in 算法相关
二
22
2009
关键代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| void selection_sort(int list[], int n)
{
int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{
if (list[j] < list[min])
{
min = j;
}
}
swap(&list[i], &list[min]);
}
} |
no comments | tags: c, 算法相关 | posted in 算法相关
二
22
2009
关键代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = choose_pivot(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
} |
原文:http://cprogramminglanguage.net/quicksort-algorithm-c-source-code.aspx
no comments | tags: c, quicksort, 算法相关 | posted in 算法相关